在android中更改ListView的Imageview onItemClickListener的图像

时间:2014-02-20 12:36:20

标签: android listview

我使用自定义ListView来显示图片和文字,并使用Cursor。现在,onItemClickListener(),我想要更改ImageView的图像,我还想更改TextView的textColor。我想要点击哪个用户Click禁用该项目。我尝试过几件事,但都没有 这该怎么做 ?

XML代码:

        <ImageView
            android:id="@+id/icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/queimg"
            android:src="@drawable/ic_answer" />

        <TextView
            android:id="@+id/option"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingTop="14dp"
            android:layout_toRightOf="@+id/icon"
            android:text="hello" />

Java代码:

        final List<String> lstOption = new ArrayList<String>();

        ArrayList<Map<String, String>> arrlist = new ArrayList<Map<String, String>>();

        Map<String, String> m = null;
        if (option != null) {
            if (option.moveToFirst()) {
                do {
                    lstOption.add(option.getString(option
                            .getColumnIndex("Option")));

                    m = new HashMap<String, String>();
                    m.put("option",
                            option.getString(option.getColumnIndex("Option")));

                    arrlist.add(m);
                } while (option.moveToNext());
            }
        }

        SimpleAdapter adapter = new SimpleAdapter(this, arrlist,
                R.layout.topicwisequestion, new String[] { "option" },
                new int[] { R.id.option });


lvTWOptions.setAdapter(adapter);
lvTWOptions.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
}
});

2 个答案:

答案 0 :(得分:1)

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    selection[position] = true;
    ((ImageView)view.findViewById(R.id.icon)).setImageResource(R.drawable.yourImageResourceId);
    ((TextView)view.findViewById(R.id.option)).setTextColor(Color.BLUE);
}

试试这段代码,希望它会有所帮助。

final boolean[] selection = new boolean[arrlist.length];
SimpleAdapter adapter = new SimpleAdapter(this, arrlist, R.layout.topicwisequestion, new String[] { "option" },new int[] { R.id.option }){
    @Override
    public boolean isEnabled(int position) {
        return !(selection[position]);
    }
}

答案 1 :(得分:0)

实现这一目标的最简单方法是扩展BaseAdaptor类并使用列表中的自定义项。