[英]How to change an ImageView in a row of ListView?
I have a ListView which is populated using a SimpleCursorAdapter. 我有一个使用SimpleCursorAdapter填充的ListView。 The row layout is given below:
行布局如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/row"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp" />
<ImageView
android:id="@+id/enable_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:padding="10dp"
android:contentDescription="@string/image_description"
android:src="@android:drawable/checkbox_off_background" />
</RelativeLayout>
What i want to do is to change the source of the imageview to something else based on some shared preference when the activity is created. 我要做的是在创建活动时基于某些共享的首选项将imageview的源更改为其他内容。 This is the code that i use but it doesn't work:
这是我使用的代码,但不起作用:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
long appliedProfileId = prefs.getLong("appliedProfileId", -1);
if(appliedProfileId != -1) {
/*View rowView = (View) getListView().getAdapter().getView(info.psition, null, null);
TextView textView = (TextView) rowView.findViewById(R.id.row);*/
ListView listView = getListView();
ListAdapter adapter = listView.getAdapter();
View appliedRowLayout = (View) adapter.getView(getItemPositionByAdapterId(adapter, appliedProfileId), null, null);
TextView textView = (TextView) appliedRowLayout.findViewById(R.id.row);
Log.d("asdasd", textView.getText().toString());
ImageView imageView = (ImageView) appliedRowLayout.findViewById(R.id.enable_image);
imageView.setImageResource(android.R.drawable.checkbox_on_background);
setListAdapter(adapter);
}
This is the getItemPositionByAdapterId method: 这是getItemPositionByAdapterId方法:
private int getItemPositionByAdapterId(ListAdapter adapter, final long id)
{
for (int i = 0; i < adapter.getCount(); i++)
{
if (adapter.getItemId(i) == id)
return i;
}
return -1;
}
I am able to read the contents of the listview but cannot change them.. Pleas help :) 我能够读取listview的内容,但无法更改它们。
You need to override the getView method for this listview. 您需要为此列表视图重写getView方法。 Check Customizing Android ListView Items
检查自定义Android ListView项目
You need to do something like this. 您需要做这样的事情。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (position == 0) // first element in the list
{
convertView.findViewById(R.id.image1).setImageURI(....)
}
and so on....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.