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()方法。