Java 是否使用多个微调器筛选listview?

Java 是否使用多个微调器筛选listview?,java,android,arrays,string,listview,Java,Android,Arrays,String,Listview,此代码是一个显示练习[]字符串中项目的列表视图。您还可以使用EditTextinputSearch和charsequence搜索这些项目,它通过比较字符串来检测选择的列表项目 我需要的是: 使用多个微调器过滤listview(字符串练习[])的方法 完整代码: public class CalExercises extends Activity { private ListView myListView; ArrayAdapter<String> myAdapter;

此代码是一个显示
练习[]
字符串中项目的
列表视图。您还可以使用
EditText
inputSearch和charsequence搜索这些项目,它通过比较字符串来检测选择的列表项目

我需要的是: 使用多个微调器过滤listview(字符串练习[])的方法

完整代码:

public class CalExercises extends Activity {
    private ListView myListView;
    ArrayAdapter<String> myAdapter;
    EditText inputSearch;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calexercise);
        String exercises[] = {"Wall Pushup", "Knees Pushup", "Regular Pushup", "Pullup"};
        myListView = (ListView) findViewById(R.id.exp_list);
        inputSearch = (EditText) findViewById(R.id.itemSearch);
        myAdapter = new ArrayAdapter<String>(this, R.layout.parent_layout, R.id.parent_txt, exercises);
        myListView.setAdapter(myAdapter);
        myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

                String PosItem = (String) arg0.getItemAtPosition(arg2);

                Intent itemIntent = new Intent(getBaseContext(), TempCalEx.class);
                String exIntent = "";

                if ("Wall Pushup".equals(PosItem))
                    exIntent = "wallpushup";
                else if ("Knees Pushup".equals(PosItem))
                    exIntent = "kneepushup";
                else if ("Regular Pushup".equals(PosItem)) {
                    exIntent = "regpushup";
                    Toast.makeText(CalExercises.this, "List Detect Test Success - Pushup", Toast.LENGTH_LONG).show();
                } else if ("Pullup".equals(PosItem)) {
                    exIntent = "pullup";
                    Toast.makeText(CalExercises.this, "List Detect Test Success - Pullup", Toast.LENGTH_LONG).show();
                }

                itemIntent.putExtra("exString", exIntent);
                startActivity(itemIntent);


                ;
                inputSearch.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                        CalExercises.this.myAdapter.getFilter().filter(cs);
                    }

                    @Override
                    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                                  int arg3) {
                    }

                    @Override
                    public void afterTextChanged(Editable arg0) {
                    }
                });


            }

            ;

        });

    }}
公共类CalExercises扩展活动{
私有列表视图myListView;
阵列适配器;
编辑文本输入搜索;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u calexercise);
弦乐练习[]={“墙式俯卧撑”、“膝盖俯卧撑”、“常规俯卧撑”、“拉起”};
myListView=(ListView)findViewById(R.id.exp\u列表);
inputSearch=(EditText)findViewById(R.id.itemSearch);
myAdapter=new ArrayAdapter(这个,R.layout.parent\u布局,R.id.parent\u txt,练习);
myListView.setAdapter(myAdapter);
myListView.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
公共视图单击(AdapterView arg0、视图arg1、整型arg2、长型arg3){
字符串PosItem=(字符串)arg0.getItemAtPosition(arg2);
Intent itemIntent=newintent(getBaseContext(),TempCalEx.class);
字符串excontent=“”;
如果(“墙壁上推”。等于(正数))
exIntent=“wallpushup”;
否则,如果(“膝盖俯卧撑”。等于(正数))
exIntent=“膝盖俯卧撑”;
否则,如果(“常规俯卧撑”。等于(正数)){
exIntent=“regpushup”;
Toast.makeText(CalExercises.this,“列表检测测试成功-俯卧撑”,Toast.LENGTH\u LONG.show();
}否则,如果(“向上拉”。等于(正数)){
exIntent=“上拉”;
Toast.makeText(CalExercises.this,“列表检测测试成功-上拉”,Toast.LENGTH\u LONG.show();
}
itemIntent.putExtra(“exString”,ExContent);
startActivity(itemIntent);
;
inputSearch.addTextChangedListener(新的TextWatcher(){
@凌驾
public void onTextChanged(字符序列cs、int arg1、int arg2、int arg3){
CalExercises.this.myAdapter.getFilter().filter(cs);
}
@凌驾
更改前的公共void(字符序列arg0、int arg1、int arg2、,
int arg3){
}
@凌驾
public void PostTextChanged(可编辑arg0){
}
});
}
;
});
}}

