Android自定义ListView搜索与复选框,筛选器不工作
问题描述:
我正在Android应用程序,我有一个自定义列表与复选框,我可以选择复选框,并获得选定的值。但是当我尝试搜索一些特定的名称应用程序崩溃。Android自定义ListView搜索与复选框,筛选器不工作
这里是我的代码,我想我错过了一些东西。
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();
}
}
这里是我的模型类
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)
这是怎么了Searchin的摹列表视图用的EditText:
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与复选框和搜索功能的例子。
答
的java.util.ArrayList.addAll(系列三)方法 追加所有指定集合中的元素的 年底这个名单的,因为它们是由指定 集合的迭代器返回的顺序。
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();
}
答
先不要上CheckBox
使用setOnCheckedChangeListener
使用onClickListener
,第二你有对空列表尝试@IntelliJ阿米亚·回答
大这个工作,现在我没有得到任何的异常和搜索工作,非常感谢。但仍然有问题与复选框的选中状态,如果您可以给任何提示如何跟踪检查复选框,同时搜索 – user6768912