使用CheckBoxes进行Android自定义ListView搜索,过滤器无效

时间:2017-07-27 09:20:20

标签: android listview checkbox baseadapter

我正在开发Android应用程序,我有一个带复选框的自定义列表视图,我可以选中复选框并获取选定的值。但是当我尝试搜索某些特定名称的应用程序崩溃时。

这是我的代码,我想我错过了一些东西。

public class MyAdapter extends BaseAdapter {
    private ArrayList<InfoRowdata> arraylisto;
    private List<InfoRowdata> worldlist = null;

    Context mContext;
    LayoutInflater inflater;

    public MyAdapter(Context context, List<InfoRowdata> worldpopulationlist) {
        mContext = context;
        inflater = LayoutInflater.from(mContext);
        this.worldlist = worldpopulationlist;

        this.arraylisto = new ArrayList<InfoRowdata>();
        this.arraylisto.addAll(worldpopulationlist);

    }

    public class ViewHolder {
        TextView Name;
        CheckBox cb;

    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return data.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return worldlist.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

 public View getView(final int position, View view, ViewGroup parent) {
final ViewHolder holder;
if (view == null) {
    holder = new ViewHolder();
    view = inflater.inflate(R.layout.creditunionrow, null);
    // Locate the TextViews in listview_item.xml
    holder.Name = (TextView) view.findViewById(R.id.label);
    holder.cb = (CheckBox) view.findViewById(R.id.check);

    view.setTag(holder);
} else {
    holder = (ViewHolder) view.getTag();
}
// Set the results into TextViews
holder.Name.setText(worldlist.get(position).getName());
 // holder.cb.setChecked(worldlist.get(position).getChecked());


holder.cb.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Selection = "";
                count=0;
                String selecteditems = "";
                // TODO Auto-generated method stub
                if (infodata.get(position).isclicked) {
                    infodata.get(position).isclicked = false;

                } else {
                    infodata.get(position).isclicked = true;
                }
                for (int i = 0; i < infodata.size(); i++) {
                    if (infodata.get(i).isclicked) {
                   //System.out.println("Selectes Are == "+ data[i]);
                        selecteditems = selecteditems + data[i] + ",";
                    count++;
                    }
                }
                System.out.println("Selected Are == " + selecteditems);
                Selection = selecteditems;
            }



});
if (infodata.get(position).isclicked) {

    holder.cb.setChecked(true);
} else {
    holder.cb.setChecked(false);
}




// Listen for ListView Item Click
view.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View arg0) {

    }
});

return view;
}

    // Filter Class
    public void filter(String charText) {
        charText = charText.toLowerCase(Locale.getDefault());
        worldlist.clear();
        if (charText.length() == 0) {
            worldlist.addAll(arraylisto);
        } else {
            for (InfoRowdata wp : arraylisto) {
                if (wp.getName().toLowerCase(Locale.getDefault())
                        .contains(charText)) {
                    worldlist.add(wp);
                }
            }
        }
        notifyDataSetChanged();
    }

}

这是我的模型CLass

public class InfoRowdata {

public boolean isclicked=false;
public int index;
public String name;
/*public String strAmount;*/

public InfoRowdata(boolean isclicked,int index,String name/*,String strAmount*/)
{
    this.index=index;
    this.isclicked=isclicked;
    this.name=name;
    /*this.strAmount=strAmount;*/
}


public String getName() {
    return this.name;
}

public void setName(String name) {
    this.name = name;
}


public Boolean getChecked() {
    return isclicked;
}

public void setChecked(Boolean checked) {
    this.isclicked = checked;
}

}

我得到的例外:

                                                                   java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2
                                                                       at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
                                                                       at java.util.ArrayList.get(ArrayList.java:308)
                                                                       at My.Profile.activity.UnionActivity$MyAdapter.getView(UnionActivity.java:240)
                                                                       at android.widget.AbsListView.obtainView(AbsListView.java:2346)
                                                                       at android.widget.ListView.measureHeightOfChildren(ListView.java:1281)
                                                                       at android.widget.ListView.onMeasure(ListView.java:1188)
                                                                       at android.view.View.measure(View.java:18794)
                                                                       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                                       at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
                                                                       at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
                                                                       at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
                                                                       at android.view.View.measure(View.java:18794)
                                                                       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                                       at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
                                                                       at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
                                                                       at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
                                                                       at android.view.View.measure(View.java:18794)
                                                                       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                                       at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
                                                                       at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
                                                                       at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
                                                                       at android.view.View.measure(View.java:18794)
                                                                       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                                       at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                                       at android.view.View.measure(View.java:18794)
                                                                       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                                       at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
                                                                       at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
                                                                       at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
                                                                       at android.view.View.measure(View.java:18794)
                                                                       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                                       at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                                       at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643)
                                                                       at android.view.View.measure(View.java:18794)
                                                                       at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100)
                                                                       at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1216)
                                                                       at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1452)
                                                                       at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
                                                                       at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
                                                                       at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
                                                                       at android.view.Choreographer.doCallbacks(Choreographer.java:670)
                                                                       at android.view.Choreographer.doFrame(Choreographer.java:606)
                                                                       at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
                                                                       at android.os.Handler.handleCallback(Handler.java:739)
                                                                       at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                       at android.os.Looper.loop(Looper.java:148)
                                                                       at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

这就是我使用edittext搜索listview的方式:

inputSearch.addTextChangedListener(new TextWatcher()
    {
        public void afterTextChanged(Editable s)
        {
            //Abstract Method of TextWatcher Interface.
        }
        public void beforeTextChanged(CharSequence s,int start, int count, int after)
        {

        }
        public void onTextChanged(CharSequence s,int start, int before, int count)
        {



            String text = inputSearch.getText().toString().toLowerCase(Locale.getDefault());
            adapter.filter(text);
        }
    });

我不知道我在哪里遗漏了什么,我想检查随机复选框,并在正确引用已选中的复选框上应用搜索,首先我没有使用自定义适配器,搜索很好,但我丢失了在应用搜索时检查复选框的状态,现在我正在使用customadapter,如何在搜索listview时获得正确的复选框参考。我将非常感谢任何帮助,我几乎花了两天时间在这里,并搜索所有的互联网,但无法得到任何实现listview与复选框和搜索功能的例子。

2 个答案:

答案 0 :(得分:0)

  

java.util.ArrayList.addAll(Collection c)方法   将指定集合中的所有元素追加到末尾   此列表,按指定的返回顺序排列   集合的迭代器。

<强> WRONG

    this.arraylisto = new ArrayList<InfoRowdata>();
    this.arraylisto.addAll(worldpopulationlist); // Problem Here

从右

this.arraylisto = new ArrayList<InfoRowdata>();
this.arraylisto.addAll(worldlist); // Rectify here

 @Override
public int getCount() {
    // TODO Auto-generated method stub
    return worldlist.size();
}

答案 1 :(得分:0)

首先不要STABLE使用onClickListener CheckBox使用setOnCheckedChangeListener,第二次使用空列表尝试@IntelliJ Amiya回答