Android ListView pass data to another listview

Go To StackoverFlow.com

0

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();
}

}

2012-04-05 23:36
by user1316438


0

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...
    }
}
2012-04-06 01:14
by neevek
I wouldn't generally recommend this. Not only do you swap out data, but probably you want different behavior when you click on an item too. Also, what if you want to use some layout other than just a single line? It's inherently different data, it's usually better to just use a new ListView, and often even a new Activity - kabuko 2012-04-06 01:41
Using either a new ListView or a new Activity is overkill for the OP's problem. Yet I agree with you if the problem is more complicated. I just offered an option and tried to keep simple things simple - neevek 2012-04-06 03:04
So what if the user selects BMW, you clear the data and load all the BMWs that you want, but then the user realized they wanted Mercedes instead? If you have one activity, then when the user clicks the back button unless you manually handle the back button and manage your own navigation stack, they'll back out of the whole thing. Using a new Activity is not overkill. It's standard - kabuko 2012-04-06 18:27
Ads