2011-08-09 125 views
0

我想設置一個微調項目的位置,但微調控件正在用來自數據庫的數據的simplecursoradapter填充。我想從數據庫中獲取當前字符串,然後將微調器設置爲該值。我知道,通過使用arrayadapter我可以從適配器獲得的位置是這樣的:Android:從SimpleCursorAdapter獲取位置

String myString = queryData.getString(queryData.getColumnIndex("myString")); 

//cast to an ArrayAdapter 
mySpinnerAdapter adapter = (mySpinnerAdapter) mySpinner.getAdapter(); 
int spinnerPosition = ? 

//set the default according to value 
mySpinner.setSelection(spinnerPosition); 

但是,有沒有辦法讓一個項目從SimpleCursorAdapter的位置?在過去,我總是在cursoradapter旁邊構建一組我的微調數據,但這似乎是一種骯髒的方式。如果它雖然唯一途徑...

回答

0

你可以通過這樣獲得該項目的位置:

ArrayList<View> views = new ArrayList<View>(); 
listView1.reclaimViews(views); 
for (int i = 0; i < views.size(); i++) 
{ 
    View v = views.get(i); 
    // Get the view's text value and compare it with your string here 
    // If the two strings match, store the position, which is 'i' in this case 
    // If your view is a textview, you would do this: 
    TextView tv = (TextView)v.findViewById(R.id.textView1); 
    if (tv.getText().toString().toLowerCase().compareTo(textToCompare.toLowerCase()) == 0) 
    { 
     // Store position here 
    } 
} 
+0

我想從SimpleCursorAdapter中獲取位置,以便從數據庫中設置一個spinners selected items。我無法使用微調對象的reclaimViews。是否有另一種方法可以用來實現相同? – ryandlf

+0

我想不出任何其他方式來實現你想要的東西,除非你做你已經在你的問題中說過的話。 –

0

即使我面臨同樣的問題。打破了我的頭幾個小時後,我找到了這個解決方案。 我希望它能幫助你。

/// value is column name in DB. take value from      
final String[] from = new String[] {"value"}; 
/// field in spinner. Put value into (keep "R.id.text1" as it is..) 
final int[] to = new int[]{R.id.text1}; 

// Get Database Access Object to interact with DB 

DataAccessObject dataAccessObject = new DataAccessObject(context); 

final Cursor valueCursor = dataAccessObject.querySpinnerValues(); 

final SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(
     context, R.layout.spinner_row_layout, valueCursor, 
     from , to);   

// when ever a field is selected in the spinner's "spinner.setOnItemSelectedListener" method will be called 
// and the primary key 
// associted with that particular value will be saved into "someFieldPkMap" map or you can choose other collection..or some 
// instance variable.. 
// 
// Note : Here i am fetching two columns fron DB. 
// 1. "_id" Primary key should always be "_id" otherwise it will not work 
// 2. "value" which will be displayed in the spinner. this column can be anything. but you need to map it. The way i did eg.. 

// final String[] from = new String[] {"value"}; /// value is column name in DB. take value from 
// final int[] to = new int[]{R.id.text1}; /// field in spinner. Put value into (keep "R.id.text1" as it is..) 
// 
spinner.setOnItemSelectedListener(new OnItemSelectedListener() { 
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) 
    {   

     valueCursor.moveToPosition(position);      

     // Get the primary Key associated with the value... 
     // Use "someFieldPkMap" map to get the primary key 
     someFieldPkMap.put(fieldName, Integer.parseInt(valueCursor.getString(0)));       
    } 
    public void onNothingSelected(AdapterView<?> arg0) {} 
}); 

spinner.setAdapter(simpleCursorAdapter); 
+0

我將不得不嘗試這個併發布我的結果。謝謝! – ryandlf