我想实现一个ListView,它在每行中都有Delete Btn。
我唯一的问题是当我点击某行的删除Btn时,位置0的行只是已删除!
我认为我在getView中的Position参数无法更新并始终具有0值
我该怎么办?!
感谢。
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
//import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class InvoiceListAdapter extends BaseAdapter {
ArrayList<Object> _itemList;
public Activity _context;
public LayoutInflater _inflater;
public InvoiceListAdapter(Activity context,ArrayList<Object> itemList)
{
super();
this._context=context;
this._itemList=itemList;
this._inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return _itemList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return _itemList.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public static class ViewHolder
{
TextView ProductName;
TextView Qnt;
TextView Price;
Button Del;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null)
{
holder = new ViewHolder();
convertView = _inflater.inflate(R.layout.custom_row_view, null);
holder.ProductName = (TextView) convertView.findViewById(R.id.txt_CRow_ProdName);
holder.Price = (TextView) convertView.findViewById(R.id.txt_CRow_Price);
holder.Qnt = (TextView) convertView.findViewById(R.id.txt_CRow_Qnt);
holder.Del = (Button) convertView.findViewById(R.id.btn_CRow_Delete);
/*-----------------------------Deleting Item with Button--------------------*/
holder.Del.setTag(holder);
holder.Del.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(_context,"Item Deleted!", Toast.LENGTH_SHORT).show();
_itemList.remove(position);
notifyDataSetChanged();
// TODO Auto-generated method stub
}
});
convertView.setTag(holder);
}
else
{
holder=(ViewHolder)convertView.getTag();
}
AnItem Item = (AnItem) _itemList.get(position);
holder.ProductName.setText(Item.getProductName());
holder.Price.setText(Item.getPrice());
holder.Qnt.setText(Item.getQnt());
return convertView;
}
答案 0 :(得分:11)
您可能在ScrollView中拥有该ListView。我只是浪费了2个小时,直到我偶然发现了这个答案:
Why is my BaseAdapter class not incrementing the position in getView?
答案 1 :(得分:3)
if(convertView==null)
{
holder = new ViewHolder();
convertView = _inflater.inflate(R.layout.custom_row_view, null);
holder.ProductName = (TextView) convertView.findViewById(R.id.txt_CRow_ProdName);
holder.Price = (TextView) convertView.findViewById(R.id.txt_CRow_Price);
holder.Qnt = (TextView) convertView.findViewById(R.id.txt_CRow_Qnt);
holder.Del = (Button) convertView.findViewById(R.id.btn_CRow_Delete);
/*-----------------------------Deleting Item with Button--------------------*/
holder.Del.setTag(holder);
convertView.setTag(holder);
}
else
{
holder=(ViewHolder)convertView.getTag();
}
AnItem Item = (AnItem) _itemList.get(position);
holder.ProductName.setText(Item.getProductName());
holder.Price.setText(Item.getPrice());
holder.Qnt.setText(Item.getQnt());
holder.Del.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(_context,"Item Deleted!", Toast.LENGTH_SHORT).show();
_itemList.remove(position);
notifyDataSetChanged();
// TODO Auto-generated method stub
}
});
return convertView;
我认为onClickListener不在if块内。
答案 2 :(得分:1)
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null)
{
holder = new ViewHolder();
convertView = _inflater.inflate(R.layout.custom_row_view, null);
holder.ProductName = (TextView) convertView.findViewById(R.id.txt_CRow_ProdName);
holder.Price = (TextView) convertView.findViewById(R.id.txt_CRow_Price);
holder.Qnt = (TextView) convertView.findViewById(R.id.txt_CRow_Qnt);
holder.Del = (Button) convertView.findViewById(R.id.btn_CRow_Delete);
/*-----------------------------Deleting Item with Button--------------------*/
holder.Del.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(_context,"Item Deleted!", Toast.LENGTH_SHORT).show();
Integer position = (Integer) v.getTag();
_itemList.remove(position.intValue());
notifyDataSetChanged();
// TODO Auto-generated method stub
}
});
convertView.setTag(holder);
}
else
{
holder=(ViewHolder)convertView.getTag();
}
AnItem Item = (AnItem) _itemList.get(position);
holder.ProductName.setText(Item.getProductName());
holder.Price.setText(Item.getPrice());
holder.Qnt.setText(Item.getQnt());
holder.Del.setTag(Integer.valueOf(position));
return convertView;
}