Android:更改listview的背景颜色

时间:2012-12-28 06:03:25

标签: android android-listview

在我的应用程序中,我将联系人导入列表视图,尝试更改列表视图的背景颜色,我可以更改整个列表视图的颜色但是,我希望仅对具有地址和其余的联系人保持颜色更改同样的。

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

        View view = convertView;

        if (view == null) {
                        LayoutInflater inflater = (LayoutInflater) (getContext()
                                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE));
                        view = inflater.inflate(renderer, null);
                }

                TextView text = (TextView) view.findViewById(R.id.name);
                ImageView photo = (ImageView) view.findViewById(R.id.photo);

                TextView textContNo = (TextView) view.findViewById(R.id.contactno);
                TextView textEmailId = (TextView) view.findViewById(R.id.emailId);

                Profile contact = listCont.get(position);
                text.setText(contact.getName());
                contact.getName();
                contact.getId();

                text.setTag(contact);
                text.setOnTouchListener(new OnTouchListener() {

                        @Override
                        public boolean onTouch(View v, MotionEvent event) {
                                Profile myContact= (Profile) v.getTag();

                        Intent intent = new Intent();
                        intent.putExtra("type", profileType);
                        intent.putExtra("name", myContact.getName());
                        intent.putExtra("email", myContact.getEmail());
                        intent.putExtra("contactid", myContact.getId());
                        intent.putExtra("address", myContact.getAddress());
                        intent.putExtra("city", myContact.getCity());
                        intent.putExtra("state", myContact.getState());
                        intent.putExtra("countryName", myContact.getCountryName());
                        intent.putExtra("postalCode", myContact.getPostalCode());
                        intent.putExtra("website", myContact.getWebSite());
                        intent.putExtra("mobileNumber", myContact.getMobileNo());
                        intent.putExtra("phoneNumber", myContact.getLandLineNo());
                        Log.d(TAG, "On Activity Result Method : 1");
                        activity.setResult(100, intent);

                        activity.finish();
                        Constants.loadEntries.cancel(true);
                                return false;
                        }
                });



                if(contact.getPhoto() !=null && !contact.getPhoto().equals("")){
                        photo.setImageBitmap(contact.getPhoto());
                }else{
                        photo.setImageResource(R.drawable.profile);
                }

                photo.setTag(contact);
                photo.setOnTouchListener(new OnTouchListener() {

                        @Override
                        public boolean onTouch(View v, MotionEvent event) {
                                Profile myContact= (Profile)v.getTag();

                        Intent intent = new Intent();
                        intent.putExtra("type", profileType);
                        intent.putExtra("name", myContact.getName());
                        intent.putExtra("email", myContact.getEmail());
                        intent.putExtra("contactid", myContact.getId());
                        intent.putExtra("address", myContact.getAddress());
                        intent.putExtra("website", myContact.getWebSite());
                        intent.putExtra("mobileNumber", myContact.getMobileNo());
                        intent.putExtra("phoneNumber", myContact.getLandLineNo());
                        Log.d(TAG, "On Activity Result Method : 2");
                        activity.setResult(100, intent);
                        activity.finish();
                        Constants.loadEntries.cancel(true);
                                return false;
                        }
                });

                textContNo.setText(contact.getNumber());
                textEmailId.setText(contact.getEmail());


                view.setClickable(true);
        view.setTag(contact);
        view.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                                Profile myContact= (Profile) v.getTag();

                        Intent intent = new Intent();
                        intent.putExtra("type", profileType);
                        intent.putExtra("name", myContact.getName());
                        intent.putExtra("email", myContact.getEmail());
                        intent.putExtra("contactid", myContact.getId());
                        intent.putExtra("address", myContact.getAddress());
                        intent.putExtra("website", myContact.getWebSite());
                        intent.putExtra("mobileNumber", myContact.getMobileNo());
                        intent.putExtra("phoneNumber", myContact.getLandLineNo());
                        Log.d(TAG, "On Activity Result Method : 3");
                        activity.setResult(100, intent);
                        activity.finish();
                        Constants.loadEntries.cancel(true);
                        }
                });

                LinearLayout layout = (LinearLayout)view.findViewById(R.id.profilelayout);
                layout.setTag(contact);
                layout.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                                Profile myContact= (Profile) v.getTag();
                        Intent intent = new Intent();
                        intent.putExtra("type", profileType);
                        intent.putExtra("name", myContact.getName());
                        intent.putExtra("phoneNumber", myContact.getLandLineNo());
                        intent.putExtra("email", myContact.getEmail());
                        intent.putExtra("contactid", myContact.getId());
                        intent.putExtra("address", myContact.getAddress());
                        intent.putExtra("website", myContact.getWebSite());
                        intent.putExtra("mobileNumber", myContact.getMobileNo());
                        Log.d(TAG, "On Activity Result Method : 4");
                        activity.setResult(100, intent);
                        activity.finish();
                        Constants.loadEntries.cancel(true);
                        }
                });


                if (position % 2 == 0)
                        view.setBackgroundResource(R.drawable.listshape);
                else
                        view.setBackgroundResource(R.drawable.favoritebody);
        return view;
    }

