如何为ListView创建过滤器?

时间:2014-02-10 23:48:57

标签: android listview filter

我正在开发一个应用程序,我希望过滤ListView中的项目,但是项目ID被定义为来自另一个类(构造函数)的字符串,并且过滤器不起作用。 (没有任何反应)

我希望过滤器从构造函数中搜索这些字符串,但我不知道如何。

这是我的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;
    }
}

3 个答案:

答案 0 :(得分:0)

  

请看这个链接..

Here is good example

答案 1 :(得分:0)

  

请删除

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                          
                }
            });
  从MyListAdapter

并将其放在oncreate方法中,并在过滤器使用中   你的条件无论你想要什么。它还没有解决,然后让我知道。

答案 2 :(得分: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;
    }
}