Reputation: 105
Ok, I'm very new to Android and Programming in general. I followed the tutorial on the android developers page for creating a list view. Now I actually want to do something with it. I have it working for going to one view, but I want two items in the list each going to a different Activity. Here's the code I have already.
package com.pais.convert;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class list extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] choose = getResources().getStringArray(R.array.list_chooser);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, choose));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Intent intent = new Intent(this, tempConvert.class);
intent.putExtra("KEY_SELECTED_INDEX", position);
startActivity(intent);
}
}
I have it going to one different activity but how can I have two separate items in the listView each going to different activities. But still launching from the same list?
Thanks For All The Help.
Upvotes: 0
Views: 1080
Reputation: 234795
For a ListActivity, the easiest thing to do is to override onListItemClick
in the activity itself.
EDIT:
Here's what I mean:
public class test extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] choose = getResources().getStringArray(R.array.list_chooser);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, choose));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// Start another activity to do something with the selected item.
// I'll assume the other activity is defined in the class
// AnotherActivity:
Intent intent = new Intent(this, AnotherActivity.class);
// now you can add additional information to the intent for the
// other activity to use. For instance, to pass just the index of the
// selected item, you could code:
intent.putExtra("KEY_SELECTED_INDEX", position);
// (The string "KEY_SELECTED_INDEX" is an arbitrary string you choose
// to name this piece of data. AnotherActivity will use the same name
// to retrieve it. Other extras would be added under different names.)
startActivity(intent);
}
}
Then you would have to define a separate activity to display the second view:
public class AnotherActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
int position = intent.getIntExtra("KEY_SELECTED_INDEX", -1);
if (position == -1) {
Toast.makeText(this, "No selection to show!", Toast.DURATION_LONG)
.show();
}
// continue with setting up the activity
}
}
You also would need to add this second activity to your app's manifest file.
Upvotes: 1