首先,我建议使用此代码而不是复制粘贴;)

在编辑器中编写代码时,我还没有对其进行测试,但它应该做完全相同的工作。它更容易阅读,并且由于使用了else-if,应该会稍微提高性能

另外,如果您试图比较字符串,则始终在您知道的对象上调用equals,它将永远不会为null

关于你原来的问题:

显示将显示哪些子字符串的方法

我不明白你想展示什么。是否要筛选列表,或者如果设置了筛选器,只需在某些单击时作出反应?你能详细说明一下吗

编辑

这应该行得通

// setting up the map, you might have to add more.
Map <String, String[]> map = new Map<String, String[]>();

map.put("normal",new String[]{"Wall Pushup", "Knees Pushup", "Regular Pushup","Pullup"}); 
map.put("beginner",new String[]{"Knees Pushup","Wall Pushup"});

// this would be the spinner that selects the "normal", "beginner" etc
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
        String[] content = map.get(spinner.getSelectedItem().toString());
        if (content = null)
            myListView.setAdapter(new ArrayAdapter<String>(this, R.layout.parent_layout, R.id.parent_txt, content));
    }

    @Override
    public void onNothingSelected(AdapterView<?> parentView) {
    }

});
//设置地图时,可能需要添加更多内容。
Map Map=newmap();
map.put(“正常”,新字符串[]{“墙壁俯卧撑”,“膝盖俯卧撑”,“常规俯卧撑”,“拉起”});
放置(“初学者”,新字符串[]{“膝盖俯卧撑”,“墙壁俯卧撑”});
//这将是选择“正常”、“初学者”等的微调器
spinner.setOnItemSelectedListener(新的OnItemSelectedListener(){
@凌驾
已选择公共视图(AdapterView父视图、视图selectedItemView、整型位置、长id){
String[]content=map.get(spinner.getSelectedItem().toString());
if(content=null)
setAdapter(新的ArrayAdapter(this,R.layout.parent_layout,R.id.parent_txt,content));
}
@凌驾
未选择公共无效(AdapterView父视图){
}
});
但是,您可能需要向映射中添加更多数组,因为我不知道可能发生的所有状态

并使用适当的字符串作为键,如果微调器显示“初学者”,则必须使用put(key,value)和该确切文本作为键,否则它将始终为null。我建议您使用自定义适配器,因为您的arrayadapter选项有限,但情况不同

编辑2:

