android listview中的复选框状态

时间:2013-03-06 07:38:22

标签: android arraylist android-listview android-checkbox

我想从列表视图中选择所有家庭ID。我将家庭ID添加到一个单独的 单击相应复选框时的arraylist。当我滚动列表视图时,arraylist中的值会丢失。

以下是我的代码。

    public class FamilyList extends Activity{
ListView fam_list;
DataAdapter myadapt;
ArrayList<ListClass> familyarraylist;
ArrayList<String> familyselectedlist;
Button show;
  @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Log.i("zacharia", "inside oncreate");
setContentView(R.layout.activity_familylist);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
fam_list=(ListView) findViewById(R.id.familylist);
myadapt=new DataAdapter(this);
final TextView empty=(TextView) findViewById(R.id.emptyView);
empty.setText("No Data Found");
familyarraylist= myadapt.getfamilylist("All");
FamilyListAdapter familyadapter=new FamilyListAdapter(this, R.layout.family_list_view,familyarraylist);
fam_list.setAdapter(familyadapter);
fam_list.setEmptyView(empty);


familyselectedlist=new ArrayList<String>();

ImageButton button=(ImageButton) findViewById(R.id.imageButton1);
button.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        EditText edit=(EditText) findViewById(R.id.editText1);
        if(edit.getText().length()>0){
            if(myadapt==null){
            myadapt=new DataAdapter(FamilyList.this);
            }
             familyarraylist=myadapt.getfamilylist(edit.getText().toString());
            fam_list.setAdapter(new FamilyListAdapter(FamilyList.this, R.layout.family_list_view, familyarraylist));                }
        else{
            if(myadapt==null){
                myadapt=new DataAdapter(FamilyList.this);
                }
            familyarraylist=myadapt.getfamilylist("All");
            fam_list.setAdapter(new FamilyListAdapter(FamilyList.this, R.layout.family_list_view,familyarraylist ));    
        }
        fam_list.setEmptyView(empty);

    }
});


show=(Button) findViewById(R.id.button1);
show.setOnClickListener(new OnClickListener() {

    public void onClick(View arg0) {
        for(int i=0;i<familyselectedlist.size();i++){
            Log.i("zacharia", familyselectedlist.get(i));
        }
        MainActivity.selected_list=true;
        MainActivity.selected_family_id_array=familyselectedlist;           
        FamilyList.this.finish();

    }
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
getMenuInflater().inflate(R.menu.activity_list, menu);
return true;
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
// TODO Auto-generated method stub
if(item.getItemId()==R.id.item1){
    final SharedPreferences prefs=getSharedPreferences("search_mode",  Activity.MODE_WORLD_READABLE);
    int pos=prefs.getInt("position", 0);
    new AlertDialog.Builder(this).setTitle("Search By")
            .setSingleChoiceItems(new String[]{"Name","House Name"}, pos, new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                    Editor edit=prefs.edit();
                    edit.putInt("position", which);
                    edit.commit();
                    dialog.dismiss();
                }
            }).show();
}
return true;
}
static class ViewHolder {
protected TextView id_text,id_name,id_address;
protected CheckBox id_check;
   }
class FamilyListAdapter extends ArrayAdapter<ListClass>{

private ArrayList<ListClass> list_array;
Context context;


boolean checkchange=false;
ListClass obj;
public FamilyListAdapter(Context context, int textViewResourceId,
        ArrayList<ListClass> objects) {
    super(context, textViewResourceId, objects);
    this.context=context;
    list_array=objects;

}

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

public int getFamilyId(int position){
    return list_array.get(position).getFamilyid();
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View v=convertView;
    if(v==null){
        LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v=inflater.inflate(R.layout.family_list_view, null);
        final ViewHolder holder = new ViewHolder();
        obj=getItem(position);
        holder.id_text=(TextView) v.findViewById(R.id.list_familyid);
        holder.id_text.setText(""+obj.getFamilyid());
        holder.id_name=(TextView) v.findViewById(R.id.list_name);
        holder.id_name.setText(""+obj.getHeadname());
        holder.id_address=(TextView) v.findViewById(R.id.list_address);
        holder.id_address.setText(""+obj.getAddress());
        holder.id_check=(CheckBox) v.findViewById(R.id.check_familyview);
        Log.i("zacharia", "check result:"+ getFamilyId(position)+"  "+familyselectedlist.contains(""+getFamilyId(position)));
        holder.id_check.setSelected(false);
        if(familyselectedlist.contains(""+getFamilyId(position))){

            holder.id_check.setSelected(true);
        }


        holder.id_check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Log.i("zacharia", "inside on change");

                if(isChecked){
                    familyselectedlist.add(""+getFamilyId(position));
                }
                else{
                    familyselectedlist.remove(""+getFamilyId(position));
                }

            }
        });
        v.setTag(holder);
        holder.id_check.setTag(getFamilyId(position));
    }
    else{
        v=convertView;
        ((ViewHolder)v.getTag()).id_check.setTag(getFamilyId(position));
    }
    obj=getItem(position);
    ViewHolder holder = (ViewHolder) v.getTag();
    holder.id_text.setText(""+obj.getFamilyid());
    holder.id_name.setText(obj.getHeadname());
    holder.id_address.setText(obj.getAddress());
    if(familyselectedlist.contains(""+getFamilyId(position))){      
    holder.id_check.setChecked(true);
    }
    else{
        holder.id_check.setChecked(false);
    }

    return v;
}

} }

2 个答案:

