2017-04-08 117 views
1

我有一個listview顯示某個餐廳的食品訂單。當用戶點擊其中一個列表視圖項目時,它應該顯示所點擊項目位置的烘烤以及食品訂單的訂單標識。出於某種原因顯示錯誤的位置。ListView位置的奇怪行爲onlicklistener

PendingOrders.java。數據從火力

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

這是如果我等待數據被加載我n並立即點擊按鈕。我注意到,如果我等待數據加載,然後按下後退按鈕並再次返回活動,那麼輸出將是正確的。我在做什麼錯誤?

回答

0

OnClickListener應設置出側和getView

得到ViewHolder模式爲什麼奇怪?

你點擊鏈接監聽器只將在第0位連接時 convertView將。在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; 
} 
+0

您是否解決了您的問題? –

+0

我試圖根據你的建議移動代碼塊,但仍然遭受同樣的問題。 – ko5win

+0

發佈了整個適配器代碼 – ko5win

0

請嘗試移動setOnClickListenerif(convertview == null) statment的。