在我的应用程序中,我只使用列表视图来显示图像和文本.这是我的适配器编码
ArrayAdapter<String> adapter=new ArrayAdapter<String>(MainActivity.this, R.layout.List_item, R.ID.name, arrList); ListProductCategorIEs.setAdapter(adapter); ListProductCategorIEs.setonItemClickListener(new OnItemClickListener() { @OverrIDe public voID onItemClick(AdapterVIEw<?> parent, VIEw vIEw, int position, long ID) { ImageVIEw imgvIEw = (ImageVIEw) vIEw.findVIEwByID(R.ID.iv_Listitem); //And change its background here imgvIEw.setBackgroundResource(R.drawable.fail); String name = ((TextVIEw) vIEw.findVIEwByID(R.ID.name)) .getText().toString(); Toast.makeText(MainActivity.this, "" + name, 10).show(); } });
这是上面编码的屏幕截图.
这里我突出显示所选列表项.但是我的输出显示了许多列表视图项目图像背景中的一些已更改..
请给我一个解决方案….
<ImageVIEw androID:ID="@+ID/iv_Listitem" androID:layout_wIDth="20dp" androID:layout_height="20dp" androID:layout_gravity="center_vertical" androID:background="@drawable/next_unselect"androID:focusable="false" /> <TextVIEwandroID:ID="@+ID/name" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:layout_gravity="center_vertical"androID:focusable="false" androID:text="TextVIEw"androID:textcolor="#003366" />
项目清单-
<ImageVIEw androID:ID="@+ID/iv_Listitem" androID:layout_wIDth="20dp" androID:layout_height="20dp" androID:layout_gravity="center_vertical" androID:background="@drawable/next_unselect" androID:focusable="false" /> <TextVIEw androID:ID="@+ID/name" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:layout_gravity="center_vertical" androID:focusable="false" androID:text="TextVIEw" androID:textcolor="#003366" />
解决方法:
我使用了自定义适配器的Custom ListVIEw和带有getter和setter的ItemsHolder类.
改变
>获取项目的位置.
>更改位置的项目.
>适配器上的CallnotifyDataSetChanged()以刷新ListvIEw.
例:
ItemsHolder ih = hold.get(position);ih.setimage(decodeImage(R.drawable.appicon));ih.setname("Changed");cus.notifyDataSetChanged();
的test.xml
<relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"xmlns:tools="http://schemas.androID.com/tools"androID:layout_wIDth="match_parent"androID:layout_height="match_parent"tools:context=".MenuActivity" ><ListVIEw androID:ID="@+ID/ListVIEw_Menu" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" ></ListVIEw></relativeLayout>
List_item.xml
<?xml version="1.0" enCoding="utf-8"?><relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"androID:layout_wIDth="match_parent"androID:layout_height="match_parent" ><ImageVIEw androID:ID="@+ID/imageVIEw_List_Item" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_centerHorizontal="true" androID:contentDescription="@string/app_name" androID:src="@drawable/ic_launcher" /><TextVIEw androID:ID="@+ID/textVIEw1" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_below="@+ID/imageVIEw_List_Item" androID:layout_centerHorizontal="true" androID:layout_margintop="28dp" androID:text="TextVIEw" /></relativeLayout>
MainActivity.java
public class MainActivity extends Activity {ArrayList<ItemsHolder> hold= new ArrayList<ItemsHolder>();CustomAdapter cus;@OverrIDeprotected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.test); Bitmap[] images = {decodeImage(R.drawable.ic_launcher),decodeImage(R.drawable.ic_launcher)}; ListVIEw List = (ListVIEw)findVIEwByID(R.ID.ListVIEw_Menu); hold.add(new ItemsHolder(images[0],"image1")); hold.add(new ItemsHolder(images[1],"image2")); cus = new CustomAdapter(hold); List.setAdapter(cus); List.setonItemClickListener(new OnItemClickListener() { public voID onItemClick(AdapterVIEw<?> parent, VIEw vIEw, int position, long ID) { ItemsHolder ih = hold.get(position); ih.setimage(decodeImage(R.drawable.appicon)); ih.setname("Changed"); cus.notifyDataSetChanged(); } });}private Bitmap decodeImage(int res) { Bitmap bitmap = BitmapFactory.decodeResource(getResources(),res); return bitmap; }class ItemsHolder{ Bitmap image; String name; public ItemsHolder(Bitmap bitmap, String string) { // Todo auto-generated constructor stub image = bitmap; name =string; } public Bitmap getimage() { return image; } public voID setimage(Bitmap image) { this.image = image; } public String getname() { return name; } public voID setname(String name) { this.name = name; }}class CustomAdapter extends BaseAdapter{ LayoutInflater inflater; ArrayList<ItemsHolder> List; public CustomAdapter(ArrayList<ItemsHolder> List) { this.List=List; inflater= LayoutInflater.from(MainActivity.this); } @OverrIDe public int getCount() { // Todo auto-generated method stub return List.size(); } @OverrIDe public Object getItem(int arg0) { // Todo auto-generated method stub return arg0; } @OverrIDe public long getItemID(int position) { // Todo auto-generated method stub return 0; } @OverrIDe public VIEw getVIEw(int position, VIEw convertVIEw, VIEwGroup parent) { // Todo auto-generated method stub VIEwHolder holder; if(convertVIEw==null) { holder = new VIEwHolder(); convertVIEw = inflater.inflate(R.layout.List_item, null); holder.iv= (ImageVIEw) convertVIEw.findVIEwByID(R.ID.imageVIEw_List_Item); holder.tv = (TextVIEw) convertVIEw.findVIEwByID(R.ID.textVIEw1); convertVIEw.setTag(holder); } else { holder = (VIEwHolder)convertVIEw.getTag(); } ItemsHolder ih = List.get(position); holder.iv.setimageBitmap(ih.getimage()); holder.tv.setText(ih.getname()); return convertVIEw; }}class VIEwHolder{ ImageVIEw iv; TextVIEw tv;}}
捕捉
编辑:
在评论中提出您的问题
Listbkg.xml
<?xml version="1.0" enCoding="utf-8"?><selector xmlns:androID="http://schemas.androID.com/apk/res/androID"> <item androID:state_pressed="true" androID:drawable="@drawable/appicon" /> <item androID:state_focused="false" androID:drawable="@drawable/ic_launcher" /></selector>
然后是xml中的ImageVIEw
androID:background="@drawable/Listbkg"
总结 以上是内存溢出为你收集整理的在android中更改所选列表视图项图像问题全部内容,希望文章能够帮你解决在android中更改所选列表视图项图像问题所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)