2016-10-04 98 views
0

我的问题是关于listview多个按钮点击。例如我的列表视图包含3行,每行包含3个按钮。当我在第一排第一个按钮,双击它,在最后一行第一个按钮Android Listview多个按钮点击

please check image you will know the problem

我曾尝试使用按钮标签,查看持有人来解决它改变颜色,试图指定位置按钮,但仍没有动静

public class ViewOrderAdapter extends BaseAdapter { 

    @SuppressWarnings("unused") 
    private LayoutInflater mInflater; 
    Context mcontext; 
    ArrayList<ViewOrder> view_order_array = null; 
    SimpleDateFormat sdf; 
    String currentTime; 
    String ord_id; 
    public Button accept_btn, pickup_btn, delieverd_btn, order_view, rest_address, cust_address; 
    GPSTracker gps; 
    String latitude, longitude; 
    private static int ipos; 
    boolean[] buttonState; 
    AlertDialog alert; 
    View view; 
    OrdersFragment ord_frag; 
    ViewOrderAdapter view_order_adapter1; 

    public static final String PREFS_NAME = "MyApp_Settings"; 
    SharedPreferences settings; 
    SharedPreferences.Editor editor; 

    SharedPreferences prefs; 

    public ViewOrderAdapter (Context context, ArrayList<ViewOrder> view_order_array) { 
     mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     this.mcontext = context; 
     prefs = mcontext.getSharedPreferences(PREFS_NAME, mcontext.MODE_PRIVATE); 
     this.view_order_array = view_order_array; 
     buttonState = new boolean[view_order_array.size()]; 
     gps = new GPSTracker(mcontext); 
    } 

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

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

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

