我有一个ListView
的自定义行。在自定义行中,我有一个复选框和两个textview。就像我在Listview
中有5个自定义行一样。现在我检查了第一行复选框和第二行复选框。现在我需要在另一个屏幕上显示这些已检查行的数据。
这里的片段
public class ProcessPurchaseOrder extends Activity{
HashMap<String, String> ProcessPOHashMap;
ArrayList<HashMap<String,String>> arListHashMap;
ListView processPOListView;
ProcessPOAdapter processPOAdapter;
@InjectView(R.id.processPONextBtn) protected Button procesPONxtBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.process_po);
ButterKnife.inject(this);
procesPONxtBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent drugSupplier = new Intent(ProcessPurchaseOrder.this, DrugSupplierActivity.class);
startActivity(drugSupplier);
}
});
arListHashMap = new ArrayList<HashMap<String, String>>();
ProcessPOHashMap = new HashMap<String, String>();
ProcessPOHashMap.put("DrugName", "crocin");
ProcessPOHashMap.put("QtyAvail", "300");
ProcessPOHashMap.put("QtyOrderd", "500");
arListHashMap.add(ProcessPOHashMap);
ProcessPOHashMap = new HashMap<String, String>();
ProcessPOHashMap.put("DrugName", "zintac");
ProcessPOHashMap.put("QtyAvail", "200");
ProcessPOHashMap.put("QtyOrderd", "400");
arListHashMap.add(ProcessPOHashMap);
ProcessPOHashMap = new HashMap<String, String>();
ProcessPOHashMap.put("DrugName", "anacin");
ProcessPOHashMap.put("QtyAvail", "400");
ProcessPOHashMap.put("QtyOrderd", "400");
arListHashMap.add(ProcessPOHashMap);
ProcessPOHashMap = new HashMap<String, String>();
ProcessPOHashMap.put("DrugName", "tapic-EM");
ProcessPOHashMap.put("QtyAvail", "400");
ProcessPOHashMap.put("QtyOrderd", "400");
arListHashMap.add(ProcessPOHashMap);
ProcessPOHashMap = new HashMap<String, String>();
ProcessPOHashMap.put("DrugName", "omnacartil 5mg");
ProcessPOHashMap.put("QtyAvail", "400");
ProcessPOHashMap.put("QtyOrderd", "400");
arListHashMap.add(ProcessPOHashMap);
ProcessPOHashMap = new HashMap<String, String>();
ProcessPOHashMap.put("DrugName", "maxiguard 250mg");
ProcessPOHashMap.put("QtyAvail", "400");
ProcessPOHashMap.put("QtyOrderd", "400");
arListHashMap.add(ProcessPOHashMap);
ProcessPOHashMap = new HashMap<String, String>();
ProcessPOHashMap.put("DrugName", "c-led");
ProcessPOHashMap.put("QtyAvail", "400");
ProcessPOHashMap.put("QtyOrderd", "400");
arListHashMap.add(ProcessPOHashMap);
ProcessPOHashMap = new HashMap<String, String>();
ProcessPOHashMap.put("DrugName", "1000 mcg");
ProcessPOHashMap.put("QtyAvail", "400");
ProcessPOHashMap.put("QtyOrderd", "400");
arListHashMap.add(ProcessPOHashMap);
ProcessPOHashMap = new HashMap<String, String>();
ProcessPOHashMap.put("DrugName", "omikind");
ProcessPOHashMap.put("QtyAvail", "400");
ProcessPOHashMap.put("QtyOrderd", "400");
arListHashMap.add(ProcessPOHashMap);
ProcessPOHashMap = new HashMap<String, String>();
ProcessPOHashMap.put("DrugName", "nice");
ProcessPOHashMap.put("QtyAvail", "900");
ProcessPOHashMap.put("QtyOrderd", "100");
arListHashMap.add(ProcessPOHashMap);
ProcessPOHashMap = new HashMap<String, String>();
ProcessPOHashMap.put("DrugName", "omez");
ProcessPOHashMap.put("QtyAvail", "400");
ProcessPOHashMap.put("QtyOrderd", "400");
arListHashMap.add(ProcessPOHashMap);
ProcessPOHashMap = new HashMap<String, String>();
ProcessPOHashMap.put("DrugName", "tellmekind 40mg");
ProcessPOHashMap.put("QtyAvail", "400");
ProcessPOHashMap.put("QtyOrderd", "400");
arListHashMap.add(ProcessPOHashMap);
ProcessPOHashMap = new HashMap<String, String>();
ProcessPOHashMap.put("DrugName", "Amlokind-at");
ProcessPOHashMap.put("QtyAvail", "400");
ProcessPOHashMap.put("QtyOrderd", "400");
arListHashMap.add(ProcessPOHashMap);
ProcessPOHashMap = new HashMap<String, String>();
ProcessPOHashMap.put("DrugName", "gatimac 400mg");
ProcessPOHashMap.put("QtyAvail", "400");
ProcessPOHashMap.put("QtyOrderd", "400");
arListHashMap.add(ProcessPOHashMap);
ProcessPOHashMap = new HashMap<String, String>();
ProcessPOHashMap.put("DrugName", "nupenta-DSR");
ProcessPOHashMap.put("QtyAvail", "400");
ProcessPOHashMap.put("QtyOrderd", "400");
arListHashMap.add(ProcessPOHashMap);
processPOListView = (ListView) findViewById(R.id.processPOListView);
processPOAdapter= new ProcessPOAdapter(ProcessPurchaseOrder.this, arListHashMap);
processPOListView.setAdapter(processPOAdapter);
}}`
AdapterClass
就在这里
public class ProcessPOAdapter extends BaseAdapter {
private Activity activity;
Activity context;
ArrayList<HashMap<String, String>> data;
HashMap<String, String> mapdata;
public ProcessPOAdapter(Activity context, ArrayList<HashMap<String, String>> data) {
super();
this.context = context;
this.data = data;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
private class ViewHolder {
CheckBox drugCheck;
TextView drugName;
TextView qtyAvail;
TextView qtyOrderd;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
LayoutInflater inflater = context.getLayoutInflater();
mapdata = new HashMap<String, String>();
mapdata = data.get(position);
if (convertView == null) {
convertView = inflater.inflate(R.layout.process_po_row, null);
holder = new ViewHolder();
holder.drugCheck = (CheckBox) convertView.findViewById(R.id.chkBoxDrugs);
holder.drugName = (TextView) convertView.findViewById(R.id.txtDrugValue);
holder.qtyAvail = (TextView) convertView.findViewById(R.id.txtQtyAvailValue);
holder.qtyOrderd = (TextView) convertView.findViewById(R.id.txtQtyOrderdValue);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.drugName.setText(mapdata.get("DrugName"));
holder.qtyAvail.setText(mapdata.get("QtyAvail"));
holder.qtyOrderd.setText(mapdata.get("QtyOrderd"));
return convertView;
}
}
答案 0 :(得分:0)
首先你应该使用一个对象而不是一个地图,但这不是问题。
您需要创建对象或地图的HashSet以在您的适配器中存储已检查的项目,否则由于视图在列表中重新出现,您检查项目将在滚动时更改。
以下是使用这些更改更新的代码
public class ProcessPOAdapter extends BaseAdapter {
private Activity activity;
Activity context;
ArrayList<HashMap<String, String>> data;
HashMap<String, String> mapdata;
private HashSet<HashMap<String, String>> selectedItems = new HashSet<HashMap<String, String>>();
public ProcessPOAdapter(Activity context, ArrayList<HashMap<String, String>> data) {
super();
this.context = context;
this.data = data;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
private class ViewHolder {
CheckBox drugCheck;
TextView drugName;
TextView qtyAvail;
TextView qtyOrderd;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
LayoutInflater inflater = context.getLayoutInflater();
mapdata = new HashMap<String, String>();
mapdata = data.get(position);
if (convertView == null) {
convertView = inflater.inflate(R.layout.process_po_row, null);
holder = new ViewHolder();
holder.drugCheck = (CheckBox) convertView.findViewById(R.id.chkBoxDrugs);
holder.drugCheck.setOnCheckedChangeListener(listener);
holder.drugName = (TextView) convertView.findViewById(R.id.txtDrugValue);
holder.qtyAvail = (TextView) convertView.findViewById(R.id.txtQtyAvailValue);
holder.qtyOrderd = (TextView) convertView.findViewById(R.id.txtQtyOrderdValue);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.drugCheck.setTag(mapdata);//link object to retrieve it later in onCheckedChanged
holder.drugCheck.setChecked(selectedItems.contains(mapdata));
holder.drugName.setText(mapdata.get("DrugName"));
holder.qtyAvail.setText(mapdata.get("QtyAvail"));
holder.qtyOrderd.setText(mapdata.get("QtyOrderd"));
return convertView;
}
private CompoundButton.OnCheckedChangeListener listener = new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mapdata = (HashMap<String, String>) buttonView.getTag();
boolean isSelected = selectedItems.contains(mapdata);
if(isSelected!=isChecked){ // protection to avoid updating selection when calling setChecked() from getView()
if(isChecked){
selectedItems.add(mapdata);
}else{
selectedItems.remove(mapdata);
}
}
}
};
public List<HashMap<String, String>> getSelectedItems(){
List<HashMap<String, String>> ret = new ArrayList<HashMap<String, String>>();
for(HashMap<String, String> item : selectedItems){
ret.add(item);
}
return ret;
}
}
如果使用HashMap不起作用(我不知道hashMap.equals / .hashCode实现)请尝试使用自定义objetcs。
您可以通过将所有项目添加到selectItems /清除它来选择/取消全选。你可能需要调用notifyDataSetChanged();为您的列表实际更新它的观点。
编辑:
将其发送到您的其他活动
yourButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
Bundle b = new Bundle();
b.putSerializable("key", yourAddapter.getSelectedItems()); //Your data
intent.putExtras(b);
startActivity(intent);
}
});
并在第二个活动中检索它
Bundle b = getIntent().getExtras();
list<HashMap<String, String>> list = b.getSerializable("key");
答案 1 :(得分:0)
Try this:
public class ProcessPOAdapter extends BaseAdapter {
private Activity activity;
Activity context;
ArrayList<HashMap<String, String>> data;
HashMap<String, String> mapdata;
private HashSet<HashMap<String, String>> selectedItems = new HashSet<HashMap<String, String>>();
public ProcessPOAdapter(Activity context, ArrayList<HashMap<String, String>> data) {
super();
this.context = context;
this.data = data;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
private class ViewHolder {
CheckBox drugCheck;
TextView drugName;
TextView qtyAvail;
TextView qtyOrderd;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
LayoutInflater inflater = context.getLayoutInflater();
mapdata = new HashMap<String, String>();
mapdata = data.get(position);
if (convertView == null) {
convertView = inflater.inflate(R.layout.process_po_row, null);
holder = new ViewHolder();
holder.drugCheck = (CheckBox) convertView.findViewById(R.id.chkBoxDrugs);
holder.drugCheck.setOnCheckedChangeListener(listener);
holder.drugName = (TextView) convertView.findViewById(R.id.txtDrugValue);
holder.qtyAvail = (TextView) convertView.findViewById(R.id.txtQtyAvailValue);
holder.qtyOrderd = (TextView) convertView.findViewById(R.id.txtQtyOrderdValue);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.drugCheck.setTag(mapdata);//link object to retrieve it later in onCheckedChanged
holder.drugCheck.setOnCheckedChangeListener(new CheckchangeListener(position));
holder.drugCheck.setChecked(selectedItems.contains(mapdata));
holder.drugName.setText(mapdata.get("DrugName"));
holder.qtyAvail.setText(mapdata.get("QtyAvail"));
holder.qtyOrderd.setText(mapdata.get("QtyOrderd"));
return convertView;
}
class CheckchangeListener implements OnCheckedChangeListener {
int position;
public CheckchangeListener(int position) {
this.position = position;
}
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mapdata = data.get(position);
boolean isSelected = selectedItems.contains(mapdata);
if(isSelected!=isChecked){ // protection to avoid updating selection when calling setChecked() from getView()
if(isChecked){
selectedItems.add(mapdata);
}else{
selectedItems.remove(mapdata);
}
}
}
}
}
public List<HashMap<String, String>> getSelectedItems(){
List<HashMap<String, String>> ret = new ArrayList<HashMap<String, String>>();
for(HashMap<String, String> item : selectedItems){
ret.add(item);
}
return ret;
}
}