2014-01-28 14 views
0

我有一個的ListView其中有三個的TextView每一行(IDPRODUCT_NAMEPRODUCT_PRICE如何可以找到提高的ID號在列表視圖中點擊後刪除按鈕

我在從數據庫中顯示產品名稱產品價格,並通過增加arrayList.size()+ 1來顯示id增加(1,2,3,4)。

刪除每一行中的按鈕。

當用戶點擊刪除按鈕爲(id - 3)刪除行時,我找不到Id以顯示增加的數字(1,2,3)。

現在我的列表視圖顯示(1,2,4)。我該怎麼做?

QuickKeysAdapter

public class QuickKeysAdapter extends BaseAdapter 
{ 
private Context mContext; 
private List<sellProductInfo> dataQuickKeys; 
private List<String> imageQuickKeys; 
private LayoutInflater inflater; 

private List<sellOrderDetailInfo> _listsellOrderDetailInfo; 
private sellProductLogic _sellProductLogic; 

public String flagDelete = ""; 

public QuickKeysAdapter(Context paramContext, 
     List<sellProductInfo> paramList1, List<String> paramList2) { 
    this.mContext = paramContext; 
    this.dataQuickKeys = paramList1; 
    this.imageQuickKeys = paramList2; 

    Log.v("QKey", "Hay Chan! i in QuickKeysAdapter()"); 
} 

@Override 
public int getCount() { 
    return (dataQuickKeys == null) ? 0 : dataQuickKeys.size(); 
} 

@Override 
public Object getItem(int paramInt) { 
    // TODO Auto-generated method stub 
    return paramInt; 
} 

@Override 
public long getItemId(int paramInt) { 
    // TODO Auto-generated method stub 
    return paramInt; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup paramViewGroup) { 

    inflater = (LayoutInflater) mContext 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    ViewHolder viewHolder = null; 
    View gridView = null; 

    if (convertView == null) { 

     try { 
      gridView = new View(mContext); 

      gridView = inflater.inflate(R.layout.mobile, null); 

      TextView txtTitle = (TextView) gridView 
        .findViewById(R.id.grid_item_label); 

      ImageView albumImage = (ImageView) gridView 
        .findViewById(R.id.grid_item_image); 

      albumImage.setImageResource(R.drawable.c_f); // temporatory 

      viewHolder = new ViewHolder(); 
      viewHolder.imgview = albumImage; 
      viewHolder.txtname = txtTitle; 
      viewHolder._linearLayout = (LinearLayout) gridView 
        .findViewById(R.id.linear_layout); 

      gridView.setTag(viewHolder); 

     } catch (Exception e) { 
      Log.v("view null", e.getMessage()); 
      e.printStackTrace(); 
     } 
    } else { 
     gridView = convertView; 
     viewHolder = (ViewHolder) gridView.getTag(); 
    } 

    viewHolder.txtname.setText(dataQuickKeys.get(position).getDescEng() 
      .toString()); 

    viewHolder._linearLayout.setOnClickListener(new OnClickQuickKeys(
      this.dataQuickKeys.get(position))); 

    return gridView; 
} 

class OnClickQuickKeys implements View.OnClickListener { 
    private String btnname; 
    private String product_code; 

    public OnClickQuickKeys(sellProductInfo sellProductInfo) { 
     this.btnname = sellProductInfo.getDescEng(); 
     this.product_code = sellProductInfo.getProductCode(); 
     Log.d("OnClick", "Chan! I'm Constructor"); 

     Log.d("OnClick1", "Chan2! " + btnname + " " + product_code); 
    } 

    @Override 
    public void onClick(View v) { 

     Log.d("OnClick", product_code.toString()); 

     Toast.makeText(mContext, btnname, Toast.LENGTH_LONG).show(); 

     _sellProductLogic = new sellProductLogic(mContext); 
     _listsellOrderDetailInfo = _sellProductLogic 
       .getallsellProductDetailLogic(product_code); 

     MainActivity._PrdSellListAdapter = new PrdSellListAdapter(mContext, 
       MainActivity._listsellOrderDetailInfo); 
     MainActivity.lvprdcurrentsell 
       .setAdapter(MainActivity._PrdSellListAdapter); 


     Log.v("CC", "Chan , onclick call me orderdetail = " 
       + MainActivity._listsellOrderDetailInfo.size() + 1); 

     int prdId = MainActivity._listsellOrderDetailInfo.size() + 1; 
     String a = Integer.toString(prdId); 

     MainActivity._sellOrderDetailInfo = new sellOrderDetailInfo(); 
     MainActivity._sellOrderDetailInfo 
       .setPrdname(_listsellOrderDetailInfo.get(0).getPrdname() 
         .toString()); 
     MainActivity._sellOrderDetailInfo 
       .setPrditemprice(_listsellOrderDetailInfo.get(0) 
         .getPrditemprice()); 

     MainActivity._sellOrderDetailInfo.setPrdId(prdId); 

     // 
     MainActivity._listsellOrderDetailInfo 
       .add(MainActivity._sellOrderDetailInfo); 

     Log.v("InClick", "Chan , onclick call me orderdetail = " + prdId); 

     if (flagDelete == "U") { 
      for (int i = 1; i <= MainActivity._listsellOrderDetailInfo 
        .size(); i++) { 
       MainActivity._sellOrderDetailInfo.setPrdId(i); 

      } 
     } 

    } 
} 

public static class ViewHolder { 
    TextView txtname; 
    ImageView imgview; 
    LinearLayout _linearLayout; 
} 
} 
} 

