I fulfill the AlertDIalog and listView with two values, Name and id, and i cannot find conclusion оr the right way, when i checked some CheckBoxes, i need the value of the Checked/Selected rows of (id's text) to save.
@OnClick(R.id.group_delete)
void deleteGroup() {
AlertDialog.Builder dialog4 = new AlertDialog.Builder(PreviewActivity.this);
LayoutInflater inflater2 = getLayoutInflater();
View convertView2 = (View) inflater2.inflate(R.layout.alert_dialog_delete_group, null);
dialog4.setView(convertView2);
ListView listView2 = (ListView) convertView2.findViewById(R.id.listViewGroup);
final List<Item> groupList = this.getGroup();
CustomAdapter listAdapter2 = new CustomAdapter(getBaseContext(), groupList);
// listAdapter2.notifyDataSetChanged();
convertView2.setTag(dialog4);
// SparseBooleanArray sp = listView2.getCheckedItemPositions();
listView2.setOnItemClickListener((AdapterView<?> adapterView, View view, int position, long l) -> {
// Get user selected item.
Object itemObject = adapterView.getAdapter();
Item itemDto = (Item) itemObject;
CheckBox itemCheckbox = (CheckBox) convertView2.findViewById(R.id.checkMark5);
listAdapter2.setCheckedItem(position);
});
ImageView delete = (ImageView) convertView2.findViewById(R.id.del_group);
delete.setOnClickListener((View view) -> {
CheckBox itemCheckbox = (CheckBox) convertView2.findViewById(R.id.checkMark5);
Iterator<String> it = listAdapter2.getCheckedItems().values().iterator();
for (int i=0;i<listAdapter2.getCheckedItems().size();i++){
//Do whatever
// String result = "";
// ArrayList<String> resultList = new ArrayList<String>();
// for (int y = 0; y < resultList.size(); y++) {
// result += String.valueOf(resultList.get(i)) + "\n";
// }
// if (result.matches("")) {
////
// Toast.makeText(getApplicationContext(),"Please select some thing from list to show",Toast.LENGTH_LONG).show();
// }
// else {
Toast.makeText(getApplicationContext(),result,Toast.LENGTH_LONG).show();
// }
listAdapter2.getItem(Integer.parseInt(it.next()));
}
// List<Item> resultList = listAdapter2.getCheckedItems();
// for (int i = 0; i < resultList.size(); i++) {
// result += String.valueOf(resultList.get(i)) + "\n";
// }
// listAdapter2.getCheckedItemPositions().toString();
//
// if (result.matches("")) {
//
// Toast.makeText(getApplicationContext(),"Please select some thing from list to show",Toast.LENGTH_LONG).show();
// }
// else {
Toast.makeText(getApplicationContext(),result,Toast.LENGTH_LONG).show();
// }
// shared.setSharedPreferencesString(Constants.ID, size2);
listAdapter2.notifyDataSetChanged();
});
listView2.setAdapter(listAdapter2);
listView2.setTag(dialog4);
AlertDialog alert4 = dialog4.create();
alert4.show();
alert4.getWindow().
setBackgroundDrawable(new ColorDrawable(android.graphics.Color.argb(0, 100, 100, 100)));
}
public List<Company> getCompanies() {
return mCompanies;
}
private List<Item> getGroup() {
ArrayList<String> groups = new ArrayList<String>();
ArrayList<String> groups2 = new ArrayList<>();
for (Group group2 : mRepository.getCompanies().get(posit).getGroups()) {
groups2.add(group2.getId());
}
for (Group group1 : mRepository.getCompanies().get(posit).getGroups()) {
groups.add(group1.getName());
}
List<Item> ret = new ArrayList<Item>();
int length = groups.size();
for (int i = 0; i < length; i++) {
String itemText2 = groups.get(i);
String itemText3 = groups2.get(i);
Item dto = new Item();
dto.setChecked(false);
dto.setIdText(itemText3);
dto.setItemText(itemText2);
ret.add(dto);
}
return ret;
}
Item Class:
public class Item {
private boolean checked ;
private String itemText = "";
private String IdText = "";
private String IdText2 = "";
public String getIdText2() {
return IdText2;
}
public void setIdText2(String idText2) {
IdText2 = idText2;
}
public String getIdText() {
return IdText;
}
public void setIdText(String idText) {
IdText = idText;
}
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}
public String getItemText() {
return itemText;
}
public void setItemText(String itemText) {
this.itemText = itemText;
}}
Custom Adapter :::
public class CustomAdapter extends BaseAdapter {
private List<Item> ItemList ;
public HashMap<String, String> checked = new HashMap<String, String>();
private Context ctx;
public CustomAdapter(Context ctx, List<Item> ItemList) {
this.ctx = ctx;
this.ItemList = ItemList;
}
public void setCheckedItem(int item) {
if (checked.containsKey(String.valueOf(item))) {
checked.remove(String.valueOf(item));
} else {
checked.put(String.valueOf(item), String.valueOf(item));
}
}
public HashMap<String, String> getCheckedItems() {
return checked;
}
@Override
public int getCount() {
return ItemList.size();
}
@Override
public Object getItem(int position) {
return ItemList.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
ItemViewHolder viewHolder = null;
if (convertView != null) {
viewHolder = (ItemViewHolder) convertView.getTag();
} else {
convertView = View.inflate(ctx, R.layout.custom, null);
CheckBox checkbox = (CheckBox) convertView.findViewById(R.id.checkMark5);
TextView listItemText = (TextView) convertView.findViewById(R.id.item_unit_id5);
TextView tetx = (TextView) convertView.findViewById(R.id.tekst5);
TextView textId = (TextView) convertView.findViewById(R.id.item_number5);
viewHolder = new ItemViewHolder(convertView);
viewHolder.setItemCheckbox(checkbox);
viewHolder.setItemTextView(listItemText);
viewHolder.setIdTextView(textId);
viewHolder.setIdTextView2(tetx);
convertView.setTag(viewHolder);
}
Item items = ItemList.get(position);
viewHolder.getItemCheckbox().setChecked(items.isChecked());
viewHolder.getItemTextView().setText(items.getItemText());
viewHolder.getIdTextView().setText(items.getIdText());
viewHolder.getIdTextView2().setText(items.getIdText2());
return convertView;
}}
Viev Holder ::
public class ItemViewHolder extends RecyclerView.ViewHolder {
private CheckBox itemCheckbox;
private TextView IdTextView;
private TextView itemTextView;
private TextView IdTextView2;
public TextView getIdTextView2() {
return IdTextView2;
}
public void setIdTextView2(TextView idTextView2) {
IdTextView2 = idTextView2;
}
public ItemViewHolder(View itemView) {
super(itemView);
}
public TextView getIdTextView() {
return IdTextView;
}
public void setIdTextView(TextView idTextView) {
IdTextView = idTextView;
}
public CheckBox getItemCheckbox() {
return itemCheckbox;
}
public void setItemCheckbox(CheckBox itemCheckbox) {
this.itemCheckbox = itemCheckbox;
}
public TextView getItemTextView() {
return itemTextView;
}
public void setItemTextView(TextView itemTextView) {
this.itemTextView = itemTextView;
} }
Aucun commentaire:
Enregistrer un commentaire