0

我想获取从列表视图中选择的所有家庭 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;
}

} }

4

2 回答 2

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 不为空,那么它已经带有旧的 onClickListener。此 OnClickListener 使用错误的位置(从旧视图)。

因此,您不仅应该在 (v == null) 的情况下设置 OnClickListener。

于 2013-03-06T08:09:16.720 回答
0

我对我的代码进行了一些更改,并将一个数组列表的实例(对于 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;
}


}
}

Now i need to show the selected items when i open this activity. How can i done that? i try to pass it by the familyselectedlist to the adapter. If i uncheck the checkbox and scroll the check box status is remain as checked.

于 2013-03-07T06:35:15.907 回答