    @SuppressLint("ViewHolder") 
    @Override 
    public View getView(final int position, View convertView, ViewGroup parent) { 
     LayoutInflater inflater = (LayoutInflater) mcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = inflater.inflate(R.layout.order_single_row, parent, false); 
     view = convertView; 

     sdf = new SimpleDateFormat("HH:mm:ss"); 
     currentTime = sdf.format(new Date()); 

     final ViewOrder v_ordr = view_order_array.get(position); 
     TextView cname = (TextView)convertView.findViewById(R.id.restaurant_name_view); 
     cname.setText(v_ordr.getRestaurant()); 
     TextView caddr = (TextView)convertView.findViewById(R.id.restaurant_address_view); 
     caddr.setText(v_ordr.getRaddress()); 

     accept_btn = (Button)convertView.findViewById(R.id.order_accept_btn); 
     pickup_btn = (Button)convertView.findViewById(R.id.order_pickup_btn); 
     delieverd_btn = (Button)convertView.findViewById(R.id.order_delievery_btn); 

     accept_btn.setTag(position); 
     pickup_btn.setTag(position); 
     delieverd_btn.setTag(position); 

     order_view = (Button)convertView.findViewById(R.id.order_info); 
     rest_address = (Button)convertView.findViewById(R.id.rest_addrs); 
     cust_address = (Button)convertView.findViewById(R.id.cstom_addrs); 
     int id = Integer.parseInt(view_order_array.get(position).getId()); 


     accept_btn.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(final View v) { 

       AppConstants.btn_id = view_order_array.get(position).getId(); 
       String id = AppConstants.btn_id; 
       //accept_btn.setId(position); 

       buttonState[position] = false; 

       String token = AppPreferences.getSharedPrefValue(mcontext, AppConstants.auth); 
       ShowMessagesDialogsUtitly.showProgressDialog(mcontext); 
       RequestParams requestParams = new RequestParams(); 
       requestParams.put("k", token); 
       requestParams.put("id", id); //order id 
       requestParams.put("status", Integer.toString(1)); 
       requestParams.put("time", currentTime); 
       requestParams.put("page", "send"); 

       HttpUtils.httpPostRequest(AppConstants.BASE_URL, requestParams, new HttpResponseCallback() { 
        @Override 
        public void onCompleteHttpResponse(String whichUrl, String jsonResponse) { 
         ShowMessagesDialogsUtitly.hideProgressDialog(); 

         if (jsonResponse != null) { 
          try { 
           JSONObject jsonResponseObj = new JSONObject(jsonResponse); 
           if (jsonResponseObj.getInt("status") == 1) { 
            accept_btn.setBackgroundColor(Color.GRAY); 

            Intent n = new Intent(mcontext, Order.class); 
            mcontext.startActivity(n); 
            ((Activity)mcontext).finish(); 

           } else { 
            Toast.makeText(mcontext, "Order progress error", Toast.LENGTH_SHORT).show(); 
           } 
          } catch (Exception e) { 
           AppUtilites.printStackTrace(e); 
          } 
         } else { 
          Toast.makeText(mcontext, AppConstants.SERVER_ERROR, Toast.LENGTH_SHORT).show(); 
         } 
        } 
       }); 
      } 
     }); 

     pickup_btn.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       AppConstants.btn_id = view_order_array.get(position).getId(); 
       String id = AppConstants.btn_id; 
       String token = AppPreferences.getSharedPrefValue(mcontext, AppConstants.auth); 
       pickup_btn.setId(position); 

       ShowMessagesDialogsUtitly.showProgressDialog(mcontext); 
       RequestParams requestParams = new RequestParams(); 
       requestParams.put("k", token); 
       requestParams.put("id", id); //order id 
       requestParams.put("status", Integer.toString(2)); 
       requestParams.put("time", currentTime); 
       requestParams.put("page", "send"); 

       HttpUtils.httpPostRequest(AppConstants.BASE_URL, requestParams, new HttpResponseCallback() { 
        @Override 
        public void onCompleteHttpResponse(String whichUrl, String jsonResponse) { 
         ShowMessagesDialogsUtitly.hideProgressDialog(); 

         if (jsonResponse != null) { 
          try { 
           JSONObject jsonResponseObj = new JSONObject(jsonResponse); 

           Toast.makeText(mcontext, "Order accepted", Toast.LENGTH_SHORT).show(); 
           } else { 
            Toast.makeText(mcontext, "Order order not accepted", Toast.LENGTH_SHORT).show(); 
           } 
          } catch (Exception e) { 
           AppUtilites.printStackTrace(e); 
          } 
         } else { 
          Toast.makeText(mcontext, AppConstants.SERVER_ERROR, Toast.LENGTH_SHORT).show(); 
         } 
        } 
       }); 
      } 
     }); 
     delieverd_btn.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       AppConstants.btn_id = view_order_array.get(position).getId(); 
       String id = AppConstants.btn_id; 
       String token = AppPreferences.getSharedPrefValue(mcontext, AppConstants.auth); 
       delieverd_btn.setId(position); 

       ShowMessagesDialogsUtitly.showProgressDialog(mcontext); 
       RequestParams requestParams = new RequestParams(); 
       requestParams.put("k", token); 
       requestParams.put("id", id); //order id 
       requestParams.put("status", Integer.toString(3)); 
       requestParams.put("time", currentTime); 
       requestParams.put("page", "send"); 

       HttpUtils.httpPostRequest(AppConstants.BASE_URL, requestParams, new HttpResponseCallback() { 
        @Override 
        public void onCompleteHttpResponse(String whichUrl, String jsonResponse) { 
         ShowMessagesDialogsUtitly.hideProgressDialog(); 

         if (jsonResponse != null) { 
          try { 
           JSONObject jsonResponseObj = new JSONObject(jsonResponse); 

           if (jsonResponseObj.getInt("status") == 1) {       
           } else { 
            Toast.makeText(mcontext, "Order order not delievered", Toast.LENGTH_SHORT).show(); 
           } 
          } catch (Exception e) { 
           AppUtilites.printStackTrace(e); 
          } 
         } else { 
          Toast.makeText(mcontext, AppConstants.SERVER_ERROR, Toast.LENGTH_SHORT).show(); 
         } 
        } 
       }); 
      } 
     }); 

     order_view.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       LayoutInflater inflater = (LayoutInflater) mcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       View layout = inflater.inflate(R.layout.order_view_lay, (ViewGroup)view.findViewById(R.id.order_view_lay_id)); 
       AlertDialog.Builder builder = new AlertDialog.Builder(mcontext); 
       builder.setView(layout); 
       alert = builder.create(); 

       TextView order_id = (TextView) layout.findViewById(R.id.order_no); 
       order_id.setText(view_order_array.get(position).getOid()); 
       TextView rest_name = (TextView) layout.findViewById(R.id.rstarnt_name); 
       rest_name.setText(view_order_array.get(position).getRestaurant()); 

       TextView quantity = (TextView) layout.findViewById(R.id.quantity); 
       quantity.setText(view_order_array.get(position).getOid()); 
       TextView item = (TextView) layout.findViewById(R.id.item); 
       item.setText(view_order_array.get(position).getMenu()); 
       TextView price = (TextView) layout.findViewById(R.id.price); 
       price.setText(view_order_array.get(position).getAmount()); 

       TextView subtotal = (TextView) layout.findViewById(R.id.sub_total); 
       subtotal.setText("XXXX"); 
       TextView total = (TextView) layout.findViewById(R.id.total); 
       total.setText("XXXX"); 

       Button ok = (Button) layout.findViewById(R.id.ok_popup_order_view_btn); 
       ok.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         alert.dismiss(); 
        } 
       }); 
       alert.show(); 
      } 
     }); 
     rest_address.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       LayoutInflater inflater = (LayoutInflater) mcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       View layout = inflater.inflate(R.layout.restaurant_addr_lay, (ViewGroup)view.findViewById(R.id.rest_addr_lay_id)); 
       AlertDialog.Builder builder = new AlertDialog.Builder(mcontext); 
       builder.setView(layout); 
       alert = builder.create(); 

       TextView r_name = (TextView) layout.findViewById(R.id.rst_name_popup); 
       r_name.setText(view_order_array.get(position).getRestaurant()); 
       TextView r_add = (TextView) layout.findViewById(R.id.rst_addr_popup); 
       r_add.setText(view_order_array.get(position).getRaddress()); 
       TextView r_phone = (TextView) layout.findViewById(R.id.rst_phone_popup); 
       r_phone.setText(view_order_array.get(position).getRestaurant()); 

       Button map = (Button) layout.findViewById(R.id.map_to_restaurant_popup); 
       map.setOnClickListener(new OnClickListener() { 
        @Override 
        public void onClick(View view) { 

         if (gps.canGetLocation()) { 
          latitude = gps.getLatitude() + ""; 
          longitude = gps.getLongitude() + ""; 

          AppConstants.Latitude = latitude.toString(); 
          AppConstants.Longitude = longitude.toString(); 

          double lat = Double.valueOf(latitude); 
          double lon = Double.valueOf(longitude); 
          gps.getCompleteAddressString(lat, lon); 

          AppConstants.Address_To = view_order_array.get(position).getRaddress(); 
          String addr = AppConstants.Address_To; 
          gps.getLocationFromAddress(mcontext, addr); 
          Intent intent = new Intent(mcontext, MapLayout.class); 
          mcontext.startActivity(intent); 
         } else { 
          gps.showSettingsAlert(); 
         } 
        } 
       }); 

       Button map_dirct = (Button) layout.findViewById(R.id.directions_to_restaurant_popup); 
       map_dirct.setOnClickListener(new OnClickListener() { 
        @Override 
        public void onClick(View view) { 

         if (gps.canGetLocation()) { 
          latitude = gps.getLatitude() + ""; 
          longitude = gps.getLongitude() + ""; 

          AppConstants.Latitude = latitude.toString(); 
          AppConstants.Longitude = longitude.toString(); 

          double lat = Double.valueOf(latitude); 
          double lon = Double.valueOf(longitude); 
          gps.getCompleteAddressString(lat, lon); 

          AppConstants.Address_To = view_order_array.get(position).getRaddress(); 
          Intent intent = new Intent(mcontext, MapLayout.class); 
          mcontext.startActivity(intent); 
         } else { 
          gps.showSettingsAlert(); 
         } 
        } 
       }); 

       Button ok = (Button) layout.findViewById(R.id.ok_popup_raddr_btn); 
       ok.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         alert.dismiss(); 
        } 
       }); 
       alert.show(); 
      } 
     }); 
     cust_address.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       LayoutInflater inflater = (LayoutInflater) mcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       View layout = inflater.inflate(R.layout.customer_addr_lay, (ViewGroup)view.findViewById(R.id.cust_addr_lay_id)); 
       AlertDialog.Builder builder = new AlertDialog.Builder(mcontext); 
       builder.setView(layout); 
       alert = builder.create(); 

       TextView c_name = (TextView) layout.findViewById(R.id.cst_name_popup); 
       c_name.setText(view_order_array.get(position).getCustomer()); 
       TextView c_add = (TextView) layout.findViewById(R.id.cst_addr_popup); 
       c_add.setText(view_order_array.get(position).getCaddress()); 
       TextView c_phone = (TextView) layout.findViewById(R.id.cst_phone_popup); 
       c_phone.setText(view_order_array.get(position).getCphone()); 
       TextView order_inst = (TextView) layout.findViewById(R.id.cst_order_inst_popup); 
       order_inst.setText(view_order_array.get(position).getInstruction()); 

       Button map = (Button) layout.findViewById(R.id.map_to_customer_popup); 
       map.setOnClickListener(new OnClickListener() { 
        @Override 
        public void onClick(View view) { 

         if (gps.canGetLocation()) { 
          latitude = gps.getLatitude() + ""; 
          longitude = gps.getLongitude() + ""; 

          AppConstants.Latitude = latitude.toString(); 
          AppConstants.Longitude = longitude.toString(); 

          double lat = Double.valueOf(latitude); 
          double lon = Double.valueOf(longitude); 
          gps.getCompleteAddressString(lat, lon); 

          AppConstants.Address_To = view_order_array.get(position).getCaddress(); 
          Intent intent = new Intent(mcontext, MapLayout.class); 
          mcontext.startActivity(intent); 
         } else { 
          gps.showSettingsAlert(); 
         } 
        } 
       }); 

       Button map_direct = (Button) layout.findViewById(R.id.directions_to_customer_popup); 
       map_direct.setOnClickListener(new OnClickListener() { 
        @Override 
        public void onClick(View view) { 

         if (gps.canGetLocation()) { 
          latitude = gps.getLatitude() + ""; 
          longitude = gps.getLongitude() + ""; 

          AppConstants.Latitude = latitude.toString(); 
          AppConstants.Longitude = longitude.toString(); 

          double lat = Double.valueOf(latitude); 
          double lon = Double.valueOf(longitude); 
          gps.getCompleteAddressString(lat, lon); 

          AppConstants.Address_To = view_order_array.get(position).getCaddress(); 
          Intent intent = new Intent(mcontext, MapLayout.class); 
          mcontext.startActivity(intent); 
         } else { 
          gps.showSettingsAlert(); 
         } 
        } 
       }); 

       Button ok = (Button) layout.findViewById(R.id.ok_popup_caddr_btn); 
       ok.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         alert.dismiss(); 
        } 
       }); 
       alert.show(); 
      } 
     }); 
     return convertView; 
    } 
} 
+0

