以编程方式访问作为列表视图布局一部分的imageview / textview

时间:2014-06-16 08:20:34

标签: android listview textview imageview expandablelistview

我想在按下按钮后将imageview的可见性更改为可见。我正在使用可扩展的列表视图,每次我尝试更改imageview的可见性时,我都会收到空指针异常。我正在使用findViewById找到imageview并且所有内容都在编译,但是当我尝试实际更改imageview的可见性时,我崩溃了。是否可以以编程方式更改作为列表视图布局一部分的textviews / imageview?如果是这样,你能解释一下吗?我也从SQL数据库获取数据并使用SimpleCursorTreeAdapter。

谢谢Nickolai Astashonok! 我能够使用以下代码来更改作为列表布局

一部分的imageview的可见性
 SimpleCursorTreeAdapter adapter = new SimpleCursorTreeAdapter(this,
                            cursor, R.layout.list_parent, fromParent, toParent,
                            R.layout.list_child, fromChild, toChild) {

        //method overridden to access the imageview
                        @Override
                        protected void bindGroupView (View view, Context context, Cursor cursor, boolean isExpanded) {
                            super.bindGroupView(view, context, cursor, isExpanded);

                       //don't forget to access via the view that was passed in
                            ImageView editIcon= (ImageView) view.findViewById(R.id.ivEdit);
                            editIcon.setVisibility(View.VISIBLE);
                        }

                        @Override
                        protected Cursor getChildrenCursor(Cursor groupCursor) {
                            Long rowId = Long.parseLong(groupCursor.getString(0));

                            startManagingCursor(groupCursor);
                            groupCursor = dbAdapter.getRow(rowId);
                            // grabs the row of data for the appropriate child
                            return groupCursor;
                        }
                    };

2 个答案:

答案 0 :(得分:0)

如果ImageView是ListView的一部分,请先声明ListView:

ListView list = (ListView) findViewById(R.id.your_listview);

然后根据它声明ImageView

ImageView image = (ImageView) list.findViewById(R.id.your_imageview);

或者如果它包含在ListView的布局中,声明该布局并根据它声明ImageView。

答案 1 :(得分:0)

you can do this in adapter
On getview() method do the changes  :  


  public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
    Button btn = (Button) rowView.findViewById(R.id.button1);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
    textView.setText(values[position]);
      imageView.setImageResource(R.drawable.no);
     btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                imageView.setVisibility(View.INVISIBLE);
            }
        });
    }