答案 0 :(得分:0)

只需将以下代码移出if-condition(if(v == null)):

    holder.id_check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            Log.i("zacharia", "inside on change");

            if(isChecked){
                familyselectedlist.add(""+getFamilyId(position));
            }
            else{
                familyselectedlist.remove(""+getFamilyId(position));
            }

        }
    });

只有在没有获得重用视图时才设置侦听器。但是,在重用现有视图时仍需要设置侦听器。因此,如果convertView不为null,则它已经与旧的onClickListener一起出现。此OnClickListener使用错误的位置(从旧视图)。

因此,您不仅要设置OnClickListener(v == null)。

答案 1 :(得分:0)

我对代码进行了一些更改,并将一个arraylist实例(对于selected_ids)传递给它运行的适配器。

以下是我的代码:

public class FamilyList extends Activity{
ListView fam_list;
DataAdapter myadapt;
ArrayList<ListClass> familyarraylist;
ArrayList<String> familyselectedlist;
Button show;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Log.i("zacharia", "inside oncreate");
setContentView(R.layout.activity_familylist);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
fam_list=(ListView) findViewById(R.id.familylist);
myadapt=new DataAdapter(this);
final TextView empty=(TextView) findViewById(R.id.emptyView);
empty.setText("No Data Found");
familyarraylist= myadapt.getfamilylist("All");
familyselectedlist=new ArrayList<String>();

FamilyListAdapter familyadapter=new FamilyListAdapter(this, R.layout.family_list_view,familyarraylist,familyselectedlist);
fam_list.setAdapter(familyadapter);
fam_list.setEmptyView(empty);




ImageButton button=(ImageButton) findViewById(R.id.imageButton1);
button.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        EditText edit=(EditText) findViewById(R.id.editText1);
        if(edit.getText().length()>0){
            if(myadapt==null){
            myadapt=new DataAdapter(FamilyList.this);
            }
            familyarraylist=myadapt.getfamilylist(edit.getText().toString());
            fam_list.setAdapter(new FamilyListAdapter(FamilyList.this, R.layout.family_list_view, familyarraylist,familyselectedlist));             }
        else{
            if(myadapt==null){
                myadapt=new DataAdapter(FamilyList.this);
                }
            familyarraylist=myadapt.getfamilylist("All");
            fam_list.setAdapter(new FamilyListAdapter(FamilyList.this, R.layout.family_list_view,familyarraylist,familyselectedlist));  
        }
        fam_list.setEmptyView(empty);

    }
});


show=(Button) findViewById(R.id.button1);
show.setOnClickListener(new OnClickListener() {

    public void onClick(View arg0) {
        for(int i=0;i<familyselectedlist.size();i++){
            Log.i("zacharia", familyselectedlist.get(i));
        }

        MainActivity.selected_list=true;
        MainActivity.selected_family_id_array=familyselectedlist;           
        FamilyList.this.finish();

    }
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
getMenuInflater().inflate(R.menu.activity_list, menu);
return true;
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
// TODO Auto-generated method stub
if(item.getItemId()==R.id.item1){
    final SharedPreferences prefs=getSharedPreferences("search_mode", 0);
    int pos=prefs.getInt("position", 0);
    new AlertDialog.Builder(this).setTitle("Search By")
            .setSingleChoiceItems(new String[]{"Name","House Name"}, pos, new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                    Editor edit=prefs.edit();
                    edit.putInt("position", which);
                    edit.commit();
                    dialog.dismiss();
                }
            }).show();
}
return true;
}

class FamilyListAdapter extends ArrayAdapter<ListClass>{

private ArrayList<ListClass> list_array;
Context context;
ArrayList<String> selectedlist;
TextView id_text,id_name,id_address;
CheckBox id_check;

boolean checkchange=false;
ListClass obj;
public FamilyListAdapter(Context context, int textViewResourceId,
        ArrayList<ListClass> objects,ArrayList<String> selectedlist) {
    super(context, textViewResourceId, objects);
    this.context=context;
    list_array=objects;
    this.selectedlist=selectedlist;
}

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

public int getFamilyId(int position){
    return list_array.get(position).getFamilyid();
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View v=convertView;
    if(v==null){
        LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v=inflater.inflate(R.layout.family_list_view, null);

    }
    obj=getItem(position);
    id_text=(TextView) v.findViewById(R.id.list_familyid);
    id_text.setText(""+obj.getFamilyid());
    id_name=(TextView) v.findViewById(R.id.list_name);
    id_name.setText(""+obj.getHeadname());
    id_address=(TextView) v.findViewById(R.id.list_address);
    id_address.setText(""+obj.getAddress());
    id_check=(CheckBox) v.findViewById(R.id.check_familyview);

    id_check.setSelected(false);
    if(selectedlist.contains(""+getFamilyId(position))){

        id_check.setSelected(true);
    }


    id_check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            Log.i("zacharia", "inside on change");

            if(isChecked){
                selectedlist.add(""+getFamilyId(position));
            }
            else{
                selectedlist.remove(""+getFamilyId(position));
            }

        }
    });

    id_check.setTag(getFamilyId(position));
    if(selectedlist.contains(""+getFamilyId(position))){        
    id_check.setChecked(true);
    }
    else{
        id_check.setChecked(false);
    }

    return v;
}


}
}

现在我需要在打开此活动时显示所选项目。我怎么能这样做?我尝试通过familyselected列表将它传递给适配器。如果我取消选中该复选框并滚动复选框,状态将保持为已选中状态。