1

I am trying to use the selection in one ListView to determine the list displayed in another ListView to four dimensions. For example the first list would be:

A, B, C

Then by selecting list A you'd get

1, 2, 3

and B would return.

4, 5, 6

If 1 were selected then

I, II, III.

Selecting 5 would return yet anther list so on and so forth. This would continue on to a forth and final list which ultimately would be the user's selection.

The final ListView, to start, will have about 380 different lists it could be. If selection A then 2 then XII then display the correct list. Presumably I could name the lists according to selection as A2XII then call the list by that name.

Currently, my theory is to set the first ListView then in the Listener make it INVISIBLE and make VISIBLE the next. Then use the previous selection to determine what list the Adapter uses.

So my question is: How do I take the selection from one list and have it determine the next list by name?

        package org.wmu.LMWTA;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

        import org.wmu.LMWTA.R;

        public class Frag_Form extends Fragment {

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        final View V = (LinearLayout)inflater.inflate(R.layout.form_layout, container, false);
        if (container == null) {
            return null;
        }

        final ListView cityList = (ListView) V.findViewById(R.id.list_city);
        final ListView cat = (ListView) V.findViewById(R.id.cat);
        ArrayAdapter<CharSequence> cityAdapt = ArrayAdapter.createFromResource(getActivity(),
                R.array.cities, android.R.layout.simple_spinner_dropdown_item);
        ArrayAdapter<CharSequence> catAdapt = ArrayAdapter.createFromResource(getActivity(),
                R.array.cat, android.R.layout.simple_spinner_dropdown_item);
        //ArrayAdapter<CharSequence> catAdapt = ArrayAdapter.createFromResource(getActivity(),
        //        R.array.subCat, android.R.layout.simple_spinner_dropdown_item);

        cat.setAdapter(catAdapt);
        cat.setVisibility(View.GONE);
        cityList.setAdapter(cityAdapt);
        cityList.setOnItemClickListener( new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // When clicked, show toast
                Toast.makeText(parent.getContext(),
                (CharSequence) cityList.getItemAtPosition(position).toString().replaceAll("\\s+", ""), Toast.LENGTH_SHORT).show();
                cityList.setVisibility(View.GONE);
                cat.setVisibility(View.VISIBLE);
            }
        });
        return V;
    }
}

If there is a better way to do this I would be very grateful to know the answer. My first thought was to use Spinners but I run into the same problem without nesting 380 Switches inside of more Switches.

CaptinG
  • 13
  • 3

2 Answers2

1

Instead of a multitude of ListViews, use a single large data structure and one ListView for each dimension populated based on the selection in the previous ListView.

For example, you might have a data structure with locations divided by continent, country, region, and city. Each continent has a list of countries; each country has a list of regions; and each region has a list of cities. Depending on the scale of the dataset this might be implemented as a single series of nested Lists, or you could dynamically retrieve the next level from a database, web service, or other external mechanism.

Initially you would have a ListView displaying continents. Once the user selects one, the selected index gives you the List of countries to populate the next ListView with, and so on for regions and cities.

blahdiblah
  • 33,069
  • 21
  • 98
  • 152
0

Setup the arrays so that they contain an index of the item of the previous list that they belong to.

<string-array name="level_1">
        <item>A</item>
        <item>B</item>
        <item>C</item>
</string-array>

The next three arrays are the ones that depend on level_1

<string-array name="level_2_sub00">
        <item>1</item>
        <item>2</item>
        <item>3</item>
</string-array>

<string-array name="level_2_sub01">
        <item>4</item>
        <item>5</item>
        <item>6</item>
</string-array>

And the first level3 array:

<string-array name="level_3_sub00">
        <item>I</item>
        <item>II</item>
        <item>III</item>
</string-array>

Now you just need to use the position of the selected item to calculate the name of the next array:

String level2_arrayname = "level_2_sub" + String.valueof(position);
int resourceIdNext = getActivity().getResources.getIdentifier(level2_arrayname, "array", mypackagename);
Rick Falck
  • 1,778
  • 3
  • 15
  • 19