0

您好,我是 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()”未定义。

4

1 回答 1

0

绝对不是有史以来最好的代码,但你可以看看我的实现,它运行得很好:

第 252 行:https ://github.com/iRail/BeTrains-for-Android/blob/master/src/tof/cv/mpp/StationPickerActivity.java

还有这个类:https ://github.com/iRail/BeTrains-for-Android/blob/master/src/tof/cv/mpp/Utils/FilterTextWatcher.java

于 2012-07-23T19:12:58.030 回答