Android:从 SimpleCursorAdapter 获取位置

发布于 2024-11-28 08:05:41 字数 514 浏览 0 评论 0原文

我想设置微调器项目的位置,但微调器正在使用带有数据库数据的简单游标适配器填充。我想从数据库中获取当前字符串,然后将微调器设置为该值。我知道通过使用 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 获取项目的位置?过去,我总是沿着光标适配器构建一个旋转器数据数组,但这似乎是一种肮脏的做法。如果这是唯一的方法的话...

I would like to set the position of a spinner item, but the spinner is being populated with a simplecursoradapter with data from a database. I would like to get the current string from the database, then set the spinner to that value. I know that by using an arrayadapter I can get the position from the adapter this way:

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);

But is there a way to get the position of an item from a SimpleCursorAdapter? In the past I have always build an array of my spinner data along side the cursoradapter, but this seems like a dirty way of doing it. If its the only way though...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅唱々樱花落 2024-12-05 08:05:41

您可以通过执行以下操作来获取项目的位置:

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
    }
}

You can get the position of the item by doing this:

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
    }
}
别靠近我心 2024-12-05 08:05:41

即使我也面临同样的问题。在我花了几个小时的时间后,我找到了这个解决方案。
我希望它能帮助你。

/// 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);

Even i face the same problem. After breaking my head for some hours, i found this solution.
I hope it will help you.

/// 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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文