2012-07-10 81 views
4

我擴展了一個覆蓋getView()方法的ArrayAdapter,以填補我的ListView對象最初的兩個三個項目,但該適配器將抓住從ArrayList我傳遞給它的前兩個或三個對象,那麼只需在整個列表中以表面上隨機的順序重複這些內容。此外,當我在ListView中上下滾動時,某些行從一個列表項變爲另一個列表項。 ArrayList<Credit>對象從JSONObject中填充,據我所知,在傳入ArrayAdapter之前是正確的。getView()ArrayAdapter的方法重複列表

我定製ArrayAdapter

private class CreditArrayAdapter extends ArrayAdapter<Credit> { 
    private ArrayList<Credit> credits; 
    private int creditCount; 

    public CreditArrayAdapter(Context ctx, int textViewResourceId, List<Credit> items) { 
     super(ctx, textViewResourceId, items); 
     credits = (ArrayList<Credit>) items; 
     creditCount = credits.size(); 
    } 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View v = convertView; 
     if (v == null) { 
      LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      v = vi.inflate(R.layout.list_item_movie_medium, null); 
      Credit current = credits.get(position); 
      Log.d(tag, "current credit: " + current.toString()); 
      ImageView creditPicture = (ImageView) v.findViewById(R.id.image_poster_thumbnail); 
      try { 
       creditPicture.setImageBitmap(current.getPosterSmall()); 
      } catch(Exception e) { 
       Log.d(tag, "failed to set cast member picture"); 
       e.printStackTrace(); 
      } 
      TextView castMemberName = (TextView) v.findViewById(R.id.text_credit_info); 
      castMemberName.setText(current.toString()); 
     } 
     return v; 
    } 
    @Override 
    public int getCount() { 
     return creditCount; 
    } 
} 

由被稱爲:

appearsIn = (ListView) findViewById(R.id.listview_appears_in); 
List<Credit> credits = searcher.getCreditsByPersonId(personId); 
CreditArrayAdapter creditAdapter = new CreditArrayAdapter(getBaseContext(), R.layout.list_item_movie_medium, credits); 
creditAdapter.notifyDataSetChanged(); 
appearsIn.setAdapter(creditAdapter); 

ListView佈局:

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical" 
       android:layout_centerHorizontal="true" 
       android:weightSum="2"> 
    <ScrollView android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:layout_weight="1"> 
     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
         android:layout_width="fill_parent" 
         android:layout_height="wrap_content" 
         android:orientation="vertical"> 
      <ImageView android:id="@+id/image_person_info" 
         android:layout_height="wrap_content" 
         android:layout_width="wrap_content" 
         android:contentDescription="person image"/> 
      <TextView android:id="@+id/text_person_name" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:textSize="30dp"/> 
      <TextView android:id="@+id/text_person_birth" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:textSize="20dp"/> 
      <TextView android:id="@+id/text_person_death" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:textSize="20dp"/> 
      <TextView android:id="@+id/text_person_bio" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:textSize="16dp"/> 
     </LinearLayout> 
    </ScrollView> 

    <ListView android:id="@+id/listview_appears_in" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:layout_weight="1"/> 
</LinearLayout> 

我的列表項佈局:

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:orientation="horizontal"> 
    <ImageView android:id="@+id/image_poster_thumbnail" 
       android:layout_height="wrap_content" 
       android:layout_width="wrap_content"/> 
    <TextView android:id="@+id/text_credit_info" 
       android:layout_height="wrap_content" 
       android:layout_width="fill_parent"/> 
</LinearLayout> 

回答

8

變化getView這樣的:

public View getView(int position, View convertView, ViewGroup parent) { 
    View v = convertView; 
    if (v == null) { 
     LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     v = vi.inflate(R.layout.list_item_movie_medium, null); 
    } 
    Credit current = credits.get(position); 
    Log.d(tag, "current credit: " + current.toString()); 
    ImageView creditPicture = (ImageView) v.findViewById(R.id.image_poster_thumbnail); 
    try { 
     creditPicture.setImageBitmap(current.getPosterSmall()); 
    } catch(Exception e) { 
     Log.d(tag, "failed to set cast member picture"); 
     e.printStackTrace(); 
    } 
    TextView castMemberName = (TextView) v.findViewById(R.id.text_credit_info); 
    castMemberName.setText(current.toString()); 

    return v; 
} 

你的問題是你沒有做所有你需要正確設置視圖對象時convertView是代碼! = null。

+0

完美的作品,感謝您的幫助! – Max 2012-07-10 14:41:13

+0

感謝你的這個,你幫我弄清楚瞭如果需要在後面(在'convertView'不爲空的情況下)我有一段代碼。這是造成我的卡充滿了錯誤的內容。 – justinraczak 2016-03-28 05:04:02

2

使用此片段:

@Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View v = convertView; 
     if (v == null) { 
      LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      v = vi.inflate(R.layout.list_item_movie_medium, null); 
     } 

     Credit current = credits.get(position); 
     Log.d(tag, "current credit: " + current.toString()); 
     ImageView creditPicture = (ImageView) v.findViewById(R.id.image_poster_thumbnail); 
     try { 
      creditPicture.setImageBitmap(current.getPosterSmall()); 
     } catch(Exception e) { 
      Log.d(tag, "failed to set cast member picture"); 
      e.printStackTrace(); 
     } 
     TextView castMemberName = (TextView) v.findViewById(R.id.text_credit_info); 
     castMemberName.setText(current.toString()); 

     return v; 
    } 
+1

我認爲你需要在沒有「其他」分支的情況下做到這點,否則當視圖膨脹時它會在通貨膨脹後立即返回。 – FoamyGuy 2012-07-10 14:36:26

+0

你是對的.. :) – 2012-07-10 14:38:45

0

嘗試在任何引用高度的地方調整您的xml佈局,因爲android會執行一個度量來繪製每次,並將重新繪製單元格。 嘗試使用listview match_parent和行上的單元格的高度。 對不起,我的英語不好。

相關問題