How to get a specific String out from a ListView Item
-
10-03-2021 - |
Domanda
What I'm trying to do
Hello Guys, I'm creating an App for a friend of mine, i simply show his Channel in my App. I get the Data of the Videos over JSON.
Now I found a Problem. When I filled the data into my ListView over the SimpleListAdapter and try to get it over an OnItemClickListner and retrieve them over lv.getItemAtPosition(position);
I get all data which is stored in the specific row. But I only need the Link/Url I saved in that field.
Question
Like you read I got more than one information in my ListView to be exactly there are four Strings (Thumb,Title, Content, Link). But I only need to get the String of the Link, how can I do this, down here you find the Code.
Code
test2.java
package de.stepforward;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;
import android.app.ListActivity;
import android.database.Cursor;
import android.media.RingtoneManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class test2 extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String result = "";
String line = null;
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
//get the Data from URL
try{
URL url = new URL("http://gdata.youtube.com/feeds/mobile/users/TheStepForward/uploads?alt=json&format=1");
URLConnection conn = url.openConnection();
StringBuilder sb = new StringBuilder();
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
//read d response till d end
while ((line = rd.readLine()) != null) {
sb.append(line + "\n");
}
result = sb.toString();
Log.v("log_tag", "Append String " + result);
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
try{
JSONObject json = new JSONObject(result);
JSONObject feed = json.getJSONObject("feed");
JSONArray entrylist = feed.getJSONArray("entry");
for(int i=0;i<entrylist.length();i++){
//Get Title
JSONObject movie = entrylist.getJSONObject(i);
JSONObject title = movie.getJSONObject("title");
String txtTitle = title.getString("$t");
Log.d("Title", txtTitle);
//Get Description
JSONObject content = movie.getJSONObject("content");
String txtContent = content.getString("$t");
Log.d("Content", txtContent);
//Get Link
JSONArray linklist = movie.getJSONArray("link");
JSONObject link = linklist.getJSONObject(0);
String txtLink = link.getString("href");
Log.d("Link", txtLink);
//Get Thumbnail
JSONObject medialist = movie.getJSONObject("media$group");
JSONArray thumblist = medialist.getJSONArray("media$thumbnail");
JSONObject thumb = thumblist.getJSONObject(2);
String txtThumb = thumb.getString("url");
Log.d("Thumb", txtThumb.toString());
//String Array daraus machen und in Hashmap füllen
HashMap<String, String> map = new HashMap<String, String>();
map.put("Thumb", txtThumb);
map.put("Title", txtTitle);
map.put("Content", txtContent);
map.put("Link", txtLink);
mylist.add(map);
}
//ListView füllen
ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.lit,
new String[] { "Thumb","Title","Content","Link"},
new int[] { R.id.img_video,R.id.txt_title,R.id.txt_subtitle});
setListAdapter(adapter);
//OnClickLister um Youtube-Video zu öffnen
final ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
lv.getItemAtPosition(position);
}
});
}catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
}
}
Thank you in Advance
Soluzione
final ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Map<String, String> map = mylist.get(position);
String link = map.get("Link");
}
});
Altri suggerimenti
Better way is to wrap your information in an Object
and download the data from internet and make an Object
and add it to any ArrayList
.
For Example:
You will make an Java Object class. say: VideoInfo
public class VideoInfo
{
String title, content, link, thumb;
// define there setter getter
// also a constructor with these params
public VideoInfo(String title,String content,String link,String thumb)
{
//set Values accordingly
}
}
make a class level ArrayList
ArrayList<VideoInfo> myList = new ArrayList<VideoInfo>();
and when you get data from server parse it and save it like this way:
myList.add(new VideoInfo(txtTitle, txtContent, txtLink, txtThumb));
You will need customization of Adapter
(e.g override getView()
) to set data from your custom Ojbect (VideoInfo
).
and then in your ListActivity
in OnItemClickListener
you will get your desired object like this way:
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// you have class level ArrayList myList.
myList.get(position).getLink(); //getLink() will be the getter of you field link in VideoInfo
}
lv.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
HashMap<String, String> selectedHashMapObject = (HashMap<String, String>)lv.getItemAtPosition(position);
String selectedLink = selectedHashMapObject.get("Link");
}
For example, you can do something similar like this:
for(int i = 0; i < ((ListView)lv).getItemCount(); i++){
Map<String, String> currentView = (Map<String, String>) ((ListView)lv).getItemAtPosition(i);
String cVId = currentView.get("id");
}
i think you can use this http://developer.android.com/reference/android/widget/CursorAdapter.html#getItem(int).
Cursor cursor = (Cursor) simple.getItem(position);
// retrieve the data from the cursor