从ListView中删除项目

时间:2013-06-21 19:45:16

标签: android android-listview onclick textview double

我有两个按钮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));}});

1 个答案:

答案 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;}});