2011-11-28 15 views
2

我試圖做如何從一個ListView項目獲取特定字符串進行


你好,大家好,我創建一個應用程序爲我的朋友,我只是表現出他的頻道在我的應用程序。我通過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()); 
     } 
} 
} 

預先感謝您

回答

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"); 


       } 
      }); 
+0

你好Nammari工作得很好!非常感謝! – safari

1

更好的方法是來包裝你的信息在一個Object,並從互聯網上下載數據並作出Object並將其添加到任何ArrayList檢索數據。

例如:

您將作出Java對象類。說: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)。

,然後在ListActivityOnItemClickListener你會得到這樣的方式你想要的對象:

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 
} 
+0

Thx對於你的anwser來說似乎很難實現,難道沒有更簡單的方法嗎? – safari

+0

:)更簡單的方法(IMO錯誤的做法)是將你的'mylist'定義爲課程級別並遵循@Nammari的答案。 –

+0

問題是atm我在java中沒有這麼深入的理解來實現:P,但是很快我就會! – safari

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"); 
} 
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"); 
} 
相關問題