10

我正在使用以下代码为 AutoCompleteTextView 设置适配器(SimpleCursorAdapter)

mComment = (AutoCompleteTextView) findViewById(R.id.comment);

    Cursor cComments = myAdapter.getDistinctComments();
    scaComments = new SimpleCursorAdapter(this,R.layout.auto_complete_item,cComments,new String[] {DBAdapter.KEY_LOG_COMMENT},new int[]{R.id.text1});

    mComment.setAdapter(scaComments);

auto_complete_item.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

这是实际控制的xml

<AutoCompleteTextView
                        android:id="@+id/comment"
                        android:hint="@string/COMMENT"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:textSize="18dp"/>

下拉列表似乎可以正常工作,并显示项目列表。当我从列表中进行选择时,我在文本视图中得到一个 sqlite 对象('android.database.sqlite.SQLiteCursor@'...)。任何人都知道会导致这种情况,或者如何解决这个问题?

谢谢

好的,我可以挂接到 OnItemClick 事件,但 AutoCompleteTextView 小部件的 TextView.setText() 部分在此之后更新。OnItemSelected() 事件永远不会被触发,而 onNothingSelected() 事件会在首次显示下拉项目时被触发。

       mComment.setOnItemClickListener( new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub

            SimpleCursorAdapter sca = (SimpleCursorAdapter) arg0.getAdapter();


            String str = getSpinnerSelectedValue(sca,arg2,"comment");

            TextView txt = (TextView) arg1;
            txt.setText(str);
            Toast.makeText(ctx, "onItemClick", Toast.LENGTH_SHORT).show();

        }

    });
    mComment.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {


            Toast.makeText(ctx, "onItemSelected", Toast.LENGTH_SHORT).show();

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub
            Toast.makeText(ctx, "onNothingSelected", Toast.LENGTH_SHORT).show();
        }

    });

任何人都对如何覆盖 TextView 的更新有任何想法?

谢谢

帕特里克

4

4 回答 4

9

我认为您不必更新 AutoCompleteTextView 的文本。它应该自动完成。它通过调用 [CursorAdapter.convertToString(...)][1] 方法来做到这一点。如果您阅读该方法的描述,它会指出这一点。因此,如果您正在编写自己的 CursorAdapter,您将覆盖该方法以返回您希望显示在建议列表中的任何文本。这家伙很好地解释了如何做到这一点:

第 86 行 - http://thinkandroid.wordpress.com/2010/02/08/writing-your-own-autocompletetextview/

但是,由于您使用的是 SimpleCursorAdapter,因此无法覆盖此方法。相反,您需要实现/创建一个 [SimpleCursorAdapter.CursorToStringConverter][2] 并将其传递给 [Si​​mpleCursorAdapter.setCursorToStringConverter(...)][3]:

 SimpleCursorAdapter adapter = new SimpleCursorAdapter(context, layout, cursor, from, to);
 CursorToStringConverter converter = new CursorToStringConverter() {

    @Override
    public CharSequence convertToString(Cursor cursor) {
       int desiredColumn = 1;
       return cursor.getString(desiredColumn);
    }
 }; 

 adapter.setCursorToStringConverter(converter);

或者,如果您不想创建 CursorToStringConverter,请使用 [SimpleCursorAdapter. setStringConversionColumn(...)][4] 方法。但是我认为您仍然必须将 CursorToStringConverter 显式设置为空:

 int desiredColumn = 1;
 adapter.setCursorToStringConverter(null);
 adapter.setStringConversionColumn(desiredColumn);

抱歉,垃圾邮件拦截器不允许我发布指向 Android 文档的链接,该文档描述了我在上面发布的链接。但是快速的谷歌搜索会指向正确的文档页面。

于 2010-06-02T18:21:24.620 回答
3

[迟到的答案,仅作记录。编辑删除我的建议,即子类化是必要的。]

要将 SimpleCursorAdapter 与 AutoCompleteTextView 一起使用,您需要在适配器上设置两个处理程序:CursorToStringConverterFilterQueryProvider。伪代码如下:

    adapter.setCursorToStringConverter(new CursorToStringConverter() {
        public String convertToString(android.database.Cursor cursor) {
            // Assume that "someColumn" contains the strings that we want to
            // use to identify rows in the result set.
            final int columnIndex = cursor.getColumnIndexOrThrow("someColumn");
            final String str = cursor.getString(columnIndex);
            return str;
        }
    });

    adapter.setFilterQueryProvider(new FilterQueryProvider() {
        public Cursor runQuery(CharSequence constraint) {
            // runSomeQuery will look for all rows in the database
            // that match the given constraint.
            Cursor cursor = runSomeQuery(constraint);
            return cursor;
        }
    });
于 2010-11-15T20:20:20.620 回答
0

为了解决这个问题,我刚刚扩展SimpleCursorAdapter并实现了该方法convertToString()。然后我创建了一个实例并将其设置为适配器。

为了在AutoCompleteTextView使用 CursorAdapters 时允许过滤,我还使用了setFilterQueryProvider(). 看到这个问题

我在 Activity 中的扩展类如下所示:

private static class AutoCompleteCursorAdapter extends SimpleCursorAdapter {

    public AutoCompleteCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
        super(context, layout, c, from, to);
    }

    @Override
    public CharSequence convertToString(Cursor cursor) {
        // This is the method that does the trick (return the String you need)
        return cursor.getString(cursor.getColumnIndex("name"));
    }
}
于 2012-03-06T15:21:55.433 回答
0

当我从列表中进行选择时,我在文本视图中得到一个 sqlite 对象('android.database.sqlite.SQLiteCursor@'...)。

您没有说这个“textview”是什么或它与Spinner.

我将进行有根据的猜测,并假设您只是将所选项目SpinnerTextView.

Spinner使用 a从 a 中选择的项目SimpleCursorAdapter是 a Cursor,指向用户选择的行。toString() 的实现Cursor会给你一些类似于android.database.sqlite.SQLiteCursor@取决于来自哪里的东西Cursor

更有可能的是,您将要调用getString()Cursor,以检索一些列值,并将其分配给有TextView问题的。

于 2010-01-29T11:38:33.650 回答