我正在尝试做什么
Hello Guys,我正在为我的一个朋友创建一个应用程序,我只是在我的应用程序中显示他的频道。我通过JSON获得了视频数据。
现在我发现了一个问题。当我通过SimpleListAdapter将数据填充到ListView中并尝试通过OnItemClickListner获取它并通过lv.getItemAtPosition(position);
检索它时,我获得存储在特定行中的所有数据。但我只需要保存在该字段中的Link / Url。
问题
就像你读到的那样,我在ListView中获得了多个信息,确切地说有四个字符串(Thumb,Title,Content,Link)。但我只需要获取链接的字符串,我该怎么做,在这里找到代码。
代码
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());
}
}
}
先谢谢你
答案 0 :(得分:7)
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");
}
});
答案 1 :(得分:1)
更好的方法是将您的信息包装在Object
中,然后从互联网下载数据并制作Object
并将其添加到任何ArrayList
。
例如:
您将创建一个Java Object类。说: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
}
}
制作班级ArrayList
ArrayList<VideoInfo> myList = new ArrayList<VideoInfo>();
当你从服务器获取数据时解析它并像这样保存:
myList.add(new VideoInfo(txtTitle, txtContent, txtLink, txtThumb));
You will need customization of Adapter
(e.g override getView()
)设置自定义Ojbect(VideoInfo
)中的数据。
然后在ListActivity
的{{1}}中,您将以这种方式获得所需的对象:
OnItemClickListener
答案 2 :(得分:0)
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");
}
答案 3 :(得分:0)
例如,您可以执行类似的操作:
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");
}
答案 4 :(得分:-1)
我认为你可以使用它 http://developer.android.com/reference/android/widget/CursorAdapter.html#getItem(int)。
Cursor cursor = (Cursor) simple.getItem(position);
//从光标中检索数据