2014-02-10 70 views
0

我正在開發一個應用程序,並且我想過濾ListView中的項目,但項目ID被定義爲來自另一個類(構造函數)的字符串,並且過濾器不起作用。 (什麼也沒有發生)如何爲ListView製作過濾器?

我想過濾器從這些構造函數中搜索這些字符串,但我不知道如何去做。

這裏是我的mainActivity類:

package com.example.industrialcraft; 

import java.util.ArrayList; 
import java.util.List; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.text.Editable; 
import android.text.TextWatcher; 
import android.view.View; 
import android.view.ViewGroup; 
import android.view.Window; 
import android.view.WindowManager; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ListView; 
import android.widget.TextView; 

import com.example.R; 

public class Items extends Activity { 

    private List<Items_id> myItems = new ArrayList<Items_id>(); 
    ArrayAdapter<Items_id> adapter; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
     setContentView(R.layout.items_industrialcraft); 


     populateItems(); 
     adapter = new MyListAdapter(); 
     ListView list = (ListView) findViewById(R.id.list_industrialcraft); 
     list.setAdapter(adapter); 



     registerClickCallback(); 





    } 





    private void populateItems() { 

     myItems.add(new Items_id("Bronze Helmet", "Bronze_Helmet")); 
     myItems.add(new Items_id("Bronze Chestplate", "Bronze_Chestplate")); 
     myItems.add(new Items_id("Bronze Leggings", "Bronze_Leggings")); 
     myItems.add(new Items_id("Bronze Boots", "Bronze_Boots")); 
     myItems.add(new Items_id("Nano-Helmet", "Nano_Helmet")); 
     myItems.add(new Items_id("Nano-Bodyarmor", "Nano_Bodyarmor")); 
     myItems.add(new Items_id("Nano-Leggings", "Nano_Leggings")); 
     myItems.add(new Items_id("Nano-Boots", "Nano_Boots")); 
     myItems.add(new Items_id("QuantumSuit Helmet", "Quantumsuit_Helmet")); 
     myItems.add(new Items_id("QuantumSuit Bodyarmor", "Quantumsuit_Bodyarmor")); 
     myItems.add(new Items_id("QuantumSuit Leggings", "Quantumsuit_Leggings")); 
     myItems.add(new Items_id("QuantumSuit Boots", "Quantumsuit_Boots")); 
     myItems.add(new Items_id("Scuba Helmet", "Scuba_Helmet")); 
     myItems.add(new Items_id("Hazmat Suit", "Hazmat_Suit")); 
     myItems.add(new Items_id("Hazmat Suit Leggings", "Hazmat_Suit_Leggings")); 
     myItems.add(new Items_id("Rubber Boots", "Rubber_Boots")); 
     myItems.add(new Items_id("BatPack", "Batpack")); 
     myItems.add(new Items_id("CF Backpack", "CF_Backpack")); 
     myItems.add(new Items_id("Composite Vest", "Composite_Vest")); 
     myItems.add(new Items_id("Electric Jetpack", "Electric_Jetpack")); 
     myItems.add(new Items_id("Jetpack", "Jetpack")); 
     myItems.add(new Items_id("Lappack", "Lappack")); 
     myItems.add(new Items_id("Nightvision Goggles", "Nightvision_Goggles")); 
     myItems.add(new Items_id("Solar Helmet", "Solar_Helmet")); 
     myItems.add(new Items_id("Static Boots", "Statatic_Boots")); 
     myItems.add(new Items_id("Booze Barrel", "Booze_Barrel")); 
     myItems.add(new Items_id("Crop-Matron", "Crop_Matron")); 
     myItems.add(new Items_id("Crop", "Crop")); 
     myItems.add(new Items_id("Coffee Beans", "Coffee_Beans")); 
     myItems.add(new Items_id("Coffee Powder", "Coffee_Powder")); 
     myItems.add(new Items_id("Grin Powder", "Grin_Powder")); 
     myItems.add(new Items_id("Hydration Cell", "Hydration_Cell")); 
     myItems.add(new Items_id("Hops", "Hops")); 
     myItems.add(new Items_id("Stone Mug", "Stone_Mug")); 
     myItems.add(new Items_id("Coffee", "Coffee")); 
     myItems.add(new Items_id("Dark Coffee", "Dark_Coffee")); 
     myItems.add(new Items_id("Cropnalyzer", "Cropnalyzer")); 
     myItems.add(new Items_id("Electric Hoe", "Electric_Hoe")); 
     myItems.add(new Items_id("Fertilizer", "FErtilizer")); 
     myItems.add(new Items_id("Weed-EX", "Weed_EX")); 



    } 


    private class MyListAdapter extends ArrayAdapter<Items_id>{ 

     public MyListAdapter(){ 
      super(Items.this, R.layout.items_industrialcraft_buttons, myItems); 
     } 

     EditText search = (EditText) findViewById(R.id.search); 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      View itemView = convertView; 
      if(itemView == null){ 
       itemView = getLayoutInflater().inflate(R.layout.items_industrialcraft_buttons, parent, false); 
      } 

      // Find item to work with 

      Items_id currentItem = myItems.get(position); 

      search.addTextChangedListener(new TextWatcher() { 
       @Override 
       public void onTextChanged(CharSequence currentItem, int arg1, int arg2, int arg3) { 
        // When user changed the 

        Items.this.adapter.getFilter().filter(currentItem); 
       } 

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

      //Fill the view 
      TextView makeText = (TextView)itemView.findViewById(R.id.textId); 
      makeText.setText(currentItem.getId()); 
      return itemView; 
     } 



    } 

    private void registerClickCallback() { 
     ListView list = (ListView)findViewById(R.id.list_industrialcraft); 
     list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> parent, View viewClicked, int position, 
        long id) { 
       Items_id clicked = myItems.get(position); 

       //Find item 
       Items_id currentItem = myItems.get(position); 

       String abc = currentItem.getActivity(); 

       String activitys = abc; 

       try{ 
        Class classes = Class.forName("com.example.industrialcraft." + activitys); 
        Intent i = new Intent(Items.this, classes); 
        startActivity(i); 
       }catch(ClassNotFoundException e){ 
        e.printStackTrace(); 
       } 



      } 

     }); 
    } 







} 

