2012-07-23 72 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