您需要使用ViewHolder模式.. 在上面的代码中发生了什么最后添加按钮的颜色更新很自然... –

+0

那是什么?你伤心我想要改变颜色在同一行。和你的例子,改变相同的行颜色的图像?你确定你想问什么? –

+0

嗨请尝试在列表视图中实现我编辑的答案与视图持有人模式。 –

回答

0

试试这个简单的答案:

找到您所设定的标记您的按钮视图。

((Button) convertView.findViewWithTag(position)).setBackgroundColor(Color.GRAY); 

因为最后accept_btn对象适配器拿着您遇到此问题是它最后创建的视图对象。

因此,通过在按钮上设置标签,您正处于正确的轨道上。但是,您必须通过标签检索该视图。 让我知道这是否有帮助。

谢谢。

+0

是的。但是,他遇到的下一个问题将是所有使用相同TAG设置的按钮都会改变颜色。从那里他可以计算出如何为每个按钮设置不同的标签名称。此外,这个适配器将来可能会有更多问题,但这个答案将帮助他弄清楚他最初的问题。 – TerNovi

0

我已经在gridview HERE中为同样的问题写过答案。你可以使用相同的技巧来解决这个问题。 //简单的想法

您可以实现自己的自定义OnClickListener,并且可以将每个列表视图的每个onclick的id/tag/position关联起来。

yourBtn.setOnclickListener(new BtnClick(position, btnid)); 

定制OnclickListener:

Class BtnClick View.OnClickListener 
{ 
    int position; 
    int btnId; 
    Public BtnClick(int position, int id) 
    { 
     this.position = position;' 
     this.btnId = btnId; 
    } 
    @Override 
    public onClick(View v) 
    { 
     // You can add logic here for each item clicked using position and id of each item you passed in constructor 
    } 

}