I have a ListView specified by list_item.xml Now I need to change the Image in my list inside onListItemClick. How to achieve this?
//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>
I have a Custom Adapter to populate my list. Code below is the adapter.
public class MyCustomAdapter extends ArrayAdapter<String> {
public MyCustomAdapter(Context context, int textViewResourceId,
String[] objects) {
super(context, textViewResourceId, objects);
// TODO Auto-generated constructor stub
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater=getLayoutInflater();
View row=inflater.inflate(R.layout.list_item, parent, false);
TextView label=(TextView)row.findViewById(R.id.txt);
label.setText(Sounds[position]);
ImageView icon=(ImageView)row.findViewById(R.id.img);
icon.setMaxHeight(32);
icon.setMaxWidth(32);
icon.setPadding(2, 1, 5, 1);
icon.setImageResource(R.drawable.play);
return row;
}
}
And in onCreate I do the following
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try
{
setListAdapter(new MyCustomAdapter(this, R.layout.list_item, Sounds));
//Sounds --> String array
}
catch(Exception e)
{
e.printStackTrace();
}
}
Now when any row is selected, I need to change the image associated with the selected view. Your help is appreciated. Thanks.
edit: you can implement an onitemclicklistener and then react to itemclicks. use this method to change the source of the bitmap then.
edit 2: it works now with the above method. here is the code.
as you can see, R.id.musicicon is always the same id of the imageview for every row in the list. but clickedview gives you the exact row where the click happened and in THIS row, there is only one imageview child with the given id.