【问题标题】:Android ListView with a Custom Adapter - getChildAt() returns null带有自定义适配器的 Android ListView - getChildAt() 返回 null
【发布时间】:2012-01-04 06:37:27
【问题描述】:

我有一个带有自定义适配器的 ListView,我需要访问此 ListView 的各个行的视图。我正在尝试使用 getChildAt() 来执行此操作,但它返回 null。

这是我的列表视图:

myListView = (ListView) findViewById (R.id.listViewLayout);

这是自定义适配器:

    public class MyListAdapter extends BaseAdapter {
       ...
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.list_row_layout, parent, false);
            }
            ...
    }

我创建了一个列表,并将其设置为 ListView:

List <String> myList = new ArrayList<String>();
myList.add("entry 1");
myList.add("entry 2");
myList.add("entry 3");
MyListAdapter myListAdapter = new MyListAdapter(getApplicationContext(), myList);
myListView.setAdapter(myListAdapter);

现在,我想访问各个行的视图。

View v = myListView.getChildAt(myListView.getFirstVisiblePosition());

无论如何,这个 v 都是空的。此外,myListView.getChildCount() 始终返回 null。

我错过了什么?我需要在getView() 中将convertView 添加到parent 吗?我是否将错误的参数传递给inflate

我费了很大劲才弄明白。请帮帮我!

编辑:我正在发布 MyListAdapter 类:

public class MyListAdapter extends BaseAdapter {

    private Context context;
    private List<String> myList;

    public MyListAdapter(Context c, List<String> m) {
        context = c;
        myList = m;
    }

    @Override
    public int getCount() {
        return myList.size();
    }

    @Override
    public Object getItem(int position) {
        return myList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        final int pos = position;

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.list_row_layout, parent, false);
        }

            TextView text = (TextView) convertView.findViewById(R.id.itemText);
            text.setText(myList.get(pos));

            // Set the onClick Listener on this button
            Button rowButton = (Button) convertView.findViewById(R.id.itemButton);

            //commenting next two lines makes the list row clickable
            rowButton.setFocusableInTouchMode(false);
            rowButton.setFocusable(false);

            rowButton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    Toast.makeText(context, myList.get(pos), Toast.LENGTH_SHORT).show();
                } 
            });  
            convertView.setTag(pos);
            return convertView;
    }
}

【问题讨论】:

  • 你的 getChildAt 实现是什么样的?
  • 你能发布你的 MyListAdapter 类的其余部分吗?具体来说,构造函数,getView的全身和getCount()的实现?

标签: android android-widget android-listview android-ui


【解决方案1】:

您很可能在onCreate() 中执行所有这些操作。任何与视图相关的方法都将返回 null(或空),因为列表尚未呈现。您需要子视图做什么?

【讨论】:

  • 每一行都有一个TextView和一个Button,我需要更改各个按钮的可见性。
  • 我在 onResume() 中执行此操作。我只在设置适配器后才尝试 getChildAt()...
  • 你的想法有问题。一旦用户滚动您的按钮可见性将恢复为默认值。您必须在适配器的 getView() 方法中设置适当的可见性。
  • 是的,这可能是个问题,但我打算使用 getFirstVisiblePosition() 来解决它...你知道为什么我从 getChildAt() 得到 null 吗? :)
  • 就像我说的,它还没有被渲染(在onResume() 中并不能保证这一点)。而且,每次用户滚动时你都会打电话给getFirstVisiblePosition()吗?你不能那样做,这是
【解决方案2】:

尝试使用 ArrayAdapter 类而不是扩展 BaseAdapter。使用 BaseAdapter,我相信您必须同时维护对象列表和视图本身。所以像这样的

public class MyAdapter extends ArrayAdapter<String>
{ 
...
}

然后你会像这样创建类。

MyListAdapter myListAdapter = new MyListAdapter(getApplicationContext(), R.layout.list_row_layout, myList);

更新 此外,在您的构造函数中,您没有调用基类构造函数。

【讨论】:

  • 恐怕我没有得到它。 ListAdapter 是一个接口,我尝试实现它,没有任何不同的结果。在构造函数中调用 super() 也没有任何效果。我需要尝试 super.getView() 吗?这对适配器不起作用...
  • 哎呀,对不起。我输入了错误的班级名称。它应该是 ArrayAdapter 而不是 ListAdapter。更新了我的解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-08-22
  • 1970-01-01
  • 2014-09-06
  • 2013-05-21
  • 2016-03-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多