android中自定义列表视图中的错误按钮侦听器检测?

时间:2015-12-23 11:27:15

标签: java android listview android-arrayadapter

我有一个自定义列表视图,我有一些按钮。点击每个按钮,我改变它的颜色,例如。第一个按钮的灰色到红色&第二个按钮为灰色至绿色,第三个按钮为灰色至蓝色等。

我已经使用了视图保持器模式。当单击按钮时,我没有更改相应按钮的颜色。点击每个按钮只有第一行和第一行。最后一行按钮颜色已更改。

这是我的代码:

public class LocationInspectionAdapter extends
            ArrayAdapter<LocationInspectionBean> {

        private final Activity context;
        private final int layoutResourceId;
        private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
         ViewHolder holder;
        Uri uri;
        String fname;
        File image, imagesFolder;

        Bitmap bp = null;

        // static Context c;

        View v;

        // private Uri fileUri;
        Integer count;
        private ArrayList<LocationInspectionBean> values = new ArrayList<LocationInspectionBean>();

        public LocationInspectionAdapter(Activity context,
                int layoutResourceId, ArrayList<LocationInspectionBean> values) {
            super(context, layoutResourceId, values);
            this.context = context;
            this.values = values;
            this.layoutResourceId = layoutResourceId;

            notifyDataSetChanged();
            Log.i("Home", "enter");

        }

        public class ViewHolder {

            TextView componentName, count_text;
            ImageView camra, red_circle;
            Button clean, dirty, na, dc, notes;

        }

GetView方法:

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

            select_postion=position;
            Log.i("error","select--"+select_postion);
            note_pos = position;
             View rowView = convertView;
             if (convertView == null) 
             {
                    LayoutInflater inflater = context.getLayoutInflater();
                    rowView = inflater.inflate(layoutResourceId, null, true);
                    holder = new ViewHolder();

                    holder.componentName = (TextView) rowView
                            .findViewById(R.id.location_list_row);
                    holder.clean = (Button) rowView.findViewById(R.id.btn1);
                    holder.dirty = (Button) rowView.findViewById(R.id.btn2);
                    holder.dc = (Button) rowView.findViewById(R.id.btn3);
                    holder.na = (Button) rowView.findViewById(R.id.btn4);
                    holder.camra = (ImageView) rowView.findViewById(R.id.btn5);
                    holder.camra.setTag(position);
                    holder.notes = (Button) rowView.findViewById(R.id.btn6);
                    holder.count_text = (TextView) rowView
                            .findViewById(R.id.circle_count);
                    holder.red_circle = (ImageView) rowView.findViewById(R.id.img_red);
                    // String img = location_obj.getImages();
                    // Log.i("IMG", img);
                    holder.clean.setTag(position);
                    holder.camra.setTag(position);
                    holder.na.setTag(position);
                    holder.dc.setTag(position);
                    holder.notes.setTag(position);
                    holder.dirty.setTag(position);


                    rowView.setTag(holder);
             
             }
             else 
             {
                    holder=(ViewHolder)rowView.getTag();
             }
             
                    LocationInspectionBean location_obj = values.get(position);
                    if (location_inspection_array.get(position).getImages() != null) {

                        imgpath = location_inspection_array.get(position).getImages();

                        imgpath1 = imgpath.split(",");
                        count = imgpath1.length;
                        holder.red_circle.setVisibility(View.VISIBLE);
                        holder.count_text.setVisibility(View.VISIBLE);
                        holder.count_text.setText(String.valueOf(count));

                    }

                    final String a = String.valueOf(position);

                    holder.componentName.setText(location_obj.getComp_name());

                    if (location_obj.getInspectionstatus().equalsIgnoreCase("1")) {
                        holder.clean.setBackgroundColor(Color.parseColor("#1C6614"));
                    }
                    if (location_obj.getInspectionstatus().equalsIgnoreCase("3")) {
                        holder.dirty.setBackgroundColor(Color.parseColor("#FC4E3B"));
                    }
                    if (location_obj.getInspectionstatus().equalsIgnoreCase("4")) {
                        holder.na.setBackgroundColor(Color.parseColor("#0D6CC3"));
                    }
                    if (location_obj.getInspectionstatus().equalsIgnoreCase("2")) {
                        holder.dc.setBackgroundColor(Color.parseColor("#E87403"));
                    }
                    if (location_obj.getNotes().isEmpty()) {
                        holder.dirty.setBackgroundColor(Color.parseColor("#8A8787"));

                    }
                    if (location_obj.getNotes().isEmpty()) {
                        holder.dc.setBackgroundColor(Color.parseColor("#8A8787"));

                    }


                    holder.clean.setOnTouchListener(new OnTouchListener() {

                        @Override
                        public boolean onTouch(View v, MotionEvent event) {
                            // TODO Auto-generated method stub
                            index = (Integer)v.getTag();
                            //  Log.i("SYNC", "camera"+String.valueOf(index));
                                Toast.makeText(getApplicationContext(), String.valueOf(index), Toast.LENGTH_SHORT).show();
                            holder.clean.setBackgroundColor(Color.parseColor("#1C6614"));
                            holder.dirty.setBackgroundColor(Color.parseColor("#8A8787"));
                            holder.na.setBackgroundColor(Color.parseColor("#8A8787"));
                            holder.dc.setBackgroundColor(Color.parseColor("#8A8787"));
                            Status = "1";

                            index = (Integer)v.getTag();
                            Log.i("SYNC", String.valueOf(index));

                            String timeStamp = new SimpleDateFormat(
                                    "MM/dd/yyyy hh:mm:ss a").format(new Date());

                            db.updateInspectionDetails(inspection_id, user_id,
                                    location_inspection_array.get(position)
                                            .getComponentid(), subclient_id, client_id,
                                    Status, images_path_string, timeStamp);

                            return false;
                        }
                    });

                    holder.dirty.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            // TODO Auto-generated method stub
                            index = (Integer)v.getTag();
                            //  Log.i("SYNC", "camera"+String.valueOf(index));
                                Toast.makeText(getApplicationContext(), String.valueOf(index), Toast.LENGTH_SHORT).show();
                            holder.dirty.setBackgroundColor(Color.parseColor("#FC4E3B"));
                            holder.clean.setBackgroundColor(Color.parseColor("#8A8787"));
                            holder.na.setBackgroundColor(Color.parseColor("#8A8787"));
                            holder.dc.setBackgroundColor(Color.parseColor("#8A8787"));
                            Status = "3";

                            component_id_note = location_inspection_array.get(position)
                                    .getComponentid();

                            // db.updateInspectionDetails(inspection_id, user_id,
                            // location_inspection_array.get(position)
                            // .getComponentid(), subclient_id, client_id,
                            // Status, images_path_string, images_time_string);

                            intent = new Intent(getContext(), Notes.class);

                            intent.putExtra("client_id", client_id);
                            intent.putExtra("component_id_note", a);
                            intent.putExtra("inspection_id", inspection_id);
                            intent.putExtra("subClient_id", subclient_id);
                            intent.putExtra("sub_client_name", sub_client_name);
                            context.startActivityForResult(intent, 1);

                            context.overridePendingTransition(R.anim.slide_in,
                                    R.anim.slide_out);

                        }
                    });



                    holder.na.setOnTouchListener(new OnTouchListener() {

                        @Override
                        public boolean onTouch(View v, MotionEvent event) {
                            // TODO Auto-generated method stub
                            index = (Integer)v.getTag();
                            //  Log.i("SYNC", "camera"+String.valueOf(index));
                                Toast.makeText(getApplicationContext(), String.valueOf(index), Toast.LENGTH_SHORT).show();
                            holder.na.setBackgroundColor(Color.parseColor("#0D6CC3"));
                            holder.dirty.setBackgroundColor(Color.parseColor("#8A8787"));
                            holder.clean.setBackgroundColor(Color.parseColor("#8A8787"));
                            holder.dc.setBackgroundColor(Color.parseColor("#8A8787"));
                            Status = "4";

                            String timeStamp = new SimpleDateFormat(
                                    "MM/dd/yyyy hh:mm:ss a").format(new Date());

                            db.updateInspectionDetails(inspection_id, user_id,
                                    location_inspection_array.get(position)
                                            .getComponentid(), subclient_id, client_id,
                                    Status, images_path_string, timeStamp);

                            return false;
                        }
                    });

                    holder.dc.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            // TODO Auto-generated method stub
                            index = (Integer)v.getTag();
                            //  Log.i("SYNC", "camera"+String.valueOf(index));
                                Toast.makeText(getApplicationContext(), String.valueOf(index), Toast.LENGTH_SHORT).show();
                            holder.dc.setBackgroundColor(Color.parseColor("#E87403"));
                            holder.dirty.setBackgroundColor(Color.parseColor("#8A8787"));
                            holder.na.setBackgroundColor(Color.parseColor("#8A8787"));
                            holder.clean.setBackgroundColor(Color.parseColor("#8A8787"));
                            Status = "2";
                            component_id_note = location_inspection_array.get(position)
                                    .getComponentid();

                            intent = new Intent(getContext(), Notes.class);
                            intent.putExtra("client_id", client_id);
                            intent.putExtra("inspection_id", inspection_id);
                            intent.putExtra("component_id_note", a);
                            intent.putExtra("subClient_id", subclient_id);
                            intent.putExtra("sub_client_name", sub_client_name);
                            context.startActivityForResult(intent, 1);
                            context.overridePendingTransition(R.anim.slide_in,
                                    R.anim.slide_out);

                        }
                    });



                    holder.camra.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            // TODO Auto-generated method stub

                            index = (Integer)v.getTag();
                        //  Log.i("SYNC", "camera"+String.valueOf(index));
                            Toast.makeText(getApplicationContext(), String.valueOf(index), Toast.LENGTH_SHORT).show();


                            Camera(location_inspection_array.get(index).getClientid(),
                                    location_inspection_array.get(index)
                                            .getSubclientid(),
                                    location_inspection_array.get(index)
                                            .getComponentid(),
                                    location_inspection_array.get(index).getComp_name());

                        }

                    });

                    holder.notes.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            // TODO Auto-generated method stub
                            component_id_note = location_inspection_array.get(position)
                                    .getComponentid();

                            intent = new Intent(getContext(), Notes.class);

                            intent.putExtra("client_id", client_id);
                            intent.putExtra("component_id_note", a);
                            intent.putExtra("inspection_id", inspection_id);
                            intent.putExtra("subClient_id", subclient_id);
                            intent.putExtra("sub_client_name", sub_client_name);
                            context.startActivity(intent);
                            context.overridePendingTransition(R.anim.slide_in,
                                    R.anim.slide_out);

                        }
                    });

这不正确,因为它总是首先更新颜色&amp;最后的观点。

我还打印了每个按钮的索引,我得到了最后一项的正确索引,我得到0指数。请告诉我如何才能改进这一点。

3 个答案:

答案 0 :(得分:1)

在OnClick(View v)中,您将视图作为参数,更改其背景颜色,如

v.setBackgroundColor(Color.RED);

答案 1 :(得分:1)

你可以尝试这样的事情:(只有当他们拥有相同的父母时)

d

为其他人做同样的事情。

答案 2 :(得分:1)

做这样的事情:

if (location_obj.getInspectionstatus().equalsIgnoreCase("1")) {
 holder.clean.setBackgroundColor(Color.parseColor("#1C6614"));
} else{
 holder.clean.setBackgroundColor(Color.parseColor("Color_you_want_to_remian_with"));
}

if (location_obj.getInspectionstatus().equalsIgnoreCase("3")) {
 holder.dirty.setBackgroundColor(Color.parseColor("#FC4E3B"));
} else{
 holder.dirty.setBackgroundColor(Color.parseColor("Color_you_want_to_remian_with"));
}

getView()中为剩余的if条件执行此操作,然后检查这些更改是否有效