1

私のアプリでは、連絡先をリストビューにインポートし、リストビューの背景色を変更しようとすると、リストビュー全体の色を変更できますが、アドレスと残りの部分が同じである連絡先のみの色を変更したいと思います。

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

連絡先の住所がある場合にのみ色を変更するにはどうすればよいですか?

4

5 に答える 5

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

-プリーヤ

于 2012-12-28T06:12:04.123 に答える
0

アクティビティ/フラグメントで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を定義するだけです

于 2012-12-28T06:21:52.580 に答える
0

特定の行だけの属性を変更する場合は、さまざまな行に対して個別のレイアウトを定義するか、ListView の bindView() メソッドで必要な属性を変更する必要があります。

于 2012-12-28T06:07:13.313 に答える
0

これを実現するには、res dir に color フォルダーを追加し、color フォルダーで色の 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の参照を提供します

于 2012-12-28T06:12:02.330 に答える
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;
}
于 2012-12-28T06:47:14.150 に答える