我正在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與複選框和搜索功能的例子。
大這個工作,現在我沒有得到任何的異常和搜索工作,非常感謝。但仍然有問題與複選框的選中狀態,如果您可以給任何提示如何跟蹤檢查複選框,同時搜索 – user6768912