How to write the code that can pass data from one lisview to another listview like if i select BMW from the car brand list i will be able to choose a particular car series that in a listview
main.XML
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="20sp" >
</TextView>
CarActivity.JAVA
package car.brand.test;
package car.brand.test;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class CarActivity extends ListActivity {
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
String[] values = new String[] { "BMW", "Mercedes","Nissan"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
String item = (String) getListAdapter().getItem(position);
Toast.makeText(this, item + " selected", Toast.LENGTH_LONG).show();
}
}
You don't need to pass data to another ListView, only one ListView is needed to achieve what you desire. You will have to do the mapping yourself, i.e. BMW maps to a list(java.util.ArrayList) that contains all BMW car series models, and Mercedes maps to a list that contains all Mercedes car series models, etc.
When you select a car brand, you swap the underlying dataset for the ListView and call notifyDataSetChanged()
.
snippet:
MyAdapter myAdapter = ...;
Map<String, List<String>> carSeriesMap = ...;
protected void onListItemClick(ListView l, View v, int position, long id) {
String brand = (String) getListAdapter().getItem(position);
List<String> carSeriesList = carSeriesMap.get(brand);
// set carSeriesList as the underlying dataset for the adapter
myAdapter.setDataset(carSeriesList);
}
class MyAdapter extends BaseAdapter {
List<String> dataset;
public void setDataset (List<String> newDataset) {
dataset = newDataset;
notifyDataSetChanged();
}
public View getView(int position, ......) {
// get data from dataset
String text = dataset.get(position);
// other code here...
}
}