为什么我的ListView在调用setContentView后不再过滤文本?

时间:2012-01-12 09:02:18

标签: android listview filter

活动的完整代码:

package com.stevovo.myactivity;

import java.util.ArrayList;
import java.util.EmptyStackException;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckedTextView;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.ListView;

public class myactivityselect extends Activity {

ArrayList<String> mCheckedItems;
MyListView lv;
ExpandableListView elv;
Activity a;
Button tv, back1, back2;
MySimpleExpandableList simpleList; //custom expandable list view adapter
MySimpleGroup grp; //just a class to manage parents and children
boolean firstRun;


public void onCreate(Bundle savedInstanceState) {
    simpleList =  new MySimpleExpandableList(myactivityselect .this);
    super.onCreate(savedInstanceState);
    a = this;
    firstRun = true;
    changeView(R.layout.ingr_selectview);
}

public void onPause() {
    myactivitydb.db.saveInventoryItems(mCheckedItems); //save checked items in database
    super.onPause();
}

public MySimpleGroup ComputeIngredients() {
    MySimpleGroup grp = new MySimpleGroup();
    Ingredient[] ingrs = myactivitydb.db.getIngredients(lv.mCheckedItems);
    String[] currentEffects;
    String mainIngredient;
    String[] sMatchingIngredients;
    ArrayList<String> matchingIngredients;
    for (int i = 0; i < ingrs.length; i++) {
        mainIngredient = ingrs[i].name;
        currentEffects = new String[4];
        currentEffects[0] = ingrs[i].effects[0];
        currentEffects[1] = ingrs[i].effects[1];
        currentEffects[2] = ingrs[i].effects[2];
        currentEffects[3] = ingrs[i].effects[3];
        matchingIngredients = new ArrayList<String>();
        for (int x = 0; x < ingrs.length; x++) {
            if (mainIngredient.equals(ingrs[x].name)) {
                continue;
            }
            for (int y = 0; y < 4; y++) {
                if (currentEffects[y].equals(ingrs[x].effects[y])) {
                    matchingIngredients.add(ingrs[x].name);
                    break;
                }
            }
        }
        sMatchingIngredients = new String[matchingIngredients.size()];
        for (int x = 0; x < matchingIngredients.size(); x++) {
            sMatchingIngredients[x] = matchingIngredients.get(x).toString();
        }
        if (matchingIngredients.size() > 0) {
            grp.addGroup(mainIngredient, sMatchingIngredients);
        }
    }
    return grp;

}

public void changeView(int layoutResID) {
    switch (layoutResID) {
    case R.layout.ingr_selectview:
            a.setContentView(layoutResID);
            lv = (MyListView) a.findViewById(R.id.list_IngrSelect);
            lv.setTextFilterEnabled(true);
            if (firstRun) {
                lv.mCheckedItems = myactivitydb.db.getSavedInventoryItems();
                firstRun = false;
            } else if (this.mCheckedItems != null) {
                lv.mCheckedItems = this.mCheckedItems;
            }
            tv = (Button) a.findViewById(R.id.btnShowPotions);
            tv.setText("Show Available Potions");
            tv.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    if (lv.mCheckedItems.size() > 0) {
                        MySimpleGroup grp = ComputeIngredients();
                        simpleList.clear();
                        simpleList.addSimpleGroup(grp);
                        simpleList.showChildCount(true);
                        mCheckedItems = lv.mCheckedItems;
                        changeView(R.layout.ingr_selectview_ingrs);
                    }
                }
            });

            class MyCheckedTextView extends CheckedTextView {
                public boolean checked;

                public MyCheckedTextView(Context context) {

                    super(context);
                }

            }

            class MyArrayAdapter<T> extends ArrayAdapter<T> {

                Context context;

                public MyArrayAdapter(Context context, int textViewResourceId) {
                    super(context, textViewResourceId);
                    this.context = context;
                }

                public View getView(int position, View convertView, ViewGroup parent) {

                    MyCheckedTextView ctv = new MyCheckedTextView(this.context);
                    ctv.setText((String) getItem(position).toString());
                    ctv.checked = lv.isItemChecked((String) ctv.getText());
                    if (ctv.checked) {
                        ctv.setCheckMarkDrawable(android.R.drawable.button_onoff_indicator_on);
                    } else {
                        ctv.setCheckMarkDrawable(android.R.drawable.button_onoff_indicator_off);
                    }
                    ctv.setOnClickListener(new OnClickListener() {


                        public void onClick(View v) {
                            MyCheckedTextView view = (MyCheckedTextView) v;
                            if (!view.checked) {
                                view.checked = true;
                                view.setCheckMarkDrawable(android.R.drawable.button_onoff_indicator_on);
                                lv.addCheckedItem((String) view.getText());
                            } else {
                                view.checked = false;
                                view.setCheckMarkDrawable(android.R.drawable.button_onoff_indicator_off);
                                lv.removeCheckedItem((String) view.getText());

                            }
                        }
                    });
                    return ctv;
                }           
            }
            MyArrayAdapter<String> arr = new MyArrayAdapter<String>(this, android.R.layout.simple_list_item_checked);
            myactivitydb.db.ingredient_list_array(arr);
            lv.setAdapter(arr);
            lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        break;

    case R.layout.ingr_selectview_ingrs:
            a.setContentView(layoutResID);
            elv = (ExpandableListView) a.findViewById(R.id.list_ingrSelectIngrs);
            elv.setAdapter(simpleList);

            back1 = (Button) a.findViewById(R.id.btnBack);
            back1.setText("Go Back");
            back1.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    changeView(R.layout.ingr_selectview);
                }
            });
        break;
    }
}

}

我有一个方法changeView,它使用setContentView在两个不同的视图之间切换。一切正常,但初始列表视图(MyListView lv)文本过滤器仅在启动活动时起作用。

调用changeView方法后,我的lv列表视图文本过滤器不再有效。谁能告诉我为什么?

如果需要更多信息,请与我们联系。

由于

0 个答案:

没有答案