2012-03-20 1 views
0

i hav JSON 피드를 구문 분석하여 목록보기에서 이미지의 썸네일과 함께 제목을 표시하십시오. 내 코드목록보기에 표시 할 이미지 URL과 제목을 포함하는 arraylist를 전달하시오.

public class Main extends ListActivity{ 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.listplaceholder); 

    String url; 
    Bitmap bitmap; 
    ImageView img; 
    int im ; 
    ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();  

    JSONObject json = Json_mysamActivity.getJSONfromURL("http://gdata.youtube.com/feeds/mobile/videos?max-results=3&alt=json");   

    try { 
     JSONObject jb = json.getJSONObject("feed");   
     JSONArray jarr = jb.getJSONArray("entry"); 
     for(int i=0;i<jarr.length();i++){      
      HashMap<String, String> map = new HashMap<String, String>();  
      JSONObject e = jarr.getJSONObject(i); 
      JSONObject det = e.getJSONObject("title"); 
      map.put("id", String.valueOf(i)); 
      map.put("$t", "t:" + det.getString("$t")); 
      map.put("type", "type: " + det.getString("type")); 


      JSONObject det1 = e.getJSONObject("media$group"); 
      JSONArray det12 = det1.getJSONArray("media$thumbnail"); 
      for(int j=0;j<1;j++) 
      { 
       JSONObject det12obj = det12.getJSONObject(i); 
       url = det12obj.getString("url"); 
     map.put("url", "url:"+ det12obj.getString("url")); 

       mylist.add(map); 
      } 

     }  

    } catch (JSONException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    }   

    String[] from = new String[] { "$t", "type","url" }; 
    int[] to = new int[] { R.id.item_title, R.id.item_subtitle, R.id.test_image }; 
    MySimpleCursorAdapter adapter = new MySimpleCursorAdapter(this, R.layout.main, mylist , 
    from, to); 

    setListAdapter(adapter); 
    final ListView lv = getListView(); 
    lv.setTextFilterEnabled(true); 
    lv.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) {    
      @SuppressWarnings("unchecked") 
      HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);     
      Toast.makeText(Main.this, "ID '" + o.get("id") + "' was clicked.", Toast.LENGTH_SHORT).show(); 

     } 
    }); 
} 
public class MySimpleCursorAdapter extends SimpleCursorAdapter { 
// SimpleCursorAdapter requires cursor in the place of ArrayList but I need to pass //arraylist, how should I implement it 
     public MySimpleCursorAdapter(Context context, int layout, ArrayList<HashMap<String, String>> mylist, 
      String[] from, int[] to) { 
      super(context, layout, mylist, from, to); 
     } 

    @Override 
    public void setViewImage(ImageView v, String url) { 

     String path =url ; 
     Bitmap b = BitmapFactory.decodeFile(path); 
     v.setImageBitmap(b); 

    } 

} 
} 
+0

다음 질문을 올바르게 수정하는 데 더 많은 시간을 할애하십시오. 고마워요 @ 루시퍼 – Snicolas

답변

0

SimpleAdapter이 필요합니다. 커서 어댑터는 데이터베이스에서 오는 목록에 daat를 표시하는 데 사용됩니다. SimpleAdapter는 RAM의 데이터 (json을 파싱 한 후 사용자의 목록)에 사용됩니다.

먼저 확인하십시오. google entry,해야합니다.

0

데이터 소스로 arraylist를 사용하는 경우 ArrayAdapter를 사용해야하므로 ArrayAdapter에서 어댑터를 확장하십시오. 사용자 정의 배열 어댑터에 대한 링크를 참조하십시오 당신이 목록에서 이미지 뷰를로드 할 때

http://android-er.blogspot.in/2010/06/custom-arrayadapter-with-with-different.html

이 LazyLoading를 사용하는 그것의 좋은 연습, 안드로이드에 게으른로드에 대한 아래 링크를 참조하십시오

http://ballardhack.wordpress.com/2010/04/05/loading-remote-images-in-a-listview-on-android/

0

이 경우 LayoutInflater를 사용하고 자신의 어댑터 클래스를 작성해야합니다. ArrayAdapter,

mInflater = (LayoutInflater) getSystemService (Context.LAYOUT_INFLATER_SERVICE);

어댑터 = 새 CustomAdapter (this, R.layout.list_row, R.id.title, data, mInflater, imgid);

관련 문제