我有两个按钮 Lay1 和 Lay2。当用户单击时,某些信息将被放入 Listview(lv) 和 textView(inputPrice) 中。当我从 ListView 中删除单个项目时,它从 textView 中的值被正确删除,但是当 ListView 中有两个项目并且我删除单个项目时,TextView 的值返回到 0。它应该仍然具有剩余的值物品。如何在我的 TextView 中保留剩余项目的值?
private double overallTotalproduct;
public static TextView resultTextView;
ArrayList<String> listItems=new ArrayList<String>();
ArrayAdapter<String> adapter;
adapter=new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,listItems);
setListAdapter(adapter);
ArrayList<String> ai= new ArrayList<String>();
ai = getIntent().getExtras().getStringArrayList("list");
if (ai != null) {listItems.add(ai+"");
adapter.notifyDataSetChanged();}
final TextView textViewtotalproduct = (TextView) findViewById(R.id.inputPrice);
resultTextView = (TextView) findViewById(R.id.resultTextView);;
final ListView lv = getListView();
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> av, View v, int pos, long id) {
return onLongListItemClick(v,pos,id);}
protected boolean onLongListItemClick(View v, final int pos, long id) {
final String str=lv.getItemAtPosition(pos).toString();
Log.i("ListView", "onLongListItemClick string=" + str);
AlertDialog.Builder builder = new
AlertDialog.Builder(MenuView2Activity.this);
builder.setMessage("Are you sure you want to remove this Item?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id)
{listItems.remove(pos);
adapter.notifyDataSetChanged();
TextView textviewlay1 =(TextView)findViewById(R.id.m1aa);
String stringl1 = textviewlay1.getText().toString();
Double doubl1 = Double.parseDouble(stringl1);
TextView textviewp1 = (TextView)findViewById(R.id.inputPrice);
String stringp1 = textviewp1.getText().toString();
Double intp1 = Double.parseDouble(stringp1);
resultl1 = intp1 - doubl1;
textViewtotalproduct.setText(String.valueOf(resultl1));
TextView textviewlay2 =(TextView)findViewById(R.id.m1ab);
String stringl2 = textviewlay2.getText().toString();
Double doubl2 = Double.parseDouble(stringl2);
TextView textviewp2 = (TextView)findViewById(R.id.inputPrice);
String stringp2 = textviewp2.getText().toString();
Double intp2 = Double.parseDouble(stringp2);
double resultl2 = intp2 - doubl2;
textViewtotalproduct.setText(String.valueOf(resultl2));}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
return true;
}
});
lay1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
TextView textView1 = (TextView) findViewById(R.id.m1aa);
String stringm1aa = textView1.getText().toString();
Double intm1aa = Double.parseDouble(stringm1aa);
TextView t1 =(TextView)findViewById(R.id.inputPrice);
String t11 = t1.getText().toString();
Double int11 = Double.parseDouble(t11);
listItems.add("1 "+stringm1a+" - "+intm1aa );
adapter.notifyDataSetChanged();
overallTotalproduct = intm1aa + int11 ;
textViewtotalproduct.setText(String.valueOf(overallTotalproduct));}});
lay2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
TextView textView2 = (TextView) findViewById(R.id.m1ab);
String stringm1ab = textView2.getText().toString();
Double intm1ab = Double.parseDouble(stringm1ab);
TextView t2 =(TextView)findViewById(R.id.inputPrice);
String t12 = t2.getText().toString();
Double int12 = Double.parseDouble(t12);
listItems.add("1"+stringm1b+"-"+ intm1ab);
adapter.notifyDataSetChanged();
overallTotalproduct = intm1ab + int12;
textViewtotalproduct.setText(String.valueOf(overallTotalproduct));}});