我想获取从列表视图中选择的所有家庭 ID。单击相应的复选框时,我将家庭 ID 添加到单独的数组列表中。当我滚动列表视图时,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;
}
} }