Android 未调用ArrayAdapter的getView()方法

Android 未调用ArrayAdapter的getView()方法,android,android-layout,android-fragments,android-arrayadapter,Android,Android Layout,Android Fragments,Android Arrayadapter,我对安卓开发非常陌生。我正在尝试用标签(通过片段添加)构建一个应用程序。在其中一个片段中,我试图显示一个列表。这个列表是使用ListAdapter填充的,我从ArrayAdapter扩展了ListAdapter,并重载了getView()方法。 这是我的片段 public class tabcontentActivity extends Fragment { public View onCreateView(LayoutInflater inflater, ViewGroup cont

我对安卓开发非常陌生。我正在尝试用标签(通过片段添加)构建一个应用程序。在其中一个片段中,我试图显示一个列表。这个列表是使用ListAdapter填充的,我从ArrayAdapter扩展了ListAdapter,并重载了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 HERE@@@");
        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;
    }
}
公共类ListViewAdapter扩展了ArrayAdapter{
语境1;
公共ListViewAdapter(上下文上下文、int资源、int textViewResourceId){
超级(上下文、资源、textViewResourceId);
this.context1=上下文;
System.out.println(“这里也是!!!!!!!”;
//TODO自动生成的构造函数存根
}
公共视图getView(int arg0、视图转换视图、视图组arg2){
//TODO自动生成的方法存根
System.out.println(“我在这里@@”);
LayoutFlater充气机=LayoutFlater.from(上下文1);
convertView=充气机充气(R.layout.TableLayout,空);
TextView wv=(TextView)convertView.findViewById(R.id.textview1);
String summary=“这正在发生!!!”;
wv.setText(Html.fromHtml(摘要));
convertView.setTag(wv);
返回视图;
}
}
布局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>

请帮助我找到一种方法,我可以使这项工作

  adapter.notifyDataSetChanged();
  lv.setAdapter(adapter);
互换头寸

Layoutinflator=getLayoutInflator();

看起来您尚未为适配器指定数据源。数据源是提供要显示的数据的任何东西,例如arraylist或游标。如果数据源中没有数据,则根本不会调用getview()。

您当前没有向其提供任何数据。如果仍要查看列表中发生的情况,请重写getCount()函数

我想是这样的

public int getCount(){
    return 1;
}
这将在列表中绘制一个项目。并调用getView()。一旦您找到了如何提供数据源,请将getCount()更改为列表的大小。在这里查看教程

并重写getcount方法

 public int getCount(){
    return 100;
  }

创建自定义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)
publicArrayAdapter(上下文上下文,int textViewResourceId,T[]对象)
公共ArrayAdapter(上下文上下文、int资源、int textViewResourceId、T[]对象)
公共ArrayAdapter(上下文上下文、int textViewResourceId、列表对象)
公共ArrayAdapter(上下文上下文、int资源、int textViewResourceId、列表对象)

如果我们不使用上述适配器,我们需要重写getCount()方法。

您只是忘记了向构造函数提供数据,然后在构造函数中进行正确的调用:

public ListViewAdapter(Context context,int resource, int textViewResourceId, List<YourObjectType> items) {
    super(context,resource,textViewResourceId, items);
    this.context1 = context;
    // TODO Auto-generated constructor stub
}
公共ListViewAdapter(上下文上下文、int资源、int textViewResourceId、列表项){ 超级(上下文、资源、textViewResourceId、项目); this.context1=上下文; //TODO自动生成的构造函数存根 }
super(上下文、视图资源ID、项目)这一个也适用于me@Shubhayu.it帮助我
public ListViewAdapter(Context context,int resource, int textViewResourceId, List<YourObjectType> items) {
    super(context,resource,textViewResourceId, items);
    this.context1 = context;
    // TODO Auto-generated constructor stub
}