公共类CalExercises扩展活动{
私有列表视图myListView;
阵列适配器;
编辑文本输入搜索;
列出练习;
//TODO设置为2
纺纱机;
纺纱机;
字符串[]数组1;
字符串[]数组2;
Map map1=新的HashMap();
Map map2=新的HashMap();
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u calexercise);
练习=新的ArrayList(Arrays.asList(新字符串[]{“墙上推”、“膝盖上推”、“常规上推”、“上拉”}));
myListView=(ListView)findViewById(R.id.exp\u列表);
inputSearch=(EditText)findViewById(R.id.itemSearch);
spindif=(微调器)findViewById((R.id.filterDif));
spinmus=(微调器)findViewById((R.id.filterMus));
myAdapter=new ArrayAdapter(这个,R.layout.parent\u布局,R.id.parent\u txt,练习);
myListView.setAdapter(myAdapter);
myListView.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
公共视图单击(AdapterView arg0、视图arg1、整型arg2、长型arg3){
字符串PosItem=(字符串)arg0.getItemAtPosition(arg2);
// setting up the map, you might have to add more.
Map <String, String[]> map = new Map<String, String[]>();

map.put("normal",new String[]{"Wall Pushup", "Knees Pushup", "Regular Pushup","Pullup"}); 
map.put("beginner",new String[]{"Knees Pushup","Wall Pushup"});

// this would be the spinner that selects the "normal", "beginner" etc
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
        String[] content = map.get(spinner.getSelectedItem().toString());
        if (content = null)
            myListView.setAdapter(new ArrayAdapter<String>(this, R.layout.parent_layout, R.id.parent_txt, content));
    }

    @Override
    public void onNothingSelected(AdapterView<?> parentView) {
    }

});
public class CalExercises extends Activity {
private ListView myListView;
ArrayAdapter<String> myAdapter;
EditText inputSearch;

List<String> exercises;

//TODO set those 2
Spinner spindif;
Spinner spinmus;

String[] array1;
String[] array2;

Map <String, String[]> map1 = new HashMap<String, String[]>();
Map <String, String[]> map2 = new HashMap<String, String[]>();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_calexercise);

    exercises = new ArrayList<String>(Arrays.asList(new String[]{"Wall Pushup", "Knees Pushup", "Regular Pushup", "Pullup"}));

    myListView = (ListView) findViewById(R.id.exp_list);
    inputSearch = (EditText) findViewById(R.id.itemSearch);
    spindif = (Spinner) findViewById((R.id.filterDif)); 
    spinmus = (Spinner) findViewById((R.id.filterMus)); 

    myAdapter = new ArrayAdapter<String>(this, R.layout.parent_layout, R.id.parent_txt, exercises);
    myListView.setAdapter(myAdapter);
    myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

            String PosItem = (String) arg0.getItemAtPosition(arg2);

            Intent itemIntent = new Intent(getBaseContext(), TempCalEx.class);
            String exIntent = "";

            if ("Wall Pushup".equals(PosItem))
                exIntent = "wallpushup";
            else if ("Knees Pushup".equals(PosItem))
                exIntent = "kneepushup";
            else if ("Regular Pushup".equals(PosItem)) {
                exIntent = "regpushup";
                Toast.makeText(CalExercises.this, "List Detect Test Success - Pushup", Toast.LENGTH_LONG).show();
            } else if ("Pullup".equals(PosItem)) {
                exIntent = "pullup";
                Toast.makeText(CalExercises.this, "List Detect Test Success - Pullup", Toast.LENGTH_LONG).show();
            }

            itemIntent.putExtra("exString", exIntent);
            startActivity(itemIntent);

            inputSearch.addTextChangedListener(new TextWatcher() {
                @Override
                public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                    CalExercises.this.myAdapter.getFilter().filter(cs);
                }

                @Override
                public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                              int arg3) {
                }

                @Override
                public void afterTextChanged(Editable arg0) {
                }
            });


        }

        ;

    });

    // this would be the spinner that selects the "normal", "beginner" etc
    spindif.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
            array1 = map1.get(spinner1.getSelectedItem().toString());
            updateList();
        }

        @Override
        public void onNothingSelected(AdapterView<?> parentView) {
        }

    });

    spinmus.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
            array2 = map2.get(spinner2.getSelectedItem().toString());
            updateList();
        }

        @Override
        public void onNothingSelected(AdapterView<?> parentView) {
        }

    });
}

public void updateList() {
    List<Integer> aList =  Arrays.asList(array1);
    List<Integer> bList =  Arrays.asList(array2);
    aList.retainAll(bList);
    exercises = aList;
    myAdapter.notifyDataSetChanged();
}
}