Android Listview多个按钮点击

时间:2016-10-04 13:27:05

标签: android eclipse listview android-studio

我的问题是关于listview多个按钮点击。例如我的列表视图包含3行,每行包含3个按钮。当我点击第一行中的第一个按钮时,它会改变最后一行第一个按钮的颜色

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;
    }
}

2 个答案:

答案 0 :(得分:0)

试试这个简单的答案:

按照您设置的标记找到您的按钮视图。

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

您遇到此问题,因为您的适配器所持有的最后一个accept_btn对象是它创建的最后一个视图对象。

因此,您可以通过在按钮上设置标签来确定正确的方向。但是你必须通过标签检索该视图。 如果这有用,请告诉我。

感谢。

答案 1 :(得分:0)

我已经在gridview HERE中为同样的问题写了答案。您可以使用相同的技巧来解决此问题。 //简短的想法

您可以实现自己的自定义OnClickListener,并且可以为listview的每个项目的每个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
    }

}