【问题标题】:The getView() method of ArrayAdapter is not getting calledArrayAdapter 的 getView() 方法没有被调用
【发布时间】:2012-03-16 00:43:04
【问题描述】:

我对 android 开发非常陌生。我正在尝试使用标签(通过片段添加)构建应用程序。在其中一个片段中,我试图显示一个列表。此列表已使用我从 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;
    }
}

布局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>

请帮助我找到一种方法来完成这项工作。

【问题讨论】:

    标签: android android-layout android-fragments android-arrayadapter


    【解决方案1】:

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

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

    【讨论】:

    • super(context, viewResourceId, items); 这个也对我有用
    【解决方案2】:

    您目前没有向它提供任何数据。如果您仍想查看列表中发生的情况,请覆盖 getCount() 函数。

    我认为是这样的。

    public int getCount(){
        return 1;
    }
    

    这将在您的列表中绘制一项。并调用您的 getView()。而且,一旦您了解了如何提供数据源,请将 getCount() 更改为列表的大小。在这里查看教程

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

    【讨论】:

    • @Shubhayu.it 帮助了我
    【解决方案3】:

    当我们创建自定义数组适配器时,我们必须使用任一

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

    【讨论】:

      【解决方案4】:

      您似乎没有为您的适配器指定数据源。数据源是提供要显示的数据的任何东西,例如数组列表或游标。如果数据源中没有数据,则根本不会调用 getview()。

      【讨论】:

        【解决方案5】:
          adapter.notifyDataSetChanged();
          lv.setAdapter(adapter);
        

        交换位置

        Layoutinflator=getLayoutInflator();
        

        【讨论】:

          【解决方案6】:
                 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;
            }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多