我正在使用自定义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) {
}
});

最佳答案

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]);
    }
}

08-03 14:11
查看更多