Mocktheduck Mocktheduck - 20 days ago 5
Android Question

OnClickItem with unknown number of items Android

I have an array of objects. In my listview, I pass only the name of those objects but when someone clicks on any of them, I want a new window to pop up and to see the extra information from my items. Can I do that somehow?

This is how my list activity looks like:

public class ListItemsActivity extends ListActivity {
String[] mTestArray;
ListView listView;
private static final String TAG = "ListActivity";

@Override
public void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "Trying to create the list activitye");
super.onCreate(savedInstanceState);
ArrayAdapter<String> adapter;

mTestArray = getResources().getStringArray(R.array.sections);
ArrayList<Sweet> sweets = getSweets(mTestArray);
ArrayList<String> result = getSweetsNames(mTestArray);
Log.d(TAG, mTestArray.toString());

adapter = new ArrayAdapter<String>(
this,
R.layout.activity_list_items,
result;
setListAdapter(adapter);
}

public void onListItemClick(ListView parent, View v, int position, long id) {
parent.setItemChecked(position, parent.isItemChecked(position));

Toast.makeText(this, "You have selected " + mTestArray[position],
Toast.LENGTH_SHORT).show();
}


So this is ok, it shows me a lsit of names. And when I click on them it jsut tells me on a small popup thing that I've selected it. What I want is actually to open a new window and show all the information from my items. Is that possible? How would I go around to do it?

The only way I found is to do something like this:

public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
switch( position ) {
case 0: Intent newActivity = new Intent(this, i1.class);
startActivity(newActivity);
break;
case 1: Intent newActivity = new Intent(this, i2.class);
startActivity(newActivity);
break;
case 2: Intent newActivity = new Intent(this, i3.class);
startActivity(newActivity);
break;
case 3: Intent newActivity = new Intent(this, i4.class);
startActivity(newActivity);
break;
case 4: Intent newActivity = new Intent(this, i5.class);
startActivity(newActivity);
break;
}
}


But it's a bad approach for these reasons:
1) I have an unknown number of elements
2)I dont have 1000 activities for each item, I want 1 general window that would depend on some integer position.

Can I do it this way?

Answer

The easiest way of passing data between activities is using intents.

public void onItemClick(AdapterView<?> parent, View view,
          int position, long id) {

     Intent newActivity = new Intent(this, i1.class);
     newActivity.putExtra("id", postion);
     newActivity.putExtra("key", value);        
     startActivity(newActivity);

    }

In short, putExtra method takes a key and value which can be retrieved in the destination Activity.

Bundle extras = getIntent().getExtras();
String id,key;
    if(extras == null) {
        id = null;
        key = null; 
    } else {
        id= extras.getString("id");
        key= extras.getString("key");
    }