I have a listView, it has about 100 items.
Currently I have this code:
public class VanillaList extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.vanilla_block_list);
ArrayList<ItemDetails> image_details = GetSearchResults();
final ListView lv1 = (ListView) findViewById(R.id.listV_main);
lv1.setAdapter(new ItemListBaseAdapter(this, image_details));
lv1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Object o = lv1.getItemAtPosition(position);
ItemDetails obj_itemDetails = (ItemDetails)o;
Toast.makeText(VanillaList.this, "You have clicked on : " + " " + obj_itemDetails.getName(), Toast.LENGTH_LONG).show();
}
});
}
This shows a toast notification saying the name of the list item with “You have clicked on:” in front of it.
This is the case for all 100 items. How would I make a different onClick method for each item. I have tried:
public class VanillaBlockList extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.vanilla_block_list);
ArrayList<ItemDetails> image_details = GetSearchResults();
final ListView lv1 = (ListView) findViewById(R.id.listV_main);
lv1.setAdapter(new ItemListBaseAdapter(this, image_details));
lv1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Object o = lv1.getItemAtPosition(position);
ItemDetails obj_itemDetails = (ItemDetails)o;
Toast.makeText(VanillaBlockList.this, "You have chosen : " + " " + obj_itemDetails.getName(), Toast.LENGTH_LONG).show();
}
lv2.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Object o = lv2.getItemAtPosition(position);
ItemDetails obj_itemDetails = (ItemDetails)o;
Toast.makeText(VanillaBlockList.this, "You have chosen : " + " " + obj_itemDetails.getName(), Toast.LENGTH_LONG).show();
}
});
}
But this hasn’t worked for me. Can anyone help me?
I would prefer an onClick method, like in the layout XML file there is android:onClick="something"
Then in the java file:
public void something{
//method here
}
Thanks.
I hope in the Custom Adapter class, you are using an item layout to show each item in the list. In that Item list, I think you are having a TextView to show the item description. For for getting on click for each item, need to modify the adapter code as below :
Create an interface in custom adapter :
Create a field in Custom Adapter :
From the adapter Constructor, initialize the Listener :
Change/Add the code as below in getView(…) of Adapter
Then in your activity class,
create a new field :
create the adapter instance as :
Now when ever you click on the textview in listitem, your myItemClickListener ‘s onClick will getcalled and you can handle as you need.
if you need the click event for the whole listitem rather than for TextView, then set the onClick of convertView instead of TextView in getView(..) method of Adapter