I have two activities. In first I put a list of items with a ListView. In the second, I want to put a list of items too, but the contents of which will depend on the position of the element, which was pressed in the first Activitу. Here is the code of the first ListView Activity:
public class ListView1 extends Activity implements OnClickListener {
private ListView lv1;
private String cats[];
//some code
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
Bundle bundle = getIntent().getExtras();
final String category = bundle.getString("category");
lv1 = (ListView) findViewById(R.id.listView);
ArrayAdapter<CharSequence> adapter = ArrayAdapter
.createFromResource(this, R.array.first_list,
R.layout.list_items);
lv1.setAdapter(adapter);
cats = getResources().getStringArray(R.array.first_list);
lv1.setTextFilterEnabled(true);
lv1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position,
long id) {
String defStrID = new Integer(position).toString();
Intent intent = new Intent();
intent.setClass(ListView1.this, ListView2.class);
Bundle b = new Bundle();
b.putString("defStrID", defStrID);
intent.putExtras(b);
startActivity(intent);
}
});
}
}
This is the code which gets position of element in the list:
String defStrID = new Integer(position).toString();
I get it in the second ListView with:
Bundle bundle = getIntent().getExtras();
String elementID = bundle.getString("defStrID");
and I have some arrays in arrays.xml for the second ListView. For example:
R.array.category1
R.array.category2
R.array.category3
R.array.category4
or arrays like this (it’s not very important):
String category1[] = { //some items here }
String category2[] = { //some items here }
etc.
As an example, only a few arrays, actually more, so I do not want to use the if else construction. How to do it another way, I do not know. The second ListView activity will be the same as the first one, except for the described above.
Second activity:
public class List2 extends ListActivity {
private ListView lv1;
private String category1[] = {"Name1", "Name2"};
private String category2[] = {"Surname1", "Surname2"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getIntent().getExtras();
;
String elementID = bundle.getString("defStrID");
String arrayName = "category" + elementID;
int id = getResources().getIdentifier(arrayName, "array",
this.getPackageName());
Log.v("LOOK_FOR_ME", "category" + " and the id i got is " + id);
String[] stats = getResources().getStringArray(id);
lv1.setAdapter(new ArrayAdapter<String>(this, R.layout.list_items,
stats));
}
}
In your second
Activityget the id from the other activity:If your arrays have the name
categoryX(x -number) then build a String with the array name:Then you can get the id of that particular array like this:
and finally get the
Stringarray and put it in the adapter:Also keep in mind that the position in the list start at
0so name your array starting with0;