我尝试使用ListView
内的按钮删除ArrayAdapter
中显示的项目。我在getPosition
中看到了ArrayAdapter
方法,它返回了项目的索引号,但我不知道在ArrayAdapter
的remove方法中使用它。我怎样才能做到这一点?需要帮助。
arrayAdapter
public class myOrderAdapter extends ArrayAdapter<myOrder> {
ArrayList<myOrder> list;
public class ViewHolder{
TextView tableNum;
TextView itemName;
TextView itemQuantity;
TextView custName;
Button btnStatus;
}
public myOrderAdapter(Context context, ArrayList<myOrder> orderList) {
super(context, 0,orderList);
this.list = orderList;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final myOrder orderList = getItem(position);
ViewHolder viewHolder;
viewHolder = new ViewHolder();
if(convertView == null){
convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_layout,parent,false);
viewHolder.tableNum = (TextView)convertView.findViewById(R.id.tableNum);
viewHolder.itemName = (TextView)convertView.findViewById(R.id.itemName);
viewHolder.custName = (TextView)convertView.findViewById(R.id.custName);
viewHolder.itemQuantity = (TextView)convertView.findViewById(R.id.itemQuantity);
viewHolder.btnStatus = (Button)convertView.findViewById(R.id.statusButton);
convertView.setTag(viewHolder);
}
else {
viewHolder = (ViewHolder)convertView.getTag();
}
viewHolder.tableNum.setText("Order for Table: "+orderList.getTable_id());
viewHolder.itemName.setText(orderList.getItem_name());
viewHolder.custName.setText("Customer Name: " + orderList.getCust_name());
viewHolder.itemQuantity.setText("QTY: " + orderList.getQuantity());
if(orderList.getStatus_id() == 1){
viewHolder.btnStatus.setText("Ready");
viewHolder.btnStatus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getContext(),"" + getPosition(orderList),Toast.LENGTH_SHORT).show();
// Remove item codes here..
list.remove(position);
}
});
notifyDataSetChanged();
}
return convertView;
}
答案 0 :(得分:0)
viewHolder.btnStatus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(view.getContext(),"" + getPosition(orderList),Toast.LENGTH_SHORT).show();
list.remove(position); //List/ArrayList of order in your program.
}
});
notifyDataSetChanged();
答案 1 :(得分:0)
ArrayAdapter还有一个remove(T Object)方法,可以从列表中删除该项,也可以从ArrayList中删除它。 首先在getView中获取订单
final myOrder order = getItem(position);
orderList是您在Constructor中传递的ArrayList。
viewHolder.btnStatus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Your Toast message
orderList.remove(position);
}
});
notifyDataSetChanged();