私のAndroidアプリでは、レイアウトに4つのスピナーがあります。リスナーを使用して、スピナーに値を設定しました。次のコードスニペットは、私がこれをどのように試みたかを示しています。しかし、ユーザーが選択した値をリッスンしていないようです。代わりに、スピナーで設定された最初の値を取得します。onItemSelected()メソッドで定義されたパラメーターについてはよくわかりません。私がしている間違いは何ですか?
質問を編集しました。次のコードを検討してください。同じ手順を使用して、異なるクエリを使用してスピナーに値を設定しています。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grama_niladhari);
final ArrayList<String> provinceList = new ArrayList<String>();
final ArrayList<String> disList = new ArrayList<String>();
final ArrayList<String> divList = new ArrayList<String>();
final ArrayList<String> gramaList = new ArrayList<String>();
final Spinner disSpinner = (Spinner) findViewById(R.id.spinner2);
final Spinner divSpinner = (Spinner) findViewById(R.id.spinner3);
final Spinner gramaSpinner = (Spinner) findViewById(R.id.spinner4);
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, provinceList);
final ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, disList);
final ArrayAdapter<String> adapter3 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, divList);
final ArrayAdapter<String> adapter4 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, gramaList);
Button b = (Button) findViewById(R.id.button1);
DBHelper dbhlper = new DBHelper(getApplicationContext());
try {
dbhlper.createDataBase();
mydb = dbhlper.openDataBase();
} catch (IOException e) {
e.printStackTrace();
}
final String selected_item;
String province_query = "SELECT pro_code,name FROM jos_province";
Cursor c = mydb.rawQuery(province_query, null);
provinceList.clear();
final HashMap<String, Integer> hm = new HashMap<String, Integer>();
if (c != null) {
int i = 1;
int noCols = c.getCount();
if (c.moveToFirst()) {
do {
Log.d("Data" + i, c.getString(1));
provinceList.add(c.getString(1));
hm.put(c.getString(1), c.getInt(0));
} while (c.moveToNext());
}
}
final Spinner provSpinner = (Spinner) findViewById(R.id.spinner1);
// ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,
// provinceList);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
provSpinner.setAdapter(adapter1);
// //////////////////////////////// Handling spinner1 ///////////////////////////////////
final String sel_district_id = null;
provSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@TargetApi(11)
@Override
public void onItemSelected(AdapterView<?> parent, View arg1,
int arg2, long arg3) {
String province = provSpinner.getSelectedItem().toString();
int sel_province_id = hm.get(province);
String district = "SELECT id,name_english from jos_district `enter code here`where province_id= "+ sel_province_id;
// ArrayList<String> disList=new ArrayList<String>();
Cursor c = mydb.rawQuery(district, null);
hm.clear();
disList.clear();
if (c != null) {
int i = 1;
int noCols = c.getCount();
if (c.moveToFirst()) {
do {
Log.d("Data" + i, c.getString(1));
disList.add(c.getString(1));
disList.toString());
i++;
hm.put(c.getString(1),c.getInt(0));
} while (c.moveToNext());
}
}
//String sel_district_id ="66";
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
String listCount=Integer.toString(adapter2.getCount());
disSpinner.setAdapter(adapter2);
}
@Override
public void onNothingSelected(AdapterView<?> adapter1) {
// TODO Auto-generated method stub
}
});
disSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View arg1,
int arg2, long arg3) {
String selected_dis = `enter code here`disSpinner.getSelectedItem().toString();
String sel_district_id = hm.get(selected_dis).toString();
String division = "SELECT id,name_english FROM jos_division `enter code here`WHERE district_id= "
+ sel_district_id;
Cursor c = mydb.rawQuery(division, null);
hm.clear();
if (c != null) {
int i = 1;
int noCols = c.getCount();
if (c.moveToFirst()) {
do {
Log.d("Data " + i, c.getString(1));
divList.add(c.getString(1));
hm.put(c.getString(1), `enter code here`c.getInt(0));
// `enter code here`provinceList.add(c.getString(1));
} while (c.moveToNext());
}
}
// set the view for the Drop down list
`enter code here`adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// set the ArrayAdapter to the spinner
divSpinner.setAdapter(adapter3);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
divSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View arg1,
int arg2, long arg3) {
String `enter code here`selected_division=divSpinner.getSelectedItem().toString();
sel_division_id = hm.get(selected_division).toString();
String gramaniladhari = "SELECT lifecode,name_english FROM jos_gramaniladhari WHERE divisional_secretariat_id= " + sel_division_id;
hm.clear();
Cursor c=mydb.rawQuery(gramaniladhari, null);
if (c != null) {
int i = 1;
int noCols = c.getCount();
if (c.moveToFirst()) {
do {
Log.d("Data " + i, c.getString(1));
divList.add(c.getString(1));
hm.put(c.getString(1),c.getInt(0));
} while (c.moveToNext());
}
}
// set the view for the Drop down list
`enter code here`adapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// set the ArrayAdapter to the spinner
gramaSpinner.setAdapter(adapter4);
String `enter code here`selected_grama=gramaSpinner.getSelectedItem().toString();
String lifeCode = hm.get(selected_grama).toString();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
以下はレイアウトのxmlファイルです
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="22dp"
android:text="@string/enter_your_provincial_details"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="16dp"
android:text="@string/button" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="40dp"
android:text="@string/province"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignRight="@+id/textView4"
android:layout_below="@+id/textView2"
android:layout_marginTop="35dp"
android:text="@string/district"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView3"
android:layout_marginTop="44dp"
android:text="@string/division"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView4"
android:layout_marginTop="46dp"
android:text="@string/grama_niladhari"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Spinner
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView3"
android:layout_alignParentRight="true"
android:layout_toRightOf="@+id/button1" />
<Spinner
android:id="@+id/spinner2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView4"
android:layout_alignLeft="@+id/spinner1"
android:layout_alignParentRight="true" />
<Spinner
android:id="@+id/spinner3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/spinner2"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/textView4" />
<Spinner
android:id="@+id/spinner4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView5"
android:layout_alignLeft="@+id/spinner3"
android:layout_alignParentRight="true" />
</RelativeLayout>
![これはlogcatの結果です。私のコードの262行目は次のとおりです。Stringselected_grama = gramaSpinner.getSelectedItem()。toString();
] 1