0

リスト ビューから選択したすべての家族 ID を取得したい。対応するチェックボックスをクリックすると、ファミリ ID が別の配列リストに追加されます。リストビューをスクロールすると、配列リストの値が失われます。

以下は私のコードです。

    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

コードにいくつかの変更を加え、配列リストのインスタンス (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;
}


}
}

このアクティビティを開いたときに、選択したアイテムを表示する必要があります。どうすればそれを行うことができますか? familyselectedlist でアダプターに渡そうとします。チェックボックスのチェックを外してスクロールすると、チェックボックスのステータスがチェックされたままになります。

于 2013-03-07T06:35:15.907 に答える
0

次のコードを if 条件 (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 は間違った位置を使用しています (古いビューから)。

その結果、(v == null) の場合だけでなく、OnClickListener を設定する必要があります。

于 2013-03-06T08:09:16.720 に答える