将搜索功能添加到listview

时间:2013-10-13 23:46:16

标签: android android-listview

我已经在滑动抽屉中创建了用户安装的应用程序的列表视图,我想为其添加搜索功能。我正在按照教程here

到目前为止,这是我的编码(包括迄今为止实现的搜索功能): Drag_and_Drop_App.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<SlidingDrawer
    android:id="@+id/bottom"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerVertical="true"
    android:orientation="horizontal"
    android:layout_marginTop="200dp"
    android:content="@+id/content"
    android:handle="@+id/handle" >

    <Button
        android:id="@+id/handle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Handle" />

    <LinearLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:orientation="vertical"
        android:background="#00FF00">

            <!-- Editext for Search -->
<EditText android:id="@+id/inputSearch"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="Search applications.."
    android:inputType="textVisiblePassword"/>

<ListView
    android:id="@+id/lvApps"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"       
/>


    </LinearLayout>
</SlidingDrawer>

</RelativeLayout>

Drag_and_Drop_App.java:

package com.example.awesomefilebuilderwidget;

IMPORTS

public class Drag_and_Drop_App extends Activity {
private ListView mListAppInfo;
// Search EditText
EditText inputSearch;


 @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // set layout for the main screen
    setContentView(R.layout.drag_and_drop_app);
    // search bar
    inputSearch = (EditText) findViewById(R.id.inputSearch);
     // load list application
    mListAppInfo = (ListView)findViewById(R.id.lvApps);
    // create new adapter
    AppInfoAdapter adapter = new AppInfoAdapter(this, Utilities.getInstalledApplication(this), getPackageManager());
    // set adapter to list view
    mListAppInfo.setAdapter(adapter);
    // implement event when an item on list view is selected
    mListAppInfo.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView parent, View view, int pos, long id) {
            // get the list adapter
            AppInfoAdapter appInfoAdapter = (AppInfoAdapter)parent.getAdapter();
            // get selected item on the list
            ApplicationInfo appInfo = (ApplicationInfo)appInfoAdapter.getItem(pos);
            // launch the selected application
            Utilities.launchApp(parent.getContext(), getPackageManager(), appInfo.packageName);
        }


    });
}
}

我不确定在哪里编码:

inputSearch.addTextChangedListener(new TextWatcher() {

@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
    // When user changed the Text
    MainActivity.this.adapter.getFilter().filter(cs);   
}

@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
        int arg3) {
    // TODO Auto-generated method stub

}

@Override
public void afterTextChanged(Editable arg0) {
    // TODO Auto-generated method stub                          
}
});

如果您需要查看我的任何其他编码(例如应用列表视图的任何适配器),请告诉我们!

0 个答案:

没有答案