动态设置列表视图项的背景颜色,并为列表视图中的每个项加载不同的布局行

时间:2014-01-31 07:27:57

标签: android image android-listview bitmap

我有一个自定义列表视图,它是两个按钮,一个图像视图和三个文本视图。两个按钮是“完成”和“删除”

现在我只想在按完成按钮时更改一个项目的背景颜色。

例如:如果我按下第一个原始的完成按钮,原始背景应该改变。      如果我在第3行按完成按钮,那么行背景也应该改变。

我该怎么做?下面是我的getview方法

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) {


            // here i want to change the background of the row

            }
        });
        return vi;
}

我的下一个问题是,如何为不同的行加载不同的布局。请帮我提供代码示例,想法......

谢谢

1 个答案:

答案 0 :(得分:0)

我要做的是跟踪按钮被按下的索引的适配器(使用布尔值列表等)。在onClick的{​​{1}}上,您必须切换该索引的布尔值(您在“btnDone”变量中有)并在其上调用position适配器,这将导致重新绘制列表。当您使用notifyDataSetChanged()方法时,请检查列表中的当前位置是否为真,然后设置背景颜色。

对于不同的布局,您只需在getView()中填充不同的布局即可。你将无法重用convertView ......


点击已完成按钮跟踪视图的示例

在您的适配器中,保留getView()变量并添加以下方法:

SparseBooleanArray