你好,大家好,我創建一個應用程序爲我的朋友,我只是表現出他的頻道在我的應用程序。我通過JSON獲取了視頻的數據。
現在我發現一個問題。當我通過SimpleListAdapter將數據填充到ListView中並嘗試通過OnItemClickListner獲取並通過lv.getItemAtPosition(position);
檢索它們時,我獲取存儲在特定行中的所有數據。但我只需要保存在該字段中的鏈接/網址。
問題
就像你讀我在我的ListView中一個以上的信息是完全有四根弦(拇指,標題,內容,鏈接)。但我只需要獲取鏈接的字符串,我怎麼能做到這一點,在這裏你可以找到代碼。
代碼
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());
}
}
}
預先感謝您
你好Nammari工作得很好!非常感謝! – safari