【问题标题】:Android: changing background color of listview [closed]Android:更改列表视图的背景颜色 [关闭]
【发布时间】:2012-12-13 11:17:33
【问题描述】:

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

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

只有当我有联系人地址时如何更改颜色?

【问题讨论】:

  • 这有帮助吗? stackoverflow.com/q/4634796/1777090 。然后你可以设置一个条件来检查是否有任何细节!
  • 如何查看列表行是否有关联地址?
  • 对不起,你能把完整的 getView() 方法发给你吗
  • @nick..这是我的完整代码pastebin.com/D0eMR5x4
  • 在 layout.setTag(contact); 之后立即添加if( !myContact.getAddress().isEmpty() ) { layout.setBackgroundColor(color) //or //layout.setBackgroundDrawable(background) }

标签: android android-listview


【解决方案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

【讨论】:

  • 我应该在哪里添加代码?
  • 在layout.setTag(contact);之后添加
  • 我做了这个 layout.setTag(contact); if(!contact.getAddress().isEmpty()) { layout.setBackgroundColor(R.drawable.hasaddress); } 应用程序正在停止..itz 不工作
  • 好的。使用视图而不是布局。你能告诉我为什么你使用这个代码 if (position % 2 == 0) view.setBackgroundResource(R.drawable.listshape); else view.setBackgroundResource(R.drawable.favoritebody);
  • 使用:if(!myContact.getAddress().isEmpty()) view.setBackgroundColor(R.drawable.hasaddress) }
【解决方案2】:

使用这个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;
}

【讨论】:

  • 最后表示返回视图之前; ?
  • 是的。返回视图之前;
  • 但是大括号出现问题..itz 不允许...
  • 你的整个 getView 应该看起来像新的答案
  • 在 myContacts 出现错误...要求创建变量或字段
【解决方案3】:

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

【讨论】:

    【解决方案4】:

    您可以通过在 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 的引用

    【讨论】:

      【解决方案5】:

      在活动/片段中声明您自己的列表视图以及 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

      【讨论】:

      • 我很困惑..你能看看我的代码并告诉我需要改变什么..
      猜你喜欢
      • 2016-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-15
      • 1970-01-01
      相关资源
      最近更新 更多