Android:dynamic更改Listview中的图像

我有一个由以下XML定义的列表视图。 当用户点击任何行时,我需要在运行时切换列表中的图像 。 我怎样才能做到这一点? 任何帮助,高度赞赏。 谢谢

//list_item.xml <?xml version="1.0" encoding="UTF-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/play" android:id="@+id/img" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_vertical" android:id="@+id/txt" /> </LinearLayout> 

Solutions Collecting From Web of "Android:dynamic更改Listview中的图像"

您可以通过在onItemClick处理程序中的第二个参数(这是单击项目的View )上调用findViewById来find该项目的img视图:

 public void onItemClick(AdapterView parentView, View clickedItemView, int pos, long id) { ImageView imageView = (ImageView) clickedItemView.findViewById(R.id.img); // ... } 

编辑:请注意,Android重用列表项View对象(也称为视图回收),所以每个项目的切换状态需要存储供以后使用。 只要列表项视图绑定到列表项目以供显示,就需要访问每个项目的切换状态。

例如,下面是一个在点击时切换每个项目图像的活动的工作示例:

 import java.util.Arrays; import java.util.List; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.ListView; public class SO4539968TestCaseActivity extends Activity { private static final List<String> ITEM_TEXTS = Arrays.asList(new String[] {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13"}); private boolean[] itemToggled; private class MyArrayAdapter<T> extends ArrayAdapter<T> { public MyArrayAdapter(Context context, int resource, int textViewResourceId, List<T> objects) { super(context, resource, textViewResourceId, objects); } @Override public View getView(int position, View convertView, ViewGroup parent) { View itemView = super.getView(position, convertView, parent); ImageView imageView = (ImageView) itemView.findViewById(R.id.img); imageView.setImageResource(itemToggled[position] ? R.drawable.on : R.drawable.off); return itemView; } } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); itemToggled = new boolean[ITEM_TEXTS.size()]; Arrays.fill(itemToggled, false); ListView listView = (ListView) findViewById(R.id.list_view0); listView.setAdapter(new MyArrayAdapter<String>(this, R.layout.list_item, R.id.txt, ITEM_TEXTS)); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView<?> listView, View itemView, int position, long id) { itemToggled[position] = ! itemToggled[position]; ImageView imageView = (ImageView) itemView.findViewById(R.id.img); imageView.setImageResource(itemToggled[position] ? R.drawable.on : R.drawable.off); } }); } } 

研究的重要部分是onItemClickcallback以及在MyArrayAdapter重载getView

Adapter类的getView方法负责膨胀项目布局。 在这个例子中,我调用超类的getView方法来初步准备项目视图,但是我确保适当地设置项目的img视图的资源ID:

 imageView.setImageResource(itemToggled[position] ? R.drawable.on : R.drawable.off); 

另请参阅: 为Android开发应用程序 – 疑难杂症和怪癖

  listview.setOnItemClickListener(new AdapterView.OnItemClickListener(){ public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { ImageView imageView=arg1.findViewById(R.id.img); imageView.setImageResource(R.drawable.new_image); } }); 

我将dynamic生成listview,然后绑定到onclickitem事件,以便像“Kiril Kirilov”那样执行setimageresource。

这可以很容易地完成,你只需要看看用户点击的列表项的位置。 完整的演示代码如下

  public class ChangeImageInListDynamically extends Activity { /** * Adapter for your data. */ ImageAdapter adpAR; ArrayList<String> itemList; ListView mListView; ImageAdapter mAdapter; /** * Indicate which item is currently selected. * Its default selected item is '0' eg, 1st item. */ private int selectedPosition=0; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.main); itemList = new ArrayList<String>(); for (int i = 0; i < 11; i++) { itemList.add("Item - "+i); } mListView = (ListView)findViewById(R.id.data_list); //Initialize your adapter mAdapter = new ImageAdapter(nChangeImageInListDynamically.this, itemList); //set adapter to your list. mListView.setAdapter(mAdapter); //set list click listener. mListView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> av, View view, int position, long id) { //your image that need to change dynamically. ImageView imageView = (ImageView) view.findViewById(R.id.select); //position of clicked item. selectedPosition=position; //change image of respective image. imageView.setImageResource(R.drawable.selected); //notify your adapter to update your list. adpAR.notifyDataSetChanged(); } }); } static class ViewHolder { TextView title; ImageView img; } /** * Your ImageAdapter. */ public class ImageAdapter extends BaseAdapter { ArrayList<String> dataList; Context context; public ImageAdapter(Context context, ArrayList<String> arrayList) { this.context = context; this.dataList = arrayList; } @Override public int getCount() { // TODO Auto-generated method stub return dataList.size();; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return position; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { View row = convertView; final ViewHolder holder; Log.d("posiiton ImageAdapater : ",""+position); if (row == null) { LayoutInflater inflater = ChangeImageInListDynamically.this.getLayoutInflater(); row = inflater.inflate(R.layout.listrow, null); holder = new ViewHolder(); holder.title = (TextView) row.findViewById(R.id.text); holder.img = (ImageView) row.findViewById(R.id.select); row.setTag(holder); } else { holder = (ViewHolder) row.getTag(); } //set titel text. holder.title.setText(dataList.get(position)); //Change Image of selected item dynamically. if(selectedPosition == position) { //Item is selected. holder.img.setImageResource(R.drawable.selected); }else{ //Other non-selected items. holder.img.setImageResource(R.drawable.dis_selected); } return row; } } } 

我希望这将有助于所有:)

使用

 public void setImageResource (int resId); 

ImageView类的方法。

PS我以前的答案似乎不完整,这里是整个解决scheme:

 ImageView imageView = (ImageView) this.findViewById(R.id.img); ((BitmapDrawable)imageView.getDrawable()).getBitmap().recycle(); imageView.setImageResource(R.drawable.new_image); imageView.invalidate();