简体   繁体   English

如何获取复杂的ListView行视图中的视图?

[英]How to get a view which is inside a complex ListView row view?

I have a ListView which has been populated by rows containing data using an adapter. 我有一个ListView,它已使用适配器包含数据的行填充。 The row layout is of "RelativeLayout" type and contains various view objects, the id of one of them is "@+id/bar_fill". 行布局为“RelativeLayout”类型,包含各种视图对象,其中一个的id为“@ + id / bar_fill”。

I have a loop running over all the childs of the ListView, and I want to "get" the view item i mentioned above. 我有一个循环遍历ListView的所有子项,我想“获取”我上面提到的视图项。

The code I'm using is: 我正在使用的代码是:

ListView list = getListView();
int count = list.getCount();
for (int i = 0; i < count; i++) 
{
   RelativeLayout row = (RelativeLayout) list.getChildAt(i);
   View bar = row.findViewById(R.id.bar_fill);         
}

However when it reaches the last line it crashes. 但是,当到达最后一行时,它将崩溃。 When I commented it out and did 当我评论出来并做了

System.out.println(findViewById(R.id.bar_fill));

It returned nulls. 它返回了null。

What am I doing wrong? 我究竟做错了什么?

overide this function in custom adapter. 在自定义适配器中覆盖此功能。

public View getView(int position, View convertView, ViewGroup parent);
View v = convertView;
    if (v == null) {
        LayoutInflater vi = (LayoutInflater) c
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(id, null);
    }

Now like if you want certain textview from the listview row item then do it like this: 现在就像如果您要从listview行项目中获取特定的textview一样,请按照以下步骤进行操作:

TextView t1 = (TextView) v.findViewById(com.pis.prototype.R.id.TextView01);

t1 is your required view now...:P t1是您现在需要的视图......:P

And if you want to get the already updated/created items in the list view and want to get some view from some row. 如果您想在列表视图中获取已更新/创建的项目,并希望从某行获取某些视图。 Then do it like this..: 那就这样做..:

    for (int i = 0; i < yourCustomAdapter.size(); i++) {

        View viewRow = listViewParts.getChildAt(i);

        CheckBox chkBox = (CheckBox) viewRow
                .findViewById(R.id.checkBox);
        yourCustomAdapter.get(i).attachmentCheck = chkBox
                .isChecked();
    }

In the above case I have extracted the checkbox views from the listview. 在上面的例子中,我从listview中提取了复选框视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM