Reputation: 328
In my app I have a dialog (dialog1), containing Listview with an ArrayAdapter having 3 string items. I want to set onItemclickListener() on this list,through which I would be able to start different activities on different item click. Please Help.
Upvotes: 1
Views: 6368
Reputation: 1221
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this,R.layout.new_service_request,LIST));
ListView lv=getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) v).getText(),
Toast.LENGTH_SHORT).show();
// int ITEM_CLICKED = (int)getSelectedItemId();
switch(position){
case 0:
Intent intent1 = new Intent(New.this, Next.class);
startActivity(intent1);
break;
case 1:
Intent intent2 = new Intent(New.this, List.class);
startActivity(intent2);
break;
case 2:
Intent intent3= new Intent(New.this, HotCard.class);
startActivity(intent3);
break;
Upvotes: 3
Reputation: 4958
I would recommend having a go at reproducing the example from http://www.vogella.de/articles/AndroidListView/article.html and then trying to adapt it to suit your purposes (i.e. in your dialog). I usually find it easier to get a grip on the problem in a simple use case before trying to shoe-horn it into my code. So in your ListActivity
, you would call
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, yourThreeStringArray));
Then instead of the call to Toast
in the example, simply start your activity something like this:
Intent myIntent = new Intent(this, MyIntent.class);
startActivityForResult(myIntent, ACTIVITY_CREATE);
(Replacing MyIntent
with the class of your intended action, of course.)
Good luck!
Upvotes: 0
Reputation: 216
Not sure if this will work but, you could try within the dialog:
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//do stuff here
}
});
Upvotes: 1
Reputation: 6277
See if this helps
dialog1.setItems(array_of_items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//which is the item number in the list which you can use
//to do things accordingly
}
});
Upvotes: 3