0

スピナー アイテムの位置を設定したいのですが、スピナーにはデータベースからのデータを含む simplecursoradapter が設定されています。データベースから現在の文字列を取得し、スピナーをその値に設定したいと思います。arrayadapter を使用すると、次の方法でアダプターから位置を取得できることがわかっています。

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 から項目の位置を取得する方法はありますか? 過去に、私は常にカーソルアダプターと一緒にスピナーデータの配列を構築していましたが、これは汚い方法のようです。それが唯一の方法であれば...

4

2 に答える 2

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);
于 2011-08-17T13:13:55.197 に答える
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
    }
}
于 2011-08-09T02:46:47.893 に答える