0

Lay1 と Lay2 の 2 つのボタンがあります。ユーザーがクリックすると、いくつかの情報が Listview(lv) と textView(inputPrice) に入れられます。ListView から 1 つの項目を削除すると、textView の値は正しく削除されますが、ListView に 2 つの項目があり、1 つの項目を削除すると、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));}});
4

1 に答える 1