列表视图位置onlicklistener的奇怪行为

时间:2017-04-08 09:00:47

标签: android listview

我有一个列表视图,显示某个餐馆的食品订单。当用户点击其中一个列表视图项时,它应该显示点击项目位置的祝酒词以及食物订单的订单ID。由于某种原因,显示错误的位置。

PendingOrders.java。数据从firebase加载

public class PendingOrders extends AppCompatActivity {

private DatabaseReference mDatabase;
ArrayList<OrderDetails> activeorders = new ArrayList<OrderDetails>();
private OrdersAdapter adapter;
ListView listview;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pending_orders);
    listview = (ListView)findViewById(R.id.pendinglistview);
    adapter = new OrdersAdapter(this,activeorders);
    listview.setAdapter(adapter);


    ValueEventListener listen = new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            OrderDetails order = dataSnapshot.getValue(OrderDetails.class);
            String str = order.UserId;

            if (str.equals("abc")) {
                boolean add = true;
                for (int i = 0; i < activeorders.size(); i++) {
                    String checking = activeorders.get(i).OrderId;
                    if (checking.equals(order.OrderId)) {
                        add = false;
                    }
                }

                if (add = true) {
                    activeorders.add(order);
                    ((OrdersAdapter) listview.getAdapter()).notifyDataSetChanged();
                }

            }
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {

        }

    };



    for (int i=1; i<=4;i++) {
        mDatabase = FirebaseDatabase.getInstance().getReference().child("Active Orders")
                .child(Integer.toString(i));
        mDatabase.addValueEventListener(listen);
    }



}

}

OrdersAdapter.java

public class OrdersAdapter extends BaseAdapter {

Context context;
ArrayList<OrderDetails> activeorders;
Button confirmationbutton;

public OrdersAdapter(Context context, ArrayList<OrderDetails> activeorders){
    this.context = context;
    this.activeorders = activeorders;
}

@Override
public int getCount() {
    return  activeorders.size() ;
}

@Override
public Object getItem(int position) {
    return activeorders.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

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

    ViewHolder viewHolder = null;

    if (convertView == null) {

        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

        viewHolder = new ViewHolder();

        convertView = layoutInflater.inflate(R.layout.orderlistview, null);

        viewHolder.mdtitle = (TextView) convertView.findViewById(R.id.mdtitle);
        confirmationbutton = (Button) convertView.findViewById(R.id.confirmationbutton);
        confirmationbutton.setTag(position);
        viewHolder.switchbutton = (MultiStateToggleButton) convertView.findViewById(R.id.mstb_multi_id);
        viewHolder.switchbutton.setElements(R.array.planets_array,0);
        Typeface font = Typeface.createFromAsset(context.getAssets(), "fonts/Lato-Semibold.ttf");

        viewHolder.mdtitle.setTypeface(font);

        convertView.setTag(viewHolder);


    } else {

        viewHolder = (ViewHolder) convertView.getTag();


    }

    confirmationbutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Integer pos = (Integer)v.getTag();
            OrderDetails orders = (OrderDetails)getItem(pos);
            String orderingid = orders.OrderId;
            Toast.makeText(context, "Position :"+ Integer.toString(pos)+" orderid : "+orderingid,
                    Toast.LENGTH_SHORT).show();
            DatabaseReference mDatabase;
            mDatabase = FirebaseDatabase.getInstance().getReference();
            mDatabase.child("Active Orders").child(orderingid).child("Status").setValue("Cooking");

            FrameLayout vwParentRow = (FrameLayout)v.getParent();
            Button btnChild = (Button)vwParentRow.getChildAt(0);
            MultiStateToggleButton btnchild2 = (MultiStateToggleButton)vwParentRow.getChildAt(1);
            btnChild.setVisibility(View.INVISIBLE);
            btnchild2.setVisibility(View.VISIBLE);
            notifyDataSetChanged();
        }
    });


    OrderDetails orders = (OrderDetails)getItem(position);
    viewHolder.mdtitle.setText(orders.foodname);

    return convertView;
}

private class ViewHolder{
    TextView mdtitle;
    Button confirmationbutton;
    MultiStateToggleButton switchbutton;

}

}

Screenshot of listview

预期输出:

点击披萨按钮:位置0 orderid 3

点击soupp的按钮:位置1 orderid 4

当前输出:

点击披萨按钮:位置0 orderid 3

点击soupp的按钮:位置0 orderid 3

如果我等待加载数据并立即单击按钮。我注意到如果我等待数据加载,然后按后退按钮再次返回活动,那么输出将是正确的。我做错了什么?

2 个答案:

答案 0 :(得分:0)

您的 OnClickListener 应该在 getView

中设置并获取 ViewHolder 模式。

为什么这么奇怪?

  

您的按钮单击Listener将仅在第0个位置附加   convertView将 null 。在onwords之后,convertView将被填充   听众不会像第1,第2等那样附加那个位置。

查看更新的 getView

public View getView(int position, View convertView, final ViewGroup parent) {
    ViewHolder viewHolder = null;
    if (convertView == null) {    
        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);  
        viewHolder = new ViewHolder();    
        convertView = layoutInflater.inflate(R.layout.orderlistview, null);
        confirmationbutton = (Button) convertView.findViewById(R.id.confirmationbutton);
        confirmationbutton.setTag(position);

        convertView.setTag(viewHolder);

    } else {

       viewHolder = (ViewHolder) convertView.getTag();
    }

   final int pos = position;
   confirmationbutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            OrderDetails orders = (OrderDetails)getItem(pos);
            String orderingid = orders.OrderId;
            Toast.makeText(context, "Position :"+ Integer.toString(pos)+" orderid : "+orderingid,
                    Toast.LENGTH_SHORT).show();
            notifyDataSetChanged(); 
            }
    });

    OrderDetails orders = (OrderDetails)getItem(pos);
    viewHolder.mdtitle.setText(orders.foodname);

    return convertView;
}

答案 1 :(得分:0)

尝试将setOnClickListener移出if(convertview == null)声明。