[英]How to change an ImageView in a row of ListView?
我有一個使用SimpleCursorAdapter填充的ListView。 行布局如下:
<?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>
我要做的是在創建活動時基於某些共享的首選項將imageview的源更改為其他內容。 這是我使用的代碼,但不起作用:
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);
}
這是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;
}
我能夠讀取listview的內容,但無法更改它們。
您需要為此列表視圖重寫getView方法。 檢查自定義Android ListView項目
您需要做這樣的事情。
@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.