15

我對android開發非常陌生。我正在嘗試構建帶有標籤(通過片段添加)的應用程序。在其中一個片段中,我試圖顯示一個列表。這個列表已經使用ListAdapter填充,我已經從ArrayAdapter擴展了,並且我重載了getView()方法。 這是我的片段ArrayAdapter的getView()方法沒有被調用

public class tabcontentActivity extends Fragment { 

    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
     if (container == null) { 
      return null; 
     } 
     View v = (LinearLayout) inflater.inflate(R.layout.tablayout, container, 
      false); 
     ListView lv = (ListView) v.findViewById(R.id.listview1); 
     ListViewAdapter adapter = new ListViewAdapter(container.getContext(), 
      android.R.layout.simple_list_item_1, R.id.textview1); 
     adapter.notifyDataSetChanged(); 
     lv.setAdapter(adapter); 

     return v; 
    } 
} 

這該是多麼我已經實現了ListAdapter

public class ListViewAdapter extends ArrayAdapter { 
    Context context1; 

    public ListViewAdapter(Context context,int resource, int textViewResourceId) { 
     super(context,resource,textViewResourceId); 
     this.context1 = context; 
     System.out.println("here aswell!!!!!!"); 
     // TODO Auto-generated constructor stub 
    } 

    public View getView(int arg0, View convertView, ViewGroup arg2) { 
     // TODO Auto-generated method stub 
     System.out.println("@@@I AM [email protected]@@"); 
     LayoutInflater inflater = LayoutInflater.from(context1); 
     convertView = inflater.inflate(R.layout.tablayout, null); 

     TextView wv = (TextView) convertView.findViewById(R.id.textview1); 
     String summary = "<html><body><h1>This is happening!!!</h1></body></html>"; 
     wv.setText(Html.fromHtml(summary)); 

     convertView.setTag(wv); 

     return convertView; 
    } 
} 

佈局XML是

<?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="vertical" > 

    <ListView 
     android:id="@+id/listview1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" > 
    </ListView> 

    <TextView 
     android:id="@+id/textview1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:text="myreader" /> 

</LinearLayout> 

請幫我找到一個辦法讓我可以做這個工作。

回答

48

你剛纔忘了提供數據的構造函數,然後讓在構造函數中調用正確:

public ListViewAdapter(Context context,int resource, int textViewResourceId, List<YourObjectType> items) { 
    super(context,resource,textViewResourceId, items); 
    this.context1 = context; 
    // TODO Auto-generated constructor stub 
} 
+2

OMG感謝。這幫助了我......! – jonassvensson 2014-03-22 20:53:56

+0

'super(context,viewResourceId,items);'這個也適合我 – 2017-11-14 09:15:32

1
adapter.notifyDataSetChanged(); 
    lv.setAdapter(adapter); 

互換位置

Layoutinflator=getLayoutInflator(); 
2

它看起來像您沒有指定爲您的適配器的數據源。數據源是提供要顯示的數據的任何事物,例如數組列表或遊標。如果沒有來自數據源的數據,getview()根本不會被調用。

16

您目前沒有提供任何數據。如果您仍想查看列表中發生了什麼,請覆蓋getCount()函數。

我認爲這是類似的東西。

public int getCount(){ 
    return 1; 
} 

這將在您的列表中繪製一個項目。還可以調用你的getView()。而且一旦你發現如何提供數據源,將getCount()更改爲列表大小。看看這裏的教程

http://www.shubhayu.com/android/listview-with-arrayadapter-and-customized-items

0
 public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    if (container == null) { 
     View v = (LinearLayout) inflater.inflate(R.layout.tablayout, container, 
       false); 
     ListView lv = (ListView) v.findViewById(R.id.listview1); 
    ListViewAdapter adapter = new ListViewAdapter(container.getContext(), 
     android.R.layout.simple_list_item_1, R.id.textview1); 
    adapter.notifyDataSetChanged(); 
    lv.setAdapter(adapter); 
    } 
    else{ 
    View v = (LinearLayout) inflater.inflate(R.layout.tablayout, container, 
     false); 
     ListView lv = (ListView) v.findViewById(R.id.listview1); 
    ListViewAdapter adapter = new ListViewAdapter(container.getContext(), 
     android.R.layout.simple_list_item_1, R.id.textview1); 
    adapter.notifyDataSetChanged(); 
    lv.setAdapter(adapter); 
} 
return v; 
} 

,並覆蓋getCount將方法

public int getCount(){ 
    return 100; 
    } 
6

當我們創建一個自定義arrayadapter我們必須使用任何

public ArrayAdapter (Context context, int textViewResourceId, T[] objects) 

public ArrayAdapter (Context context, int resource, int textViewResourceId, T[] objects) 

public ArrayAdapter (Context context, int textViewResourceId, List<T> objects) 

public ArrayAdapter (Context context, int resource, int textViewResourceId, List<T> objects) 

如果我們不使用上面提到的適配器,我們需要重寫getCount()方法。