2013-10-19 48 views
3

在我的应用程序中,我只是使用列表视图来显示图像和文本。这是我的适配器编码更改在android中的选定列表查看项目图像问题

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(); 

     } 
    }); 

这是上述编码的屏幕截图。

enter image description here

我在这里强调了选择列表items.But我的输出显示一些许多列表视图项图像背景的变化..

请给我一个解决方案....

<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" /> 

列表用品 -

<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" /> 
+0

你可以发布'R.layout.list_item' – Raghunandan

+0

在此先感谢。 –

+0

这是LIST_ITEM:

回答

6

我已经使用了自定义的ListView使用自定义适配器和ItemsHolder与吸气和吸气器类。

要更改

  1. 获得该项目的位置。
  2. 更改位置处的项目。
  3. 在您的适配器上调用notifyDataSetChanged()以刷新列表视图。

实施例:

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; 
@Override 
protected 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; 
} 
} 

捕捉

enter image description here

enter image description here

编辑:

要在评论你的问题

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> 

然后对于ImageView中的xml

android:background="@drawable/listbkg" 
+0

是的编码工作正常..谢谢很多..但我想为另一个问题的解决方案。图像将更改时,用户用户选择任何列表项目。但这里的问题是,当用户单击另一个列表项目的前一个选择的图像显示更改图标.. –

+0

如何避免这些问题?当时只有一个选定的列表项目图像将被更改。 –

+0

@Android_kalai我不明白你的问题 – Raghunandan

0

我可以猜出什么可以解决你的问题。但首先,我强烈建议你看从Android的ListView控件的API的首席架构师这个伟大的视频演示(如果你还没有):

http://www.youtube.com/watch?v=wDBM6wVEO70

+0

Yes..But我想针对我的问题的一些更快的解决方案 –

相关问题