如果我有联系人地址,如何更改颜色?

5 个答案:

答案 0 :(得分:1)

替换以下代码

 if (position % 2 == 0)
                        view.setBackgroundResource(R.drawable.listshape);
                else
                        view.setBackgroundResource(R.drawable.favoritebody);

通过

if (!contact.getAddress().isEmpty() ){
    view.setBackgroundColor(R.drawable.hasaddress);
    }
else
{



     if (position % 2 == 0)
                            view.setBackgroundResource(R.drawable.listshape);
                    else
                            view.setBackgroundResource(R.drawable.favoritebody);
    }

-preeya

答案 1 :(得分:0)

如果要更改某些行的任何属性,则需要为各行定义单独的布局,或者在ListView的bindView()方法中定义,更改所需的属性。

答案 2 :(得分:0)

你可以通过在res目录和颜色文件夹中添加一个颜色文件夹来实现这个目的,为颜色定义一个xml,并在下面定义如下所示并保存它像anyname.xml

    <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" 
        android:color="@color/whiteColor"></item>
    <item android:color="@color/bluetxt"></item> 
</selector>

并在您定义的文本视图中提供此xml的引用,您将在其中显示Info

答案 3 :(得分:0)

在activities / fragment

中声明自己的listview和SimpleAdapter
private ListView myListView;
private SimpleAdapter adapter;

从布局

中找到您的列表视图
myListView = (ListView)v.findViewById(R.id.my_list_view);

初始化您的适配器并将其绑定到您的列表视图

adapter = new SimpleAdapter(myListView.getContext(),
            my_profile_arrayList,
            R.layout.listview_layout_for_the_contact_with_different_color, new String[] { "type",
                    "name", "contactid" }, new int[] {
                    R.id.profile_type,
                    R.id.profile_name,
                    R.id.profile_contactid });

    myListView .setAdapter(adapter);

只需定义您自己的 listview_layout_for_the_contact_with_different_color

答案 4 :(得分:0)

