0
我有兩個按鈕Lay1和Lay2。當用戶點擊某個信息放入ListView(lv)和textView(inputPrice)時。當我從ListView中刪除單個項目時,它從textView中的值被正確刪除,但是當兩個項目在ListView中時,我刪除了一個項目,TextView的值返回到0.它仍應具有剩餘的值項目。我如何保留剩餘項目的值在我的TextView中?從ListView中刪除項目
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));}});
你應該完全按照你的代碼粘貼你的代碼。這是你如何擁有它? –
好吧...這是很多。 – SmulianJulian
您不必全部粘貼,但可以按照正確的順序粘貼。 –