user6768912 user6768912 - 1 year ago 47
Android Question

Android Custom ListView search with CheckBoxes , Filter is not Working

I am working on Android Application , i have a custom listview with checkboxes , i can select check boxes and get selected values . but when i try to search some particular name application crashes .

here is my code , i think i am missing something .

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();
}

}


here is my Model 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;
}


}

The Exception I am getting :

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)


This is how i am Searching listview with 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);
}
});


I dont know where i am missing something , i want to check random checkboxes and also apply search on them with correct reference of checked checkboxes , firstly i was not doing it with custom adapter , the searching was fine there but i lost the checked state of checkboxes when search is applied , now i am using customadapter , how can i get correct reference of check boxes on searching listview . I will verythankful for any help , i almost had spent two days in this , and search all the internet but could not get any example which implements listview with checkboxes and search functionality .

Answer Source

The java.util.ArrayList.addAll(Collection c) method appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator.

WRONG

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

Right

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

And

 @Override
public int getCount() {
    // TODO Auto-generated method stub
    return worldlist.size();
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download