带有复选框问题的Android自定义列表视图

时间:2013-03-15 06:33:15

标签: android android-layout

我正在创建一个类似于gmail收件箱视图的列表视图。

自定义视图的xml布局为:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/list_subject_selector"
    android:orientation="horizontal"
    android:padding="5dip" >

<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=""
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="@color/firstText"
    android:textStyle="bold"
    android:typeface="sans" />

<TextView
    android:id="@+id/details"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/title"
    android:layout_marginTop="1dip"
    android:text=""
    android:maxLines="1"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="@color/secondText" />

<CheckBox
    style="?android:attr/starStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_gravity="center_vertical|right"
    android:id="@+id/star" />

<TextView
    android:id="@+id/duration"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/details"
    android:text=""
    android:layout_marginTop="1dip"
    android:maxLines="1"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="@color/thirdText" />



<TextView
    android:id="@+id/type"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/details"
    android:gravity="right"
    android:layout_alignParentRight="true"
    android:layout_toRightOf="@id/duration"
    android:text=""
    android:layout_marginTop="1dip"
    android:maxLines="1"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="@color/thirdText" />

现在我想要的是当用户点击消息时应将其标记为已读,如果他选择了星号(复选框),则应将其标记为收藏夹。所以我的问题是,当他选择复选框时,它被标记为已读,复选框显示为已选中,当我取消选中它时,它显示为未读,我已经处理了后端的内容。

相同的代码是:

public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    if(convertView==null)
        vi = inflater.inflate(R.layout.list_row, null);

    TextView title = (TextView)vi.findViewById(R.id.title); 
    TextView details = (TextView)vi.findViewById(R.id.details); 
    TextView expiry = (TextView)vi.findViewById(R.id.duration);
    TextView type = (TextView)vi.findViewById(R.id.type);
    CheckBox fav = (CheckBox) vi.findViewById(R.id.star);

    final MessageDTO message = data.get(position);
    title.setText(message.getSubject());
    if(message.getExpiryDate() != null) {
        expiry.setText("Expires On : " + 
                new SimpleDateFormat(getContext()
                        .getString(R.string.detailsDisplayDateTimeFormat))
        .format(message.getExpiryDate()));          
    }
    details.setText(message.getMessage());
    type.setText(message.getType());

    if(message.isFavorite()) {
        fav.setChecked(true);
    } else {
        fav.setChecked(false);
    }
    if(message.isRead()) {
        vi.setBackgroundColor(activity.getResources().getColor(R.color.message_read));
        title.setTypeface(null, Typeface.NORMAL);
        details.setTypeface(null, Typeface.NORMAL);
    } else {
        vi.setBackgroundColor(Color.WHITE);
        title.setTypeface(null, Typeface.BOLD);
        details.setTypeface(null, Typeface.BOLD);
    }
    vi.setClickable(true);
    vi.setFocusable(true);
    vi.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            messageRepository.markMessageRead(message.getId());
            Intent intent = new Intent(activity, MessageDetailsActivity.class);
            intent.putExtra("com.xyz.android.msgId", message.getId());
            activity.startActivity(intent);
        }
    });

    fav.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            CheckBox cb = (CheckBox) v;
            if(message.isFavorite() && !cb.isChecked()) {
                messageRepository.markMessageFav(message.getId(), 0);
            } else if(!message.isFavorite() && cb.isChecked()) {
                messageRepository.markMessageFav(message.getId(), 1);
            }
        }
    });
    return vi;
}

2 个答案:

答案 0 :(得分:0)

而不是在convertView上处理onclickListener,而不是在“TextView title”上处理“vi”setonclickListener。

答案 1 :(得分:0)

为什么不使用首选项来检查或取消选中复选框状态,然后通过从保存的首选项值中读取值来完成其余操作