我从数据库中提取数据并用数据填充一系列EditTexts和Spinners。
我可以毫无问题地填充EditTexts,但是微调器正在重置为默认值。
我尝试使用setSelection()
,但它无法正常使用。
这是我的代码:
spinner = (Spinner) rootView.findViewById(R.id.vehicle);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_spinner_item,
new String[] {"Car", "Bus", "Plane", "Bicycle"});
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.getOnItemSelectedListener();
spinner = (Spinner) rootView.findViewById(R.id.category);
ArrayAdapter<String> adapterCategory = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_spinner_item,
new String[] {"Vacation", "Commute", "Leisure", "Shopping"});
adapterCategory.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapterCategory);
spinner.getOnItemSelectedListener();
}
public void updateTripView(String position) {
Cursor cursor = ((TripListActivity)getActivity()).dbHelper.fetchTripById(position);
if (cursor != null) {
try {
String tripId =
cursor.getString(cursor.getColumnIndexOrThrow("tripId"));
int tripCategory =
cursor.getInt(cursor.getColumnIndexOrThrow("category"));
int tripVehicle =
cursor.getInt(cursor.getColumnIndexOrThrow("vehicle"));
String tripDistance =
cursor.getString(cursor.getColumnIndexOrThrow("distance"));
String tripDate =
cursor.getString(cursor.getColumnIndexOrThrow("date"));
String tripNote =
cursor.getString(cursor.getColumnIndexOrThrow("note"));
((TextView) rootView.findViewById(R.id.tripId)).setText(tripId);
((Spinner) rootView.findViewById(R.id.category)).setSelection(tripCategory);
((Spinner) rootView.findViewById(R.id.vehicle)).setSelection(tripVehicle);
((TextView) rootView.findViewById(R.id.distance)).setText(tripDistance);
((TextView) rootView.findViewById(R.id.date)).setText(tripDate);
((TextView) rootView.findViewById(R.id.note)).setText(tripNote);
} catch (IllegalArgumentException e) {
Log.d(TAG, "IllegalArgumentException");
}
}
mCurrentPosition = position;
}
编辑:我找到了一个基于答案的解决方案:How to set selected item of Spinner by value, not by position?
//get index value of spinner
private int getIndex(Spinner spinner, String myString)
{
int index = 0;
for (int i=0;i<spinner.getCount();i++){
if (spinner.getItemAtPosition(i).toString().equalsIgnoreCase(myString)){
index = i;
break;
}
}
return index;
}
我修改过的updateTrip
课程:
public void updateTripView(String position) {
Cursor cursor = ((TripListActivity)getActivity()).dbHelper.fetchTripById(position);
if (cursor != null) {
try {
String tripId =
cursor.getString(cursor.getColumnIndexOrThrow("tripId"));
String tripCategory =
cursor.getString(cursor.getColumnIndexOrThrow("category"));
String tripVehicle =
cursor.getString(cursor.getColumnIndexOrThrow("vehicle"));
String tripDistance =
cursor.getString(cursor.getColumnIndexOrThrow("distance"));
String tripDate =
cursor.getString(cursor.getColumnIndexOrThrow("date"));
String tripNote =
cursor.getString(cursor.getColumnIndexOrThrow("note"));
spinner = ((Spinner) rootView.findViewById(R.id.category));
ArrayAdapter<String> adapterCategory = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_spinner_item,
new String[] {"Vacation", "Commute", "Leisure", "Shopping"});
spinner.setAdapter(adapterCategory);
((TextView) rootView.findViewById(R.id.tripId)).setText(tripId);
//set spinner value based on index
spinner.setSelection(getIndex(spinner, tripCategory));
spinner1 = ((Spinner) rootView.findViewById(R.id.vehicle));
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_spinner_item,
new String[] {"Car", "Bus", "Plane", "Bicycle"});
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter);
//set spinner value based on index
spinner1.setSelection(getIndex(spinner1, tripVehicle));
((TextView) rootView.findViewById(R.id.distance)).setText(tripDistance);
((TextView) rootView.findViewById(R.id.date)).setText(tripDate);
((TextView) rootView.findViewById(R.id.note)).setText(tripNote);
} catch (IllegalArgumentException e) {
Log.d(TAG, "IllegalArgumentException");
}
}
mCurrentPosition = position;
}