添加编辑文本以搜索ListView中的项目

时间:2012-07-23 19:07:33

标签: android list listview search

您好我是Android新手,我正在尝试添加搜索字段以搜索列表视图中的项目。我尝试了几种方法,包括TextWatcher,但我无法使其成功运行

package com.example.permission;

import java.util.ArrayList;
import java.util.Collections;

import android.app.Activity;
import android.content.pm.ApplicationInfo;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

public class MainActivity extends Activity {
    private ListView mListAppInfo;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // set layout for the main screen
        setContentView(R.layout.layout_main);

        // load list application
        mListAppInfo = (ListView)findViewById(R.id.lvApps);
        EditText search = (EditText)findViewById(R.id.EditText01);

        mListAppInfo.setTextFilterEnabled(true);

        // create new adapter
        AppInfoAdapter adapter = new AppInfoAdapter(this, Utilities.getSystemFilteredApplication(this), getPackageManager());

        // set adapter to list view  
        mListAppInfo.setAdapter(adapter);

        TextWatcher filterTextWatcher = new TextWatcher() {

            public void afterTextChanged(Editable s) {

            }

            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
            }

            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
               adapter.getFilter().filter(s); //Filter from my adapter
               adapter.notifyDataSetChanged(); //Update my view
            }

        };

        // implement event when an item on list view is selected
        mListAppInfo.setOnItemClickListener(new OnItemClickListener() {

            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);
                Utilities.getPermissions(parent.getContext(), getPackageManager(), appInfo.packageName);
                //Toast.makeText(MainActivity.this, "You have clicked on package: " + appInfo.packageName, Toast.LENGTH_SHORT).show();
            }
        });


    }
}
编辑:尝试实现你的例子,但似乎无法正确,我无法在我的

上使用getFilter()方法
AppInfoAdapter adapter = new AppInfoAdapter(this, Utilities.getSystemFilteredApplication(this), getPackageManager());

对于AppInfoAdapter类型,方法'getFilter()'未定义。

1 个答案:

答案 0 :(得分:0)