在按钮单击事件上更改列表项的背景颜色

时间:2014-01-30 09:43:40

标签: android android-listview

在我的应用程序中,我使用自定义列表视图,在列表视图的每一行中有两个按钮和一个图像视图。我想做这样的事情:

当用户按下列表视图上的DONE按钮时,应该打开一个对话框。在对话框中,我有一些要显示的内容。

  • 关闭对话框时,相关列表视图项的背景应更改为某种颜色
  • 当我刷新列表视图时应该有颜色变化

我该怎么办?我知道如何在列表按钮上的单击事件上显示一个对话框,但是要更改背景的颜色,*我不知道该怎么做...请给我一些逻辑,代码示例想法...... 。请...

public View getView(final int position, final View convertView, final ViewGroup parent) {
    // TODO Auto-generated method stub
    View vi = convertView;
    if(convertView==null)
        vi = inflater.inflate(R.layout.row_for_appoinments,null);


        final TextView firstname = (TextView) vi.findViewById(R.id.first_name);
        final TextView lastname = (TextView) vi.findViewById(R.id.last_name);
        final TextView startTime = (TextView) vi.findViewById(R.id.start_time);
        final TextView endTime = (TextView) vi.findViewById(R.id.end_time);
        final TextView date = (TextView) vi.findViewById(R.id.empty_field);
        final TextView hidID = (TextView) vi.findViewById(R.id.row_id_pationt);
        final TextView hidAppid = (TextView) vi.findViewById(R.id.appoinment_id_row);

         img = (ImageView) vi.findViewById(R.id.image);


        HashMap<String, String> song = data.get(position);

        firstname.setText(song.get("FirstName"));

        //Log.w("viewFirstName", firstName.toString());


        lastname.setText(song.get("LastName"));
        startTime.setText(song.get("StartTime"));
        endTime.setText(song.get("EndTime"));
        date.setText(song.get("Date"));
        hidID.setText(song.get("PersonId"));
        //Log.w("viewhidID", hidID.getText().toString());
        hidAppid.setText(song.get("AppointmentId"));

         theUrl = song.get("ImageURL");




        if(theUrl==null || theUrl.equalsIgnoreCase("") || theUrl.equalsIgnoreCase("")){
            Bitmap bImage = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.propic);
            img.setImageBitmap(bImage);

        }
        else{

            if (cancelPotentialDownload(theUrl, img)) {

                 BitmapDownloaderTask2 task = new BitmapDownloaderTask2(img);
                 DownloadedDrawable2 downloadedDrawable = new DownloadedDrawable2(task);
                 img.setImageDrawable(downloadedDrawable);
                 task.execute(theUrl);
             }


        }

        /*ImageButton direction = (ImageButton) vi.findViewById(R.id.direction);
        direction.setOnClickListener(new OnClickListener() {

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

            Toast.makeText(mContext, "under construction", Toast.LENGTH_LONG).show();
            }
        });
        */



        vi.setOnClickListener(new OnClickListener() {

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

                String getPname = hidID.getText().toString();

                Intent zoom=new Intent(parent.getContext(), Profile.class);  
                zoom.putExtra("PatientID", getPname);
                parent.getContext().startActivity(zoom);



            }
        });



        Button btnDone =  (Button) vi.findViewById(R.id.btnDone);
        btnDone.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //convertView.setBackgroundColor(Color.GREEN);
            long id =  getItemId(position);
            String s = String.valueOf(id);
            //Toast.makeText(mContext, s, Toast.LENGTH_LONG).show();
            //((ViewGroup) convertView).getChildAt(position).setBackgroundColor(Color.GREEN);

            /*  //convertView.setBackgroundColor(Color.GREEN);
                if(convertView.getId() == position){
                    convertView.setBackgroundColor(Color.GREEN);
                }
                else{
                    Toast.makeText(mContext, s, Toast.LENGTH_LONG).show();
                }*/


            // HERE I WANT TO CHNAGE THE BACKGROUND COLOR
            // LIKE CHANGING COLOR OF THE ITEM WHEN WE TOUCH
            //BUT THE COLOR SHOULD REMAIN 




            }
        });
        return vi;
}

1 个答案:

答案 0 :(得分:0)

            _button.setOnClickListener(new OnClickListener() {
            @Override
        public void onClick(View v) {
                                            LayoutInflater li= LayoutInflater.from(classname.this);
                                            View dialogView =li.inflate(R.layout.layoutname, null);
                                            AlertDialog.Builder dialogwindow = new AlertDialog.Builder(classname.this);
                                            dialogwindow.setView(dialogView);               
                                            _dialogTextView=(TextView)dialogView.findViewById(R.id.dialog_textview);
                                            dialogwindow.setCancelable(false).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                                                public void onClick(DialogInterface dialog,int id) {
                                                    // get user input and set it to result
                                                    // 
                                                    dialog.cancel();            }
                                            })
                                            .setPositiveButton("ok",
                                                    new DialogInterface.OnClickListener() {
     public void onClick(DialogInterface dialog,int id) {
_listview.setBackgroundColor(color);

                            }
                                            });
                                            // create alert dialog
                                            AlertDialog alertDialog = dialogwindow.create();
                                            // show it
                                            alertDialog.show(); 
                                        }
                                    });
                                }
                            });

试试这个。它可能对你有帮助。