按输入键过滤的自动完成文本视图

发布于 2024-09-01 18:44:59 字数 2840 浏览 5 评论 0原文

我使用 autocompletetextview 和 SimpleCursorAdapter 从 sqlite 获取数据。我想通过输入的键启动其下拉列表。在我的自动完成文本视图中,列表未显示或按输入文本过滤。

例如, 如果用户输入“an”,则所有以“an”开头的文本都会在此列表中看到。

在Java中

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.new);

        txtPNo = (AutoCompleteTextView) findViewById(R.id.txtSTo);        

        mDbHelper = new DBAdapter(this);
        mDbHelper.open();

        SimpleCursorAdapter notes = fillToData();
        txtPNo.setAdapter(notes); 
}

    private SimpleCursorAdapter fillToData() {
        Cursor c = mDbHelper.getName();
        startManagingCursor(c);
        String[] from = new String[] {DBAdapter.Name,DBAdapter.No1};
        int[] to = new int[] {R.id.txtName,R.id.txtNo1};

        Log.d(TAG, "cursor.getCount()=" + c.getCount());
        SimpleCursorAdapter notes =
            new SimpleCursorAdapter(this, R.layout.autocomplete, c, from,
to);

        return notes;
    } 

在new.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="#ffffff"
    >
   <AutoCompleteTextView android:id="@+id/txtSTo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:textColor="#000000"     
        android:hint="To"       
        android:completionThreshold="1" 
        android:selectAllOnFocus="true"  
        android:layout_alignParentTop = "true"
        android:layout_alignParentLeft="true"/>       
</RelativeLayout>

在autocomplete.xml中

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:stretchColumns="0" android:padding="5dp"> 
    <TableRow android:padding="5dp">
        <LinearLayout android:orientation="vertical">
            <TextView android:id="@+id/txtName"
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:textColor="#000000"   
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/> 
            <TextView android:id="@+id/txtNo1"
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:textColor="#000000"   
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/> 
        </LinearLayout>
    </TableRow>
</TableLayout>

如何实现获取刚刚过滤的列表?

I use autocompletetextview with SimpleCursorAdapter to get data from sqlite. I'd like to get its drop down list started by the entered key. In my autocompletetextview, the list is not shown or filtered by input text.

eg,
If user enter "an", all text started with "an" will be seen in this list.

In Java

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.new);

        txtPNo = (AutoCompleteTextView) findViewById(R.id.txtSTo);        

        mDbHelper = new DBAdapter(this);
        mDbHelper.open();

        SimpleCursorAdapter notes = fillToData();
        txtPNo.setAdapter(notes); 
}

    private SimpleCursorAdapter fillToData() {
        Cursor c = mDbHelper.getName();
        startManagingCursor(c);
        String[] from = new String[] {DBAdapter.Name,DBAdapter.No1};
        int[] to = new int[] {R.id.txtName,R.id.txtNo1};

        Log.d(TAG, "cursor.getCount()=" + c.getCount());
        SimpleCursorAdapter notes =
            new SimpleCursorAdapter(this, R.layout.autocomplete, c, from,
to);

        return notes;
    } 

In new.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="#ffffff"
    >
   <AutoCompleteTextView android:id="@+id/txtSTo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:textColor="#000000"     
        android:hint="To"       
        android:completionThreshold="1" 
        android:selectAllOnFocus="true"  
        android:layout_alignParentTop = "true"
        android:layout_alignParentLeft="true"/>       
</RelativeLayout>

In autocomplete.xml

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:stretchColumns="0" android:padding="5dp"> 
    <TableRow android:padding="5dp">
        <LinearLayout android:orientation="vertical">
            <TextView android:id="@+id/txtName"
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:textColor="#000000"   
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/> 
            <TextView android:id="@+id/txtNo1"
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:textColor="#000000"   
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/> 
        </LinearLayout>
    </TableRow>
</TableLayout>

How to implement to get just filtered list?

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

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

发布评论

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

评论(1

叫思念不要吵 2024-09-08 18:44:59
    txtPNo = (AutoCompleteTextView) findViewById(R.id.txtSTo);        

    mDbHelper = new DBAdapter(this);
    mDbHelper.open();

    SimpleCursorAdapter notes = fillToData();
    txtPhoneNo.setAdapter(notes); 

嗯,如果你只是复制粘贴你的代码......

问题是变量名。

txtPNo != txtPhoneNo


AutoCompleteTextView

    txtPNo = (AutoCompleteTextView) findViewById(R.id.txtSTo);        

    mDbHelper = new DBAdapter(this);
    mDbHelper.open();

    SimpleCursorAdapter notes = fillToData();
    txtPhoneNo.setAdapter(notes); 

Umm, if you just copy-n-pasted your code...

The issue is the variable name.

txtPNo != txtPhoneNo


AutoCompleteTextView

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