這裏是我的Items_id類:

package com.example.industrialcraft; 

public class Items_id { 

    private String id; 
    private String activity; 

    public Items_id(String id, String activity){ 
     super(); 
     this.id = id; 
     this.activity = activity; 
    } 

    public String getId() { 
     return id; 
    } 

    public String getActivity() { 
     return activity; 
    } 
} 

回答

0

請訪問以下鏈接..

Here is good example

+0

這並不幫助,因爲我不將項目添加到一個只有意向,但我用一個數組創建多個活動和Intent,其中包含我想要運行的活動的名稱!這是一個問題!再次對不起英格蘭感到抱歉 – captainbuzz123

+0

請刪除EditText search =(EditText)findViewById(R.id.search); –

0

請從MyListAdapter刪除

EditText search = (EditText) findViewById(R.id.search); 

search.addTextChangedListener(new TextWatcher() { 
       @Override 
       public void onTextChanged(CharSequence currentItem, int arg1, int arg2, int arg3) { 
        // When user changed the 

        Items.this.adapter.getFilter().filter(currentItem); 
       } 

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

,並把它放在onCreate方法,並在過濾器的使用 你的條件無論你wants.It沒有解決尚未然後讓我知道。

+0

將嘗試,我讓你知道今天晚些時候,我不完全知道如何設置過濾條件。 – captainbuzz123

+0

它仍然無法正常工作。我不知道如何設置過濾器的條件,所以我請你解釋我或給我一些教程或鏈接我的東西,將解釋過濾器或給我一個提示。無論如何,感謝您的幫助! – captainbuzz123

+0

我已經爲您提供了一個鏈接(http://www.androidbegin.com/tutorial/android-search-listview-using-filter/),其中一切都很好定義和實施。仔細閱讀並實施它。如果不能實施,那就讓我知道。 –

0

下面是代碼:

項目類:

package com.example.industrialcraft; 

import java.util.ArrayList; 
import java.util.List; 
import java.util.Locale; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.text.Editable; 
import android.text.TextWatcher; 
import android.view.View; 
import android.view.ViewGroup; 
import android.view.Window; 
import android.view.WindowManager; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ListView; 
import android.widget.TextView; 

import com.example.R; 

public class Items extends Activity { 

    private List<Items_id> myItems = new ArrayList<Items_id>(); 
    private List<Items_id> arrayList = new ArrayList<Items_id>(); 
    ArrayAdapter<Items_id> adapter; 
    String activity[]; 
    String id[]; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
     setContentView(R.layout.items_industrialcraft); 


     populateItems(); 
     adapter = new MyListAdapter(); 
     ListView list = (ListView) findViewById(R.id.list_industrialcraft); 
     list.setAdapter(adapter); 

     final MyListAdapter fil = new MyListAdapter(); 
     String abcd = ""; 
     fil.filter(abcd); 
     final EditText search = (EditText) findViewById(R.id.search); 

     search.addTextChangedListener(new TextWatcher() { 
      @Override 
      public void onTextChanged(CharSequence a, int position, int arg2, int arg3) { 
       String text = search.getText().toString(); 
       fil.filter(text); 


      } 

      @Override 
      public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, 
        int arg3) { 
       // TODO Auto-generated method stub 

      } 

      @Override 
      public void afterTextChanged(Editable arg0) { 

      } 
     }); 

     registerClickCallback(); 





    } 





    private void populateItems() { 

     activity = new String[] {"Bronze_Helmet", "Bronze_Chestplate", "Bronze_Leggings"}; 

     id = new String[] {"Bronze Helmet", "Bronze Chestplate", "Bronze Leggings"}; 


     for (int i = 0; i < activity.length; i++) 
     { 
      Items_id wp = new Items_id(id[i], activity[i]); 
      myItems.add(wp); 
     } 




    } 




    private class MyListAdapter extends ArrayAdapter<Items_id>{ 

     public MyListAdapter(){ 
      super(Items.this, R.layout.items_industrialcraft_buttons, myItems); 
     } 



     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      View itemView = convertView; 
      if(itemView == null){ 
       itemView = getLayoutInflater().inflate(R.layout.items_industrialcraft_buttons, parent, false); 
      } 

      // Find item to work with 

      Items_id currentItem = arrayList.get(position); 



      //Fill the view 
      TextView makeText = (TextView)itemView.findViewById(R.id.textId); 
      makeText.setText(currentItem.getId()); 
      return itemView; 
     } 



     public void filter(String charText){ 
      arrayList.clear(); 
      if (charText.length() == 0) { 
       arrayList.addAll(myItems); 
      } 
      else 
      { 
       for (Items_id id : arrayList) 
       { 
        if (id.getId().contains(charText)) 
        { 
         arrayList.add(id); 
        } 
       } 
      }notifyDataSetChanged(); 
     } 


    } 




    private void registerClickCallback() { 
     ListView list = (ListView)findViewById(R.id.list_industrialcraft); 
     list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> parent, View viewClicked, int position, 
        long id) { 
       Items_id clicked = arrayList.get(position); 

       //Find item 
       Items_id currentItem = arrayList.get(position); 

       String abc = currentItem.getActivity(); 

       String activitys = abc; 

       try{ 
        Class classes = Class.forName("com.example.industrialcraft." + activitys); 
        Intent i = new Intent(Items.this, classes); 
        startActivity(i); 
       }catch(ClassNotFoundException e){ 
        e.printStackTrace(); 
       } 



      } 

     }); 
    } 







} 

Items_id類:

package com.example.industrialcraft; 

public class Items_id { 

    private String id; 
    private String activity; 

    public Items_id(String id, String activity){ 
     super(); 
     this.id = id; 
     this.activity = activity; 
    } 

    public String getId() { 
     return id; 
    } 

    public String getActivity() { 
     return activity; 
    } 
} 
相關問題