prdsellListadapter類

public class PrdSellListAdapter extends BaseAdapter { 

Context mContext; 
private TableLayout tblLayout; 
private LayoutInflater inflater; 
public static ListView lvprdcurrentsell; 

private List<sellOrderDetailInfo> _sellOrderDetailInfo; 
private sellProductLogic _sellProductLogic; 

public PrdSellListAdapter(Context paramContext, 
     List<sellOrderDetailInfo> paramList1) { 

    // this.inflater = LayoutInflater.from(paramContext); 
    this.inflater = (LayoutInflater) paramContext 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    this._sellOrderDetailInfo = paramList1; 
    this.mContext = paramContext; 

    int prdId = MainActivity._listsellOrderDetailInfo.size(); 
    Log.v("PSlA", "Hay Chan ! Don't Worry , i constructor "); 
} 

public PrdSellListAdapter(List<sellOrderDetailInfo> _sellOrderDetailInfo) { 
    this._sellOrderDetailInfo = _sellOrderDetailInfo; 
} 

@Override 
public int getCount() {  

    if (_sellOrderDetailInfo.size() <= 0) { 
     return 0; 
    } else { 
     return _sellOrderDetailInfo.size(); 
    } 
} 

@Override 
public Object getItem(int position) { 
    // TODO Auto-generated method stub 
    return null; 
} 

@Override 
public long getItemId(int position) { 
    // TODO Auto-generated method stub 
    return 0; 
} 

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 
    final ViewHolder localViewHolder; 

    if (convertView == null) {  

     convertView = inflater.inflate(R.layout.currentsellprdlist, null); 
     localViewHolder = new ViewHolder();   

     localViewHolder.txtPrdId = (TextView) convertView 
       .findViewById(R.id.txtPrdId); 
     localViewHolder.txtPrdName = (TextView) convertView 
       .findViewById(R.id.txtPrdName); 
     localViewHolder.txtPrdQty = (TextView) convertView 
       .findViewById(R.id.txtPrdQty); 
     localViewHolder.txtPrdPrice = (TextView) convertView 
       .findViewById(R.id.txtPrdPrice); 
     localViewHolder.txtPrdTotal = (TextView) convertView 
       .findViewById(R.id.txtPrdTotal); 
     localViewHolder.buttonprddeletecs = (Button) convertView 
       .findViewById(R.id.btnDelete); 

     convertView.setTag(localViewHolder); 
    } else { 
     localViewHolder = (ViewHolder) convertView.getTag(); 
     Log.v("PSlA", "Hay Chan ! I'm not null convertView "); 
    } 

    // set value into localviewhloder'variable  

    if (_sellOrderDetailInfo.size() <= 0) { 

    } else {   

     localViewHolder.txtPrdId.setText(Integer 
       .toString(_sellOrderDetailInfo.get(position).getPrdId())); 
     localViewHolder.txtPrdName.setText(_sellOrderDetailInfo 
       .get(position).getPrdname().toString()); 
     localViewHolder.txtPrdPrice.setText(_sellOrderDetailInfo 
       .get(position).getPrditemprice().toString()); 

     Log.v("PSlA", 
       "Hay Chan ! I'm get view :) " + _sellOrderDetailInfo.size()); 

     localViewHolder.buttonprddeletecs 
       .setOnClickListener(new OnClickListener() { 
        @Override 
        public void onClick(View view) { 

         AlertDialog.Builder alert = new AlertDialog.Builder(
           view.getContext()); 

         alert.setMessage("Are you sure you want to delete?"); 

         alert.setNegativeButton("CANCEL", 
           new DialogInterface.OnClickListener() { 
            @Override 
            public void onClick(
              DialogInterface dialog, 
              int whichButton) { 
             dialog.cancel(); 
            } 
           }); 

         alert.setPositiveButton("OK", 
           new DialogInterface.OnClickListener() { 
            @Override 
            public void onClick(
              DialogInterface dialog, 
              int whichButton) { 
             // I get from the bundle the Id of 
             // my employee 
             PrdSellListAdapter.this._sellOrderDetailInfo 
               .remove(position); 
             // 
             PrdSellListAdapter.this 
               .notifyDataSetChanged(); 

             Toast.makeText(
               mContext, 
               "Delete button Clicked : " 
                 + position, 
               Toast.LENGTH_LONG).show(); 
            } 
           }); 

         AlertDialog alertDialog = alert.create(); 
         alertDialog.show(); 

        } 
       }); 

    } 
    return convertView; 
} 

