Android: the value of one counter depends on the value of another counter

I have a problem on Android Spinner. In my application, I created two Spinner on the main layout. 'State' Spinner and 'District' Spinner. All data on Spinner is stored in a SQLite database. I want to show the "District" list on the second counter depending on the choice of a particular "state" in the first counter. Example: suppose when I select Karnataka in the first spinner, then the application first retrieves the entire area from the SQLite database related to the state of Karnataka, and then displays it on the second Spinner.

To do this, I do all the database activities correctly, this means creating two "states" of the table and the "region" in which one column in the environment table is a foreign key that refers to one of the primary key of the "state table"

db.execSQL("create table "+STATE_TABLE+" ("+ STATE_ID+" integer primary key autoincrement not null, "+ STATE_NAME+" text"+")"); db.execSQL("create table "+DISTRICT_TABLE+" ("+DISTRICT_ID+ " integer primary key autoincrement not null,"+DISTRICT_NAME +" text,"+STATE_ID+" integer, FOREIGN KEY(" +STATE_ID+") REFERENCES "+STATE_TABLE +"("+STATE_ID+")"+")"); 

Now in the activity class:

 spinnerState = (Spinner)findViewById(R.id.spinner1); spinnerDistrict = (Spinner)findViewById(R.id.spinner2); stateList = new ArrayList<String>(); districtList = new ArrayList<String>(); 

Suppose all data is ready to be stored in a database.

Now I need to get all the "State" data from the database and add it to the article collector, which is an ArrayList.

 Cursor stateCursor = database.query(STATE_TABLE, new String[]{STATE_ID, STATE_NAME}, null, null, null, null, STATE_NAME); stateCursor.moveToFirst(); if(! stateCursor.isAfterLast()){ do{ int id = stateCursor.getInt(0); String stateName = stateCursor.getString(1); stateList.add(stateName); }while(stateCursor.moveToNext()); } stateCursor.close(); 

after that I create one ArrayAdapter and put this list of states in it.

 spinnerState.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, stateList)); 

Then I put the following code in the activity class:

 spinnerState.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View v, int pos, long id) { } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }); 

Now my problem is:

  • How do I get a StateId to execute a select query so that the entire area is associated with a specific state in the database.
  • How the adapter for the district will be generated.
  • where i put all this code.

Here is what I need to create theListList after getting the value from the Spinner state.

A similar question that was previously asked on this website is what they are doing: they already create two adapters for two counters, and then use setOnItemSelectedListener. Please help me, because here my mind completely stops working. I send out a lot of books and websites, but do not mention these issues.

+4
source share
2 answers

I see a couple of solutions here:

Option 1:

Instead of declaring stateList as ArrayList<String> create a custom POJO StateInfo and assign stateList as ArrayList<StateInfo>

 public class StateList { private int id; private String stateName; //Constructors, getters and setters } 

Then

 if(! stateCursor.isAfterLast()){ do{ int id = stateCursor.getInt(0); String stateName = stateCursor.getString(1); stateList.add(new StateInfo(id, stateName)); }while(stateCursor.moveToNext()); } 

Now you can do this:

 public void onItemSelected(AdapterView<?> parent, View v, int pos, long id) { int id = stateList.get(pos).getId(); //Use This ID to construct your next SQLite query; and populate the district spinner. } 

If you do this, you will need to create a custom ArrayAdapter and implement the getView() method. Look at the Android docs for how to do this.

Option 2:

Why do you need a status identifier? I assume that you can write a query to get a list of districts with only the state name. In this case, you can save stateList as an ArrayList<String> , and you don't need the StateInfo class at StateInfo . Just do the following:

 public void onItemSelected(AdapterView<?> parent, View v, int pos, long id) { String stateName = stateList.get(pos); //Use this name to construct your next SQLite query; and populate the district spinner. } 
+3
source

For problem 1:

You just need to keep track of which user selects the user as a spinner, and according to his position you should find your corresponding identifier. Then you can request the area accordingly. Obviously, you have to use the onItemSelectedListener of this counter to get the position of the selected state.

For problem 2:

You need to make a method in a helper class that takes stateId (which you would calculate as I said above) and return you by the county names in the String array. Thus, in your main action, in the spinner's onItemSelectedListener state, you will need to capture this array of strings and prepare an adapter for it. There you will need to install the adapter on your counter.

This will always give you information about what the user selects in the state array.

For task 3:

Almost all code will be in the onItemSelectedListener of only your status counter. Just a method to extract the associated county names will be in your helper class.

+1
source

Source: https://habr.com/ru/post/1388597/


All Articles