在我的Android应用程序中,我在布局中有4个微调器。我已经使用侦听器为旋转器设置了值。 以下代码段显示了我是如何尝试这样做的。 但它似乎没有听取用户选择的值。相反它获得了在spinner中设置的第一个值。我对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) {
}
});
以下是布局的 ![这是logcat结果。我的代码中的第262行是String selected_grama = gramaSpinner.getSelectedItem()。toString(); ] 1 <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>
答案 0 :(得分:1)
在上面发布的代码中,您要为代码id
和{{1}中的两个微调器分配相同微调器的spinner2
即provSpinner
disSpinner
}。因此,请检查布局XML文件中的资源ID,然后更改任何一个微调器的资源ID。希望有所帮助。
答案 1 :(得分:0)
您在两个微调器对象中使用id为spinner2的资源。这可能是问题