使用此getView

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

    View view = convertView;

    if (view == null) {
        LayoutInflater inflater = (LayoutInflater) (getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE));
        view = inflater.inflate(renderer, null);
    }

    TextView text = (TextView) view.findViewById(R.id.name);
    ImageView photo = (ImageView) view.findViewById(R.id.photo);

    TextView textContNo = (TextView) view.findViewById(R.id.contactno);
    TextView textEmailId = (TextView) view.findViewById(R.id.emailId);

    Profile contact = listCont.get(position);
    text.setText(contact.getName());
    contact.getName();
    contact.getId();

    text.setTag(contact);
    text.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            Profile myContact= (Profile) v.getTag();

            Intent intent = new Intent();
            intent.putExtra("type", profileType);
            intent.putExtra("name", myContact.getName());
            intent.putExtra("email", myContact.getEmail());
            intent.putExtra("contactid", myContact.getId());
            intent.putExtra("address", myContact.getAddress());
            intent.putExtra("city", myContact.getCity());
            intent.putExtra("state", myContact.getState());
            intent.putExtra("countryName", myContact.getCountryName());
            intent.putExtra("postalCode", myContact.getPostalCode());
            intent.putExtra("website", myContact.getWebSite());
            intent.putExtra("mobileNumber", myContact.getMobileNo());
            intent.putExtra("phoneNumber", myContact.getLandLineNo());
            Log.d(TAG, "On Activity Result Method : 1");
            activity.setResult(100, intent);

            activity.finish();
            Constants.loadEntries.cancel(true);
            return false;
        }
    });



    if(contact.getPhoto() !=null && !contact.getPhoto().equals("")){
        photo.setImageBitmap(contact.getPhoto());
    }else{
        photo.setImageResource(R.drawable.profile);
    }

    photo.setTag(contact);
    photo.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            Profile myContact= (Profile)v.getTag();

            Intent intent = new Intent();
            intent.putExtra("type", profileType);
            intent.putExtra("name", myContact.getName());
            intent.putExtra("email", myContact.getEmail());
            intent.putExtra("contactid", myContact.getId());
            intent.putExtra("address", myContact.getAddress());
            intent.putExtra("website", myContact.getWebSite());
            intent.putExtra("mobileNumber", myContact.getMobileNo());
            intent.putExtra("phoneNumber", myContact.getLandLineNo());
            Log.d(TAG, "On Activity Result Method : 2");
            activity.setResult(100, intent);
            activity.finish();
            Constants.loadEntries.cancel(true);
            return false;
        }
    });

    textContNo.setText(contact.getNumber());
    textEmailId.setText(contact.getEmail());


    view.setClickable(true);
    view.setTag(contact);
    view.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Profile myContact= (Profile) v.getTag();

            Intent intent = new Intent();
            intent.putExtra("type", profileType);
            intent.putExtra("name", myContact.getName());
            intent.putExtra("email", myContact.getEmail());
            intent.putExtra("contactid", myContact.getId());
            intent.putExtra("address", myContact.getAddress());
            intent.putExtra("website", myContact.getWebSite());
            intent.putExtra("mobileNumber", myContact.getMobileNo());
            intent.putExtra("phoneNumber", myContact.getLandLineNo());
            Log.d(TAG, "On Activity Result Method : 3");
            activity.setResult(100, intent);
            activity.finish();
            Constants.loadEntries.cancel(true);
        }
    });

    LinearLayout layout = (LinearLayout)view.findViewById(R.id.profilelayout);
    layout.setTag(contact);
    layout.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Profile myContact= (Profile) v.getTag();
            Intent intent = new Intent();
            intent.putExtra("type", profileType);
            intent.putExtra("name", myContact.getName());
            intent.putExtra("phoneNumber", myContact.getLandLineNo());
            intent.putExtra("email", myContact.getEmail());
            intent.putExtra("contactid", myContact.getId());
            intent.putExtra("address", myContact.getAddress());
            intent.putExtra("website", myContact.getWebSite());
            intent.putExtra("mobileNumber", myContact.getMobileNo());
            Log.d(TAG, "On Activity Result Method : 4");
            activity.setResult(100, intent);
            activity.finish();
            Constants.loadEntries.cancel(true);
        }
    });


    if (position % 2 == 0)
        view.setBackgroundResource(R.drawable.listshape);
    else
        view.setBackgroundResource(R.drawable.favoritebody);

    if (contact.getAddress() == null || contact.getAddress().length == 0){
        view.setBackgroundColor(android.R.color.background_dark);  //Set your own color here when address is null
    } else {
        view.setBackgroundColor(android.R.color.background_light);  //Set your own color here when address is present
    }

    return view;
}