我無法解決我的微調setSelection()方法的問題。我正在編寫一個程序來保存數據,這是我從某種表格中獲得的數據。當用戶打開一個程序時,它會從db中加載數據並添加3個空的「表」行,以便用戶可以填充它們並保存。當從數據庫加載數據時,我需要正確設置選項,但是當我嘗試我的代碼時,它不起作用,屏幕上的所有旋鈕都會顯示0選項。 這是我的代碼。
onCreate()方法:
Android:微調setSelection不起作用
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.data);
dbHelper = new DBHelper(this);
content = (LinearLayout) findViewById(R.id.content);
inflater = getLayoutInflater();
Calendar c = Calendar.getInstance();
cur_year = c.get(Calendar.YEAR);
cur_month = c.get(Calendar.MONTH);
cur_day = c.get(Calendar.DAY_OF_MONTH);
load_array();
load_database();
for (int i = 0; i < EMPTY_LINES_TO_DRAW; i++) {
draw();
}
}
load_array()是一個函數,負載數組 「供應商」(從文件),我然後把我的紡絲器適配器。
load_database()函數:
private void load_database() {
SQLiteDatabase db = dbHelper.getWritableDatabase();
Cursor c = db.query("mytable", null, null, null, null, null, null);
if (c.moveToFirst()) {
int date_1_Index = c.getColumnIndex("date_1");
int key_Index = c.getColumnIndex("key_field");
int supp_Index = c.getColumnIndex("supplier");
int sum_Index = c.getColumnIndex("summ_field");
int check_Index = c.getColumnIndex("check_field");
int date_2_Index = c.getColumnIndex("date_2");
do {
draw(c.getString(date_1_Index), c.getString(key_Index), c.getString(supp_Index), c.getInt(sum_Index), c.getInt(check_Index), c.getString(date_2_Index));
} while (c.moveToNext());
}
c.close();
}
draw()函數:
public void draw(String date_1_string, String key_string, String supp_string, Integer sum_int, Integer check_int, String date_2_string) {
View item = inflater.inflate(R.layout.ll_item, content, false);
for (int i = 0; i < 6; i++) {
delta_id[i] = generateViewId();
}
id.add(delta_id.clone());
Button date_1 = (Button) item.findViewById(R.id.date_1);
date_1.setText(date_1_string);
date_1.setOnClickListener(this);
date_1.setId(delta_id[0]);
EditText key = (EditText) item.findViewById(R.id.key);
key.setText(key_string);
key.setId(delta_id[1]);
Spinner spin = (Spinner) item.findViewById(R.id.spin);
adapter_supp = new ArrayAdapter<String>(this, R.layout.spinner, suppliers);
spin.setAdapter(adapter_supp);
spin.setPrompt("Поставщик:");
Log.d("my_log", "supp_string = " + supp_string);
spin.setSelection(Arrays.asList(suppliers).indexOf(supp_string));
Log.d("my_log", "Index of the supp_string = " + Arrays.asList(suppliers).indexOf(supp_string));
adapter_supp.notifyDataSetChanged();
spin.setId(delta_id[2]);
EditText sum = (EditText) item.findViewById(R.id.sum);
sum.setText(sum_int.toString());
sum.setId(delta_id[3]);
CheckBox check = (CheckBox) item.findViewById(R.id.check);
if (check_int == 1) {
check.setChecked(true);
} else if (check_int == 0){
check.setChecked(false);
}
check.setId(delta_id[4]);
Button date_2 = (Button) item.findViewById(R.id.date_2);
date_2.setText(date_2_string);
date_2.setOnClickListener(this);
date_2.setId(delta_id[5]);
content.addView(item);
}
奇怪的是,當我去到第二個活動,然後返回到第一,我有我的選擇設定正確,就像那樣。但是,當我關閉程序,然後再次打開它,設置爲默認項與索引爲0的所有紡紗這裏是我的onResume()代碼:
@Override
public void onResume() {
super.onResume();
load_array();
// Верхний, главный, фильтр.
adapter_supp = new ArrayAdapter<String>(this, R.layout.spinner, suppliers);
adapter_supp.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner filter_supp = (Spinner) findViewById(R.id.filter_supp);
filter_supp.setAdapter(adapter_supp);
filter_supp.setPrompt("Фильтр по поставщику:");
// Обновить спиннеры поставщиков. Update spinners.
for (int i = 0; i < id.size(); i++) {
Integer[] item = id.get(i);
Spinner supp = (Spinner) findViewById(item[2]);
supp.setAdapter(adapter_supp);
if (pause_cash_string != null) {
supp.setSelection(Arrays.asList(suppliers).indexOf(pause_cash_string[i]));
adapter_supp.notifyDataSetChanged();
}
}
}
是,Arrays.asList(供應商).indexOf()在這兩個地方(在onResume()和draw()函數)返回正確的索引,我已經檢查了幾次。
看看我的答案在這裏有幫助:http://stackoverflow.com/a/31774927/1617737 –