2012-07-23 68 views
0

你好,我是新来的android和我想添加一个搜索字段来搜索列表视图中的项目。我已经尝试了几种方法,包括TextWatcher但我无法得到它的成功合作添加编辑文本来搜索ListView中的项目

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(); 
      } 
     }); 


    } 
} 

编辑:努力实现您的例子,但似乎无法得到它的权利,我无法使用在我的

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

方法 '用getFilter()' 用getFilter()方法是未定义的类型AppInfoAdapter。

+0

可能重复[我有一个使用自定义ArrayList适配器的ListView - 实现过滤的最佳方式是什么?任何人都有一个示例代码来研究?](http://stackoverflow.com/questions/3071368/i-have-a-listview-using-a-custom-arraylist-adapter-whats-the-best-way-to-to- impl) – Sam 2012-07-23 19:09:20

回答

0
+0

试图根据你的例子来实现,但仍然无法正常工作编辑代码在主帖 – dythe 2012-07-23 19:35:05

+0

你没有尝试根据我的例子;-)请参阅:search.addTextChangedListener(filterTextWatcher); getListView()setTextFilterEnabled(真)。 – 2012-07-23 20:56:30

+0

这是我目前的MainActivity.java,http://pastebin.com/BJWiWgcd,但由于某些原因它仍然无法正常工作。 – dythe 2012-07-23 21:48:40