以编程方式搜索listview以获取应用名称

时间:2013-11-20 04:44:41

标签: android listview android-listview android-edittext layout-inflater

您好我正在开发一款Android搜索应用。我将以编程方式在列表视图中显示手机上安装的所有应用程序名称,如下所示。

private void loadApps()
{
    Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
    mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);

    PackageManager manager = getPackageManager();

    final List<ResolveInfo> mApps = getPackageManager().queryIntentActivities(mainIntent, 0);
    Collections.sort(mApps, new ResolveInfo.DisplayNameComparator(manager));

    mListView = getListView();
    mListView.setAdapter(new AppsAdapter(this, mApps));
}

我的BaseAdapter如下所示

public class AppsAdapter extends BaseAdapter 
{
    TextView textLable;
    private LayoutInflater inflater;
    private List<ResolveInfo> mApps;

    public AppsAdapter(Context context, List<ResolveInfo> mApps) 
    {
        this.inflater = LayoutInflater.from(context);
        this.mApps = mApps;
    }

     public View getView(int position, View convertView, ViewGroup parent) 
    {
        ViewHandler handler;
        if (convertView == null)
        {
            convertView = inflater.inflate(R.layout.easeallapps_applist, null);
            handler = new ViewHandler();
            handler.textLable = (TextView)convertView.findViewById(R.id.tvName);
            convertView.setTag(handler);
        } 
        else 
        {
            handler = (ViewHandler)convertView.getTag();
        }
        ResolveInfo info = this.mApps.get(position);
        handler.textLable.setText(info.loadLabel(getPackageManager()));
        return convertView;

    }
    class ViewHandler
    {
        TextView textLable;
    }

我想在此列表视图的顶部添加Search EditText以搜索应用。我不知道该怎么做。请帮忙。谢谢!

3 个答案:

答案 0 :(得分:1)

TextWatcher watcher = new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before,
            int count) {
        loadApps(s);

    }

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

    }

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

    }
};

我希望这篇文章对你有用。

答案 1 :(得分:1)

我为您找到了解决方案,您需要一步一步地阅读并在项目中实施。

下面是

的完整教程链接

Adding Search Functionality to ListView

enter image description here

这是代码片段。

<强> MainActivity.java

package com.androidhive.androidlistviewwithsearch;
 
import java.util.ArrayList;
import java.util.HashMap;
 
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
 
public class MainActivity extends Activity {
     
    // List view
    private ListView lv;
     
    // Listview Adapter
    ArrayAdapter<String> adapter;
     
    // Search EditText
    EditText inputSearch;
     
     
    // ArrayList for Listview
    ArrayList<HashMap<String, String>> productList;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         
        // Listview Data
        String products[] = {"Dell Inspiron", "HTC One X", "HTC Wildfire S", "HTC Sense", "HTC Sensation XE",
                                "iPhone 4S", "Samsung Galaxy Note 800",
                                "Samsung Galaxy S3", "MacBook Air", "Mac Mini", "MacBook Pro"};
         
        lv = (ListView) findViewById(R.id.list_view);
        inputSearch = (EditText) findViewById(R.id.inputSearch);
         
        // Adding items to listview
        adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, products);
        lv.setAdapter(adapter);
         
        /**
         * Enabling Search Filter
         * */
        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                         
            }
        });
    }   
}

答案 2 :(得分:0)

获取mApps列表中的所有appname后,修改/删除mApps列表中与edittext测试不匹配的名称..

获得最终列表后,在您的AppsAdapter类中为mApps创建一个 setter ..并从您的活动类中设置mApps的数据并调用以下内容

mListView.notifyDataSetChanged();