我正在将值添加到数组和适配器,并且当我删除特定的项目列表视图时,它会完美地从arraylist中删除,但是如果我有
,它将删除最后一个视图的均值a
b
c
d
e
当我删除b .... e视图时,为什么...然后列表是
a
b
c
d
我从过去2周开始进行此工作,但是我找不到此错误对我有帮助。 请 请 请 请
public class Home extends AppCompatActivity {
Button addMoreBtn,checkoutBtn;
MedicineBean medicineBean;
//CustomAdapter customAdapter;
ArrayList<MedicineBean> medOrderList;
//<MedicineBean> medOrderList1;
MyDBHandler db;
ListView listView;
int cust_id;
EditText med_nameET, med_potencyET, med_qtyET;
ImageButton increaseQty,decreaseQty;
HomeListViewHandler homeListViewHandler;
private ArrayAdapter<MedicineBean> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
String session_exp = SessionUtil.getSessionExpire(Home.this);
if(Constants.LOGGING)
Toast.makeText(getApplicationContext(),"Session Exp = "+session_exp+" Today Date "+new DateAndTimeUtil().getCurrentDate(),Toast.LENGTH_LONG).show();
//30 days session expire checker
if(session_exp.matches(new DateAndTimeUtil().getCurrentDate())){
SessionUtil.setSignout(Home.this);
Toast.makeText(getApplicationContext(),"Session has been expired due to 30 days limit exceeded",Toast.LENGTH_LONG).show();
Intent intent = new Intent(Home.this, SignIn.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
//first check, Is user LOGGED IN or not
else if(SessionUtil.isLoggedIn(Home.this)) {
Toolbar toolBar = (Toolbar) findViewById(R.id.easymed_toolbar);
setSupportActionBar(toolBar);
toolBar.setTitle(getResources().getString(R.string.home));
DrawerUtil.getDrawer(this, toolBar);
addMoreBtn = (Button) findViewById(R.id.addtoList);
checkoutBtn = (Button) findViewById(R.id.checkOutBtn);
listView = (ListView) findViewById(R.id.medListView);
med_nameET = (EditText) findViewById(R.id.frstSingleItemET);
med_potencyET = (EditText) findViewById(R.id.scndSingleItemET);
med_qtyET = (EditText) findViewById(R.id.thrdSingleItemTV);
increaseQty=(ImageButton) findViewById(R.id.increaceImgBtn);
decreaseQty=(ImageButton) findViewById(R.id.decreaseImgBtn) ;
medOrderList = new ArrayList<MedicineBean>();
//medOrderList1 = new ArrayList<MedicineBean>();
cust_id = SessionUtil.getCustomerId(Home.this);
medicineBean = new MedicineBean();
db = new MyDBHandler(Home.this);
//customAdapter = new CustomAdapter();
adapter=new CustomAdapter(this,0,medOrderList);
listView.setAdapter(adapter);
increaseQty.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(Constants.LOGGING)
System.out.println("Increment Btn pressed");
int val = Integer.parseInt(med_qtyET .getText().toString());
val=val+1;
String str = Integer.toString(val);
med_qtyET .setText(str);
}
});
decreaseQty.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int val = Integer.parseInt(med_qtyET .getText().toString());
if(val>0) {
val = val-1;
String str = Integer.toString(val);
med_qtyET.setText(str);
}
}
});
checkoutBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//add new medicines in local DB
int count = 0;
for (int i = 0; i < medOrderList.size(); i++) {
String name = medOrderList.get(i).getMediceneName();
String potency=medOrderList.get(i).getMedicenePotency();
String qty = medOrderList.get(i).getMediceneQty();
if (!name.matches("") && !potency.matches("") && !qty.matches("")) {
//Check if medicine with same potency available in DB then quantity of medicine add
Cursor cursor = db.getMedByNameAndPotency(cust_id,name,potency);
if(cursor.getCount()>0)
{
cursor.moveToNext();
String qtyInDB = cursor.getString(0);
int qtyInDBInt = Integer.parseInt(qtyInDB);
int getQtyFromET = Integer.parseInt(qtyInDB);
int resultOfSumQty = getQtyFromET + qtyInDBInt;
db.updateMedQTY(cust_id,name,potency,resultOfSumQty);
}
//add new medicine
else if (db.addUsersMed(cust_id, name, potency, qty) != -1) {
medicineBean.setCust_id(cust_id);
medicineBean.setMediceneName(name);
medicineBean.setMedicenePotency(potency);
medicineBean.setMediceneQty(qty);
medOrderList.set(i, medicineBean);
//Log.i(Constants.TAG,medOrderList.get(i).toString());
count++;
}
}
}
if (count > 0) {
Toast.makeText(getApplicationContext(), "Medicines added successfully.", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Medicines not added into cart.", Toast.LENGTH_SHORT).show();
}
Intent intent = new Intent(Home.this, Cart.class);
startActivity(intent);
}
});
addMoreBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!med_nameET.getText().toString().matches("") && !med_potencyET.getText().toString().matches("")
&& !med_qtyET.getText().toString().matches("0")) {
medOrderList.add(new MedicineBean(cust_id,med_nameET.getText().toString(),med_potencyET.getText().toString(),med_qtyET.getText().toString()));
med_nameET.setText("");
med_potencyET.setText("");
med_qtyET.setText("0");
adapter.notifyDataSetChanged();
Log.d("data",String.valueOf(medOrderList.size()));
}
else
{
Toast.makeText(getApplicationContext(), "Please fill all empty fields then press \'Add More\' button", Toast.LENGTH_SHORT).show();
}
}
});
}
else
{
Intent intent = new Intent(Home.this, SignIn.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
}
private class ListViewHolder{
EditText medET;
EditText potencyET;
EditText incrOrDecET;
ImageButton cancelBtn;
}
class CustomAdapter extends ArrayAdapter<MedicineBean> {
private Context context;
private List<MedicineBean> medicineBeanList;
ListViewHolder listViewHolder;
public CustomAdapter(Context context, int resource, ArrayList<MedicineBean> objects) {
super(context, resource, objects);
this.context = context;
this.medicineBeanList = objects;
}
@Override
public int getCount() {
return medicineBeanList.size();
}
@Override
public MedicineBean getItem(int i) {
return medicineBeanList.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(final int i, View view, ViewGroup viewGroup) {
if(view==null) {
view = getLayoutInflater().inflate(R.layout.single_item, null);
listViewHolder = new ListViewHolder();
listViewHolder.medET = (EditText) view.findViewById(R.id.mediceNameInHomeList);
listViewHolder.potencyET = (EditText) view.findViewById(R.id.medicePotencyInHomeList);
listViewHolder.incrOrDecET = (EditText) view.findViewById(R.id.mediceQtyInHomeList);
listViewHolder.cancelBtn = (ImageButton) findViewById(R.id.homeCnclBtn);
listViewHolder.medET.setEnabled(false);
listViewHolder.potencyET.setEnabled(false);
listViewHolder.incrOrDecET.setEnabled(false);
view.setTag(listViewHolder);
listViewHolder.medET.setText(medicineBeanList.get(i).getMediceneName());
listViewHolder.potencyET.setText(medicineBeanList.get(i).getMedicenePotency());
listViewHolder.incrOrDecET.setText(medicineBeanList.get(i).getMediceneQty());
listViewHolder.cancelBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
adapter.remove(medicineBeanList.get(i));
adapter.notifyDataSetChanged();
/*for (int pos = 0; pos < medOrderList.size(); pos++)
{
listViewHolder.medET.setText(medOrderList.get(pos).getMediceneName());
listViewHolder.potencyET.setText(medOrderList.get(pos).getMedicenePotency());
listViewHolder.incrOrDecET.setText(medOrderList.get(pos).getMediceneQty());
//adapter.add(new MedicineBean(medOrderList.get(pos).getCust_id(),medOrderList.get(pos).getMediceneName(),medOrderList.get(pos).getMedicenePotency(),medOrderList.get(pos).getMediceneQty()));
}*/
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
return view;
}
}
}
答案 0 :(得分:0)
只需将适配器代码替换为以下代码,然后阅读我在此代码中提到的注释
class CustomAdapter extends ArrayAdapter<MedicineBean> {
private Context context;
private List<MedicineBean> medicineBeanList;
ListViewHolder listViewHolder;
public CustomAdapter(Context context, int resource, ArrayList<MedicineBean> objects) {
super(context, resource, objects);
this.context = context;
this.medicineBeanList = objects;
}
@Override
public int getCount() {
return medicineBeanList.size();
}
@Override
public MedicineBean getItem(int i) {
return medicineBeanList.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(final int i, View view, ViewGroup viewGroup) {
if(view==null) {
view = getLayoutInflater().inflate(R.layout.single_item, null);
listViewHolder = new ListViewHolder();
listViewHolder.medET = (EditText) view.findViewById(R.id.mediceNameInHomeList);
listViewHolder.potencyET = (EditText) view.findViewById(R.id.medicePotencyInHomeList);
listViewHolder.incrOrDecET = (EditText) view.findViewById(R.id.mediceQtyInHomeList);
listViewHolder.cancelBtn = (ImageButton) findViewById(R.id.homeCnclBtn);
listViewHolder.medET.setEnabled(false);
listViewHolder.potencyET.setEnabled(false);
listViewHolder.incrOrDecET.setEnabled(false);
view.setTag(listViewHolder);
//Add this line
listViewHolder.cancelBtn.setTag(i);
listViewHolder.medET.setText(medicineBeanList.get(i).getMediceneName());
listViewHolder.potencyET.setText(medicineBeanList.get(i).getMedicenePotency());
listViewHolder.incrOrDecET.setText(medicineBeanList.get(i).getMediceneQty());
listViewHolder.cancelBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
//add this line
int selectedPos = (int) v.getTag();
//Replace this
//adapter.remove(medicineBeanList.get(i));
//with this
adapter.remove(medicineBeanList.get(selectedPos));
adapter.notifyDataSetChanged();
/*for (int pos = 0; pos < medOrderList.size(); pos++)
{
listViewHolder.medET.setText(medOrderList.get(pos).getMediceneName());
listViewHolder.potencyET.setText(medOrderList.get(pos).getMedicenePotency());
listViewHolder.incrOrDecET.setText(medOrderList.get(pos).getMediceneQty());
//adapter.add(new MedicineBean(medOrderList.get(pos).getCust_id(),medOrderList.get(pos).getMediceneName(),medOrderList.get(pos).getMedicenePotency(),medOrderList.get(pos).getMediceneQty()));
}*/
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
return view;
}
}
}
答案 1 :(得分:0)
从列表中删除值,然后刷新适配器。
listViewHolder.cancelBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
String item = medicineBeanList.get(i);
medicineBeanList.remove(position);
adapter.notifyDataSetChanged();}
catch (Exception e) {
e.printStackTrace();
}
}
});
答案 2 :(得分:0)
我无法解决任何问题,然后我决定在项目删除列表视图时使用“ Refeshesh Mean”,其中“ Refeshesh Mean”列表视图不可见,然后在新适配器集下可见。 因为当我删除listview项目。该项目是从视图中的唯一数组完美删除。 多数民众赞成在删除后我引用列表视图。 这样。
listView.setVisibility(ListView.VISIBLE);
adapter = new CustomAdapter(this,0,medOrderList);
listView.setAdapter(adapter);