public static class ViewHolder { 
    Button buttonprddeletecs; 
    TextView txtPrdId; 
    TextView txtPrdName; 
    TextView txtPrdQty; 
    TextView txtPrdPrice; 
    TextView txtPrdTotal; 
} 

}

回答

0

如果在ID想在1,2,3順序,爲什麼不使用getView功能位置顯示ID?

0

現在,我更新了prdsellListadapter類中的一些代碼getview方法。它適用於我點擊刪除後獲得序列號。

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 

    Log.v("PSlA", "Hay Chan ! Don't Worry .I'm " + position); 

    // final ViewHolder localViewHolder; 

    final ViewHolder localViewHolder; 
    View gView = null; 

    if (convertView == null) { 

     Log.v("PSlA", "Hay Chan ! I'm null convertView "); 

     gView = new View(mContext); 
     gView = inflater.inflate(R.layout.currentsellprdlist, null); 

     localViewHolder = new ViewHolder(); 

     Log.v("PSlA", "Hay Chan ! I'm null convertView 1"); 

     localViewHolder.txtPrdId = (TextView) gView 
       .findViewById(R.id.txtPrdId); 
     localViewHolder.txtPrdName = (TextView) gView 
       .findViewById(R.id.txtPrdName); 
     localViewHolder.txtPrdQty = (TextView) gView 
       .findViewById(R.id.txtPrdQty); 
     localViewHolder.txtPrdPrice = (TextView) gView 
       .findViewById(R.id.txtPrdPrice); 
     // localViewHolder.txtPrdTotal = (TextView) gView 
     // .findViewById(R.id.txtPrdTotal); 
     localViewHolder.buttonprddeletecs = (Button) gView 
       .findViewById(R.id.btnDelete); 

     localViewHolder._linearLayout = (LinearLayout) gView 
       .findViewById(R.id.linear_layout); 

     gView.setTag(localViewHolder); 

    } else { 
     gView = convertView; 
     localViewHolder = (ViewHolder) gView.getTag(); 
     Log.v("PSlA", "Hay Chan ! I'm not null convertView "); 
    } 

    if (_sellOrderDetailInfo.size() <= 0) { 
     Log.v("P", "chanchan i have no data"); 
    } else { 

     try { 
      localViewHolder.txtPrdId 
        .setText(Integer.toString(_sellOrderDetailInfo.get(
          position).getPrdId())); 
      localViewHolder.txtPrdName.setText(_sellOrderDetailInfo 
        .get(position).getPrdname().toString()); 
      localViewHolder.txtPrdPrice.setText(_sellOrderDetailInfo 
        .get(position).getPrditemprice().toString()); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     Log.v("PSlA", 
       "Hay Chan ! I'm get view :) " + _sellOrderDetailInfo.size()); 

     localViewHolder.buttonprddeletecs 
       .setOnClickListener(new OnClickListener() { 
        @Override 
        public void onClick(final View view) { 

         AlertDialog.Builder alert = new AlertDialog.Builder(
           view.getContext()); 

         alert.setMessage("Are you sure you want to delete?"); 

         alert.setNegativeButton("CANCEL", 
           new DialogInterface.OnClickListener() { 
            @Override 
            public void onClick(
              DialogInterface dialog, 
              int whichButton) { 
             dialog.cancel(); 
            } 
           }); 

         alert.setPositiveButton("OK", 
           new DialogInterface.OnClickListener() { 
            @Override 
            public void onClick(
              DialogInterface dialog, 
              int whichButton) { 

             PrdSellListAdapter.this._sellOrderDetailInfo 
               .remove(position); 

             localViewHolder.txtPrdName 
               .setText("chanlay"); 

             int a = PrdSellListAdapter.this._sellOrderDetailInfo 
               .size(); 

             PrdSellListAdapter.this 
               .notifyDataSetChanged(); // refresh_method 

             String aa = ""; 

             for (int i = 1; i <= PrdSellListAdapter.this._sellOrderDetailInfo 
               .size(); i++) { 
              aa = localViewHolder.txtPrdName 
                .getText().toString(); 
             } 
             PrdSellListAdapter.this 
               .notifyDataSetChanged(); // refresh_method 

             Log.v("pp", 
               localViewHolder.txtPrdName 
                 .getText() 
                 .toString()); 

             ButtonFlag = "D"; 
            } 
           }); 

         AlertDialog alertDialog = alert.create(); 
         alertDialog.show(); 

        } 
       }); 

     if (ButtonFlag == "D") { 
      for (int i = 1; i <= PrdSellListAdapter.this._sellOrderDetailInfo 
        .size(); i++) { 
       // localViewHolder.txtPrdId.setText(String.valueOf(i)); 

       localViewHolder.txtPrdId.setText(String 
         .valueOf(position + 1)); 

      } 
     } 
     Log.v("cp", localViewHolder.txtPrdName.getText().toString()); 
    } 
    return gView; 
} 
相關問題