有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何访问侦听器中的另一个视图?

我有一个自定义列表视图,其中有一些按钮。一次点击按钮,我试图改变他们的颜色,但有些我怎么也做不到

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();
                        v.setBackgroundColor(Color.parseColor("#1C6614"));
                        v.getParent().findViewById(R.id.dirty_view_id).setBackgroundColor(Color.parseColor("#8A8787"));
                        v.getParent().findViewById(R.id.na_view_id).setBackgroundColor(Color.parseColor("#8A8787"));
                        v.getParent().findViewById(R.id.dc_view_id).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;
                    }
                });

编辑 获取视图方法

@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);

                        }
                    });

我只是能够更改正在单击的当前按钮的颜色。如果我试图访问视图,它会给我错误

未为ViewParent定义findViewById。所以请告诉我如何解决这个问题


共 (0) 个答案