【问题标题】:The getView() method of custom ArrayAdapter is not getting called自定义 ArrayAdapter 的 getView() 方法没有被调用
【发布时间】:2018-03-28 02:21:25
【问题描述】:

我对 android 开发非常陌生。我正在尝试构建一个应用程序。我将 ArrayAdapter 用于列表视图,但未调用 ArrayAdapter getView() 方法。

我在 ArrayAdapter 构造函数中放置了一个调试器并检查,我得到的列表大小为 16,但没有调用 getView() 方法。

这是我的 ArrayAdapter 代码。

public class Adapter extends ArrayAdapter<Item> {

    private List<Item> list;
    private Context context;
    private LayoutInflater inflater;

    public Adapter(Context context, int resource, List<Item> list) {
        super(context, resource, list);
        this.list = list;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.section_list, null);
            holder = new ViewHolder();

            holder.text = convertView.findViewById(R.id.text);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final Item item = getItem(position);

        holder.text.setText(item.getTitle());



        return convertView;
    }

    @Override
    public int getCount() {
        if (this.list != null && this.list.size() > 0) {
            return this.list.size();
        } else {
            return 0;
        }
    }

    private class ViewHolder {
        private TextView text;
    }
}

我如何调用适配器:

 adapter = new Adapter(getContext(), R.layout.section_list, listItem);
 notificationListView.setAdapter(adapter);

这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:text="MAXOFEN is PEACHES"
        android:layout_margin="10dp"
        android:textSize="14sp"
        android:layout_height="wrap_content" />

</LinearLayout>

这是我在片段中的列表项:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <LinearLayout

       android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <LinearLayout

           android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/shadow"
            android:orientation="vertical"
            android:visibility="visible">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="12dp">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:text="@string/notifications"
                    android:textColor="@color/black"
                    android:textSize="18sp" />


                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"
                    android:background="@color/colorAccent"
                    android:padding="8dp"
                    android:text="@string/see_all"
                    android:textColor="@color/white" />

            </RelativeLayout>

            <View
                android:layout_width="fill_parent"
                android:layout_height="1dp"
                android:background="@color/hrLine" />

            <LinearLayout
                android:id="@+id/userNameLl"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="12dp">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/danger_alert_copy" />

                <TextView
                    android:id="@+id/userName"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="15dp"
                    android:text="harsh" />


            </LinearLayout>

            <com.knowyourmeds.utils.ExpandableHeightListView
                android:id="@+id/list"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:divider="@null"
                android:dividerHeight="0dp" />
        </LinearLayout>
 </LinearLayout>

我是如何生成列表的:

Item item;
List<Item> listItem = new ArrayList<>();
item = new Item(1l,null,"body","title");
listItem.add(item);

item = new Item(2l,null,"body 1","title 1");
listItem.add(item);

item = new Item(3l,null,"body 2","title 2");
listItem.add(item);

item = new Item(5l,null,"body 3","title 3");
listItem.add(item);

如果我遗漏了什么,请告诉我。

【问题讨论】:

  • 发布您的布局!
  • 检查你的数组列表大小,如果应该至少有一个元素
  • @MeosCoder 请检查更新的问题
  • @ChetanJoshi 是的,我放了一个调试器,检查的列表大小为 16。
  • 什么是notificationText?在 ViewHolder 中是 text

标签: android adapter android-arrayadapter


【解决方案1】:

您可以尝试覆盖基类中的方法getItem()

将此添加到您的代码中,

@Override
    public Item getItem(int position) {
        return list.get(position);
    }

【讨论】:

    【解决方案2】:

    检查布局中 Listview 的可见性。如果它被隐藏,getView() 方法将不会被调用。布局中的其他视图可能会隐藏列表视图。

    尝试移除其他视图,只保留 ListView 客栈布局。

    【讨论】:

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