我有一個的ListView其中有三個的TextView每一行(ID,PRODUCT_NAME,PRODUCT_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;
}
}