访问listview中的imagebutton并设置其setImageResource

时间:2014-06-16 12:30:34

标签: android android-listview imagebutton

我有一个包含图像按钮的列表视图,点击按钮我想要更改其图像资源,但我无法这样做。我附上了清单的截图

enter image description here

我想更改图像按钮图像,如果它处于连接状态则断开连接,反之亦然。

3 个答案:

答案 0 :(得分:1)

首先检查给定list_item.xml以查找要在listview中设置的自定义列表项

<?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:gravity="center"
    android:orientation="horizontal" >

    <ImageButton
        android:id="@+id/ibShowStatus"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/lblStatus"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="5"
        android:text="TextView" />

    <Button
        android:id="@+id/btnChangeStatus"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:text="Conntect" />

</LinearLayout>

现在创建自定义数据类以显示使用已创建列表项的适配数据,该列表项保持列表项的状态

    private class CustomeListData {
        boolean isConnected;

        public CustomeListData(boolean isConnected) {
            super();
            this.isConnected = isConnected;
        }

        public boolean isConnected() {
            return isConnected;
        }

        public void setConnected(boolean isConnected) {
            this.isConnected = isConnected;
        }
    }

现在创建要使用listview

调整的自定义类型的数组列表
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ArrayList<CustomeListData> listData = new ArrayList<CustomeListData>();

        listData.add(new CustomeListData(true));
        listData.add(new CustomeListData(false));
        listData.add(new CustomeListData(true));
        listData.add(new CustomeListData(true));
        listData.add(new CustomeListData(false));

        lv = (ListView) findViewById(R.id.listView1);
        CustomeAdapter adapter = new CustomeAdapter(this, R.layout.list_item,
                listData);
        lv.setAdapter(adapter);

    }

现在创建一个ViewHodler类,它控制list_item.xml,这个视图

    private class ViewHolder {
    ImageButton ibShowStatus;
    TextView lblStatus;
    Button btnChangeStatus;
    }

创建该自定义类的ArrayAdapter并检查imageButton的更改图像的代码 当您更改按钮的状态时单击

    private class CustomeAdapter extends ArrayAdapter<CustomeListData> {
        Context mContext;
        int layoutId;
        ArrayList<CustomeListData> tempList;
        ViewHolder vh;

        public CustomeAdapter(Context context, int resource,
                ArrayList<CustomeListData> listData) {
            super(context, resource, listData);

            mContext = context;
            layoutId = resource;
            tempList = listData;
        }

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

            if (convertView == null) {
                // initialize viewhlder
                vh = new ViewHolder();

                // inflate list_item.xml in convertview
                LayoutInflater inflater = 
                        (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(layoutId, parent);

                // hold list_item.xml control in viewholder
                vh.ibShowStatus = (ImageButton) convertView
                        .findViewById(R.id.ibShowStatus);
                vh.lblStatus = (TextView) convertView
                        .findViewById(R.id.lblStatus);
                vh.btnChangeStatus = (Button) convertView
                        .findViewById(R.id.btnChangeStatus);

                // set tag vh to the convertview
                            convertView.setTag(vh);
            } else {
                vh = (ViewHolder) convertView.getTag();
            }

            // get item from dataList at particular postition, so you can access
            // member of that item
            final CustomeListData item = tempList.get(position);

            if (item.isConnected()) {
                vh.ibShowStatus.setImageResource(R.drawable.connected);
                vh.lblStatus.setText("Connected");
                vh.btnChangeStatus.setText("Disconnect");
            } else {
                vh.ibShowStatus.setImageResource(R.drawable.disconnected);
                vh.lblStatus.setText("Not Used");
                vh.btnChangeStatus.setText("Connect");
            }

            vh.btnChangeStatus.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    final CustomeListData item = tempList.get(position);

                    if (item.isConnected()) {
                        vh.ibShowStatus
                        .setImageResource(R.drawable.disconnected);
                        vh.lblStatus.setText("Not Used");
                        vh.btnChangeStatus.setText("Connect");
                    } else {
                        vh.ibShowStatus
                                                .setImageResource(R.drawable.connected);
                        vh.lblStatus.setText("Connected");
                        vh.btnChangeStatus.setText("Disconnect");
                    }

                    item.setConnected(!item.isConnected);
                }
            });

            return convertView;
        }
        }

Now try this code, it work well.

答案 1 :(得分:0)

取一个布尔数组并相应地设置值,如果为true或false,则单击列表项检查该位置的值是否为true,反之亦然。然后通过 adapter.notifyDataSetChange();

通知适配器数据集已更改

答案 2 :(得分:0)

尝试这种方式..在get视图方法中的自定义适配器声明视图持有者并管理列表的整个布局自定义布局的点击....并在那里更改图像资源

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

            ViewHolder holder = null;

            LayoutInflater mInflater = (LayoutInflater) context
                    .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.emotion_holder, null);
                holder = new ViewHolder();
                holder.li = (LinearLayout) convertView.findViewById(R.id.li);
                holder.title = (TextView) convertView.findViewById(R.id.emo_name);
                holder.pic = (ImageView) convertView.findViewById(R.id.lock);
                holder.desc = (TextView) convertView
                        .findViewById(R.id.tim);

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



            try {




                holder.title.setText(rowItems.get(position).getE_name());
                //holder.desc.setText(rowItems.get(position).getDesc());

                // holder.rl.setTag(position);
                holder.li.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {

                    if(//check your condition here)
                {
                                     //you please put you image accordingly to the condition
                    holder.pic.setImageResource(R.drawable.lockkey);
                }
                else
                {
                                    //you please put you image accordingly to the condition
                    holder.pic.setImageResource(R.drawable.unlockkey);

                }

                    }
                });
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return convertView;
        }