我一直试图解决这个问题大约2周,但根本没有运气。我尝试使用onClickListener
在我的AlertDialog
中添加确认消息,而AlertDialog的setPositiveButton
将会执行我想要发生的事情,但是它会弄乱int position
中的getView
值。它不是获取我点击的项目的位置,而是获取最后一项的位置。我尝试在Toast
(onClick
内的 NOT )中发送AlertDialog
消息,并返回正确的位置,但是当我尝试Toast
时AlertDialog
内的消息会更改位置值。我坚持,不知道如何做这项工作。
这是我的Adapter.Class
public class myOrderAdapter extends ArrayAdapter<myOrder> {
AlertDialog.Builder builder;
AlertDialog ad;
public class ViewHolder{
TextView tableNum;
TextView itemName;
TextView itemQuantity;
TextView custName;
Button btnStatus;
}
public myOrderAdapter(Context context, ArrayList<myOrder> orderList) {
super(context, 0,orderList);
}
@Override
public View getView(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());
builder = new AlertDialog.Builder(Order.context);
builder.setTitle("Confirmation");
builder.setIcon(R.drawable.ic_confirm);
builder.setMessage("Is this Ready to Serve?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(getContext(),orderList.getOrder_id()+ "+" + orderList.getItem_name() + "+" + position,Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
ad = builder.create();
viewHolder.btnStatus.setText("Ready");
viewHolder.btnStatus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getContext(),orderList.getOrder_id()+ "+" + orderList.getItem_name() + "+" + position,Toast.LENGTH_SHORT).show();
ad.show();
}
});
return convertView;
}
}
注意:
我在Toast
中设置Button.onClickListener
消息的方式与builder.setPositiveButton
中的to_prepend = [StructField("field_0", StringType(), True)]
StructType(to_prepend + df.schema.fields)
相同,以检查值是否发生变化,是的确发生了变化。需要一些帮助和指导,谢谢。
答案 0 :(得分:1)
这里的问题是,您为每个创建的视图创建了AlertDialog,即使是最后一个视图(对于最后一个项目),这也是您将该位置作为项目列表的最后一个索引的原因。尝试类似下面的内容,这只会在您从其中一个视图中单击按钮时创建对话框。
public class myOrderAdapter extends ArrayAdapter<myOrder> {
public class ViewHolder{
TextView tableNum;
TextView itemName;
TextView itemQuantity;
TextView custName;
Button btnStatus;
}
public myOrderAdapter(Context context, ArrayList<myOrder> orderList) {
super(context, 0,orderList);
}
@Override
public View getView(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());
viewHolder.btnStatus.setText("Ready");
viewHolder.btnStatus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getContext(),orderList.getOrder_id()+ "+" + orderList.getItem_name() + "+" + position,Toast.LENGTH_SHORT).show();
createDialog(position);
}
});
return convertView;
}
private void createDialog(final int position) {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("Confirmation " + position);
builder.setIcon(R.drawable.pencil);
builder.setMessage("Is this Ready to Serve? " + position);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(getContext(),orderList.getOrder_id()+ "+" + orderList.getItem_name() + "+" + position, Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
builder.create().show();
}
}