我正在使用ArrayAdapter当我从Listview中删除第一个项目时,它会完全删除。但是当我从listview中删除第二个项目时。它不完全删除。 我怎么能这样做?
适配器编码
import android.widget.ArrayAdapter;
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// final CartBean beans = getItem(position);
View view = convertView;
final ViewHolder viewHolder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.list_cart_row, parent, false);
viewHolder = new ViewHolder();
viewHolder.row_price = (TextView) view.findViewById(R.id.row_price);
viewHolder.et_quantity = (EditText) view.findViewById(R.id.cart_quantity);
viewHolder.row_item_name = (TextView) view.findViewById(R.id.row_item_name);
viewHolder.deleteButton = (ImageView) view.findViewById(R.id.iv_delete);
// viewHolder.rastaurantoffer = (ImageView) view.findViewById(R.id.rastaurantname2);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
if (viewHolder.textWatcher != null)
viewHolder.et_quantity.removeTextChangedListener(viewHolder.textWatcher);
final CartBean bean = getItem(position);
viewHolder.textWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (charSequence.length()>0) {
viewHolder.row_price.setText(String.valueOf(df.format(Double.parseDouble(bean.getTotal_price()) * Integer.parseInt(charSequence.toString()))));
AppConstants.cartBeanArrayList.get(position).setQuantity(Integer.parseInt(charSequence.toString()));
}
}
@Override
public void afterTextChanged(Editable editable) {
Double totalPrice=0.0;
for (int i=0;i<AppConstants.cartBeanArrayList.size();i++)
{
totalPrice=totalPrice+(Double.parseDouble(AppConstants.cartBeanArrayList.get(i).getTotal_price()) * AppConstants.cartBeanArrayList.get(i).getQuantity());
Log.i("total_price12356",""+total_price);
}
ActivityCart.tv_sub_total.setText("£. "+String.valueOf(df.format(totalPrice)));
}
};
viewHolder.row_price.setText("£."+String.valueOf(df.format(Double.parseDouble(cartBeans.get(position).getTotal_price()) * cartBeans.get(position).getQuantity())));
viewHolder.et_quantity.setText(String.valueOf(cartBeans.get(position).getQuantity()));
viewHolder.row_item_name.setText(cartBeans.get(position).getItem_name());
viewHolder.et_quantity.addTextChangedListener(viewHolder.textWatcher);
if(isDeleteRequired){
viewHolder.deleteButton.setVisibility(View.VISIBLE);
viewHolder.et_quantity.setInputType(InputType.TYPE_CLASS_NUMBER);
viewHolder.et_quantity.setFocusableInTouchMode(true);
viewHolder.et_quantity.setCursorVisible(true);
viewHolder.deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AppConstants.cartBeanArrayList.remove(position);
notifyDataSetChanged();
for (int i = 0; i < AppConstants.cartBeanArrayList.size(); i++) {
// String price = AppConstants.cartBeanArrayList.get(i).getTotal_price().substring(3, AppConstants.cartBeanArrayList.get(i).getTotal_price().length());
// Log.i("total_price123",""+price);
total_price = total_price + ActivityCart.getTotal(i);
Log.i("total_price123",""+total_price);
Log.i("total_price1237",""+AppConstants.cartBeanArrayList.size());
// AppConstants.cartBeanArrayList.clear();
}
Log.i("total_price1234",""+total_price);
ActivityCart.tv_sub_total.setText("£. "+String.valueOf(total_price));
if (AppConstants.cartBeanArrayList.size()==0)
{
ActivityCart.tv_sub_total.setText("£.0.00");
}
// AppConstants.addressBeanArrayList.setText(String.valueOf(total_price));
}
});
}else{
viewHolder.deleteButton.setVisibility(View.GONE);
}
return view;
}
class ViewHolder {
TextView row_item_name;
TextView row_price;
EditText et_quantity;
ImageView deleteButton;
public TextWatcher textWatcher;
}
public void setIsDeleteRequired(boolean isDeleteRequire){
isDeleteRequired = isDeleteRequire;
}
}
getTotal()方法编码是:
public static Double getTotal(int i) {
total =(Double.parseDouble(AppConstants.cartBeanArrayList.get(i).getTotal_price()) * AppConstants.cartBeanArrayList.get(i).getQuantity());
Log.i("totalsunder", "" +total);
return total;
}
答案 0 :(得分:0)
是的,我自己得到了答案。
这里是我的代码
viewHolder.deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AppConstants.cartBeanArrayList.remove(position);
for (int i = 0; i < AppConstants.cartBeanArrayList.size(); i++) {
total_price = total_price + ActivityCart.getTotal(i);
}
ActivityCart.tv_sub_total.setText("£. "+String.valueOf(total_price));
total_price =0.0;
notifyDataSetChanged();
}
});
答案 1 :(得分:0)
delete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//removing from database
int i =impl_cart.deleteRow(data_cart.get(position).get_id());
//removing from adapter
data_cart.remove(position);
notifyDataSetChanged();
}
});