首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何实现微调器的MatrixCursor?

如何实现微调器的MatrixCursor?
EN

Stack Overflow用户
提问于 2012-04-16 11:54:28
回答 1查看 2.3K关注 0票数 1

我有一个返回CursorSQLite查询。我想通过实现一个MatrixCursorCursor中添加一些额外的行(试图在单击时保持实际数据的第一项不被自动选中)。然后我想将它们映射到一个SimpleCursorAdapter。我一直在阅读帖子(和代码),但仍然不清楚如何将其编码到下面列出的现有代码中。

代码语言:javascript
复制
    Cursor cursor = myDB.query(DATABASE_TABLE_NAME, resultColumns, whereClause,
            whereArgs, null, null, null, null);

    // Create Spinner View object from layout resource
    Spinner spinner = (Spinner) findViewById(R.id.spinner);

    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
            android.R.layout.simple_spinner_item, // Use a template
                                                  // that displays a
                                                  // text view
            cursor, // Give the cursor to the adapter
            new String[] {"ename"}, // Map the NAME column in the
                                                 // people database to...
            new int[] {android.R.id.text1}); 

    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-04-16 15:55:53

如果您想要从一个简单的Cursor构建一个MatrixCursor,则必须解析整个初始Cursor并附加所需的行:

代码语言:javascript
复制
//...
    MatrixCursor mc = new MatrixCursor(resultColumns);
    // add extra rows, this will probably not work if you want to insert them
    // between the initial cursor rows because of the _id column that need autoincrement values
    mc.addRow(new Object[] { new Long(-2), "Extra name1" }); 
    mc.addRow(new Object[] { new Long(-1), "Extra name2" });
    // I don't know what your cursor holds, I assumed you have the _id column(long value)
    // and a name(String value)
    int size = cursor.getCount();
    for (int i = 0; i < size; i++) {
        cursor.moveToPosition(i);
            mc.addRow(new Object[] {
            cursor.getLong(cursor.getColumnIndex(/*the _id column*/)),
            cursor.getString(cursor.getColumnIndex(/* the name column(ename?!?)*/)) });
    }
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
            android.R.layout.simple_spinner_item, mc, new String[] {"ename"}, new int[] {android.R.id.text1}); 
//...

如果您这样做只是为了避免在显示Spinner时触发OnItemSelectedListener,也许您可以使用另一种方法。例如,在您的监听程序中:

代码语言:javascript
复制
    //boolean status = true; flag in MyOnItemSelectedListener

    @Override
    public void onItemSelected(AdapterView<?> parent, View view,
            int position, long id) {
        if (status) {
            status = false;// this is the first listener trigger so you probably want to ignore it
            return;
        }
        // do stuff here
    }

注意:我不知道上面的解决方案有多好。如果你看一看,可能有更好的解决方案来解决这个与Spinner相关的问题。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10168530

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档