AutoCompleteTextView显示“android.database.sqlite.SQLiteCursor @”......让选择后 [英] AutoCompleteTextView displays 'android.database.sqlite.SQLiteCursor@'... after making selection

查看:172
本文介绍了AutoCompleteTextView显示“android.database.sqlite.SQLiteCursor @”......让选择后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的code到一个AutoCompleteTextView

I am using the following code to set the adapter (SimpleCursorAdapter) for an AutoCompleteTextView

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

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"/>

和THI是XML实际控制

and thi is the xml for the actual control

<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 @......)在TextView中。 任何人都知道什么会导致此,或如何解决?

The dropdown appears to work correctly, and shows a list of items. When I make a selection from the list I get a sqlite object ('android.database.sqlite.SQLiteCursor@'... ) in the textview. Anyone know what would cause this, or how to resolve this?

感谢

确定我能够挂钩到OnItemClick事件,但AutoCompleteTextView部件的TextView.setText()部分该点之后被更新。该OnItemSelected()事件永远不会被解雇,而onNothingSelected()首先显示的下拉列表中的项目,当事件被解雇了。

Ok I am able to hook into the OnItemClick event, but the TextView.setText() portion of the AutoCompleteTextView widget is updated after this point. The OnItemSelected() event never gets fired, and the onNothingSelected() event gets fired when the dropdown items are first displayed.

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

    });

任何ALSE对如何重写的TextView的更新任何想法?

Anyone alse have any ideas on how to override the updating of the TextView?

感谢

帕特里克

推荐答案

我不认为你应该有更新的文本AutoCompleteTextView。它应该会自动做。它通过调用[CursorAdapter.convertToString(...)[1]的方法做到这一点。如果你读的方法的描述,指出了这一点。所以,如果你正在写你自己的CursorAdapter你会重写该方法返回的任何文字你想显示在建议列表。这家伙不解释如何做到这一点的好工作:

线86 - <一个href="http://thinkandroid.word$p$pss.com/2010/02/08/writing-your-own-autocompletetextview/">http://thinkandroid.word$p$pss.com/2010/02/08/writing-your-own-autocompletetextview/

I don't think you should have to update the text for the AutoCompleteTextView. It should do it automatically. It does this by calling the [CursorAdapter.convertToString(...)][1] method. if you read the description of the method it points this out. So if you were writing your own CursorAdapter you would override that method to return whatever text you would want to show up in the list of suggestions. This guy does a good job of explaining how to do it:

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

不过,由于您使用的是SimpleCursorAdapter,不能覆盖这个方法。相反,你需要实现/创建[SimpleCursorAdapter.CursorToStringConverter] [2],并把它传递到[SimpleCursorAdapter.setCursorToStringConverter(...)] [3]:

However, since you are using a SimpleCursorAdapter, you can't override this method. Instead you need implement/create a [SimpleCursorAdapter.CursorToStringConverter][2] and pass it into [SimpleCursorAdapter.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为空:

Or if you don't want to create a CursorToStringConverter then use the [SimpleCursorAdapter. setStringConversionColumn(...)][4] method. But I think you still have to explicitly set the CursorToStringConverter to null:

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

抱歉,该垃圾邮件拦截器不会让我上传到Android文档描述我张贴上面的链接的链接。但快速谷歌搜索将指向您正确的文档页面。

Sorry, but the spam blocker won't let me post the links to the Android Documentation that describes the links I posted above. But a quick google search will point you to the correct doc pages.

这篇关于AutoCompleteTextView显示“android.database.sqlite.SQLiteCursor @”......让选择后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