2013-06-21 297 views
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));}}); 
+0

你應該完全按照你的代碼粘貼你的代碼。這是你如何擁有它? –

+0

好吧...這是很多。 – SmulianJulian

+0

您不必全部粘貼,但可以按照正確的順序粘貼。 –

回答

0

我解決了這個與這個有很多從SO社會的幫助和我通過發送該項目從列表,而不是長單擊列表項本身的按鈕刪除其他的問題。忽略許多字符串,你會發現這是一個比我最初嘗試做的更好的解決方案,特別是因爲用戶無法總是看到ListView中存在多少插入它後的內容。

lay18.setOnLongClickListener(new OnLongClickListener() { 
public boolean onLongClick(View v)             { 
    LayoutInflater myLayout = LayoutInflater.from(context); 
    final View dialogView = myLayout.inflate(R.layout.alerthelp, null); 
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context); 
    alertDialogBuilder.setView(dialogView); 
    final AlertDialog alertDialog = alertDialogBuilder.create(); 



    Button button5 = (Button) dialogView.findViewById(R.id.button5); 
    button5.setOnClickListener(new OnClickListener(){ 
    public void onClick(View v) { 

    TextView ss18=(TextView)findViewById(R.id.s18);//change 
    String sss18 = ss18.getText().toString(); //change 

    TextView texti18 = (TextView)findViewById(R.id.inputPrice);//change 
    String stringi18 = texti18.getText().toString();//change 
    Double ip18 = Double.parseDouble(stringi18);//change 
    if (ip18 > 0){//change 
    if (sss18.equals("st")) {//change 
    TextView stax18=(TextView)findViewById(R.id.salestax2);//change 
    String sax18 = stax18.getText().toString();//change 
    double sx18 = Double.parseDouble(sax18);//change 
    TextView textviewlay18 =(TextView)findViewById(R.id.m4ac);//change 
    String stringl18 = textviewlay18.getText().toString();//change 
    Double doubl18 = Double.parseDouble(stringl18);//change 
    TextView textviewp18 = (TextView)findViewById(R.id.inputPrice);//change 
    String stringp18 = textviewp18.getText().toString();//change 
    Double intp18 = Double.parseDouble(stringp18);//change 
    double resultl18 = intp18 - (doubl18*sx18);//change 
    int precision21t = 100; //keep 4 digits//change 
    resultl18= Math.floor(resultl18 * precision21t +.5)/precision21t;//change 
    textViewtotalproduct.setText(String.valueOf(resultl18));//change 
    if 
    (listItems != null) 
    try{ 
    final String str=lv.getItemAtPosition(pos).toString(); 
    Log.i("ListView", "onLongListItemClick string=" + str); 
    {listItems.remove(pos); 
    TextView text18 =(TextView)findViewById(R.id.TextView18); 
    dally18=(dally18-1); 
    text18.setText(String.valueOf(dally18)); 
    d18.setText(String.valueOf(dally18)); 
    adapter.notifyDataSetChanged(); 
    controller1.deletename(stringl18);} 
        } 
        catch (IndexOutOfBoundsException ex) 
        { 
        } 

    alertDialog.dismiss();//change 
    }else{ 
    if (sss18.equals("")) {//change 
    TextView textviewlay18 =(TextView)findViewById(R.id.m4ac);//change 
    String stringl18 = textviewlay18.getText().toString();//change 
    Double doubl18 = Double.parseDouble(stringl18);//change 
    TextView textviewp18 = (TextView)findViewById(R.id.inputPrice);//change 
    String stringp18 = textviewp18.getText().toString();//change 
    Double intp18= Double.parseDouble(stringp18);//change 
    double resultl18 = intp18 - doubl18;//change 
    int precision21t = 100; //keep 4 digits//change 
    resultl18= Math.floor(resultl18 * precision21t +.5)/precision21t;//change 
    textViewtotalproduct.setText(String.valueOf(resultl18));//change 
    if 
    (listItems != null) 
    try{ 
        final String str=lv.getItemAtPosition(pos).toString(); 
        Log.i("ListView", "onLongListItemClick string=" + str); 
        {listItems.remove(pos); 
        TextView text18 =(TextView)findViewById(R.id.TextView18); 
        dally18=(dally18-1); 
        text18.setText(String.valueOf(dally18)); 
        d18.setText(String.valueOf(dally18)); 
        adapter.notifyDataSetChanged(); 
        controller1.deletename(stringl18);} 
        } 
        catch (IndexOutOfBoundsException ex) 
        { 
        } 
         alertDialog.dismiss();}}}}});     



         alertDialog.show(); 
         return true;}});