2013-03-15 119 views
0

我正在創建一個類似於Gmail收件箱視圖的列表視圖。帶有複選框問題的Android自定義列表視圖

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

回答

0

而不是在你的convertView即 「六」 定onclickListner的 「TextView的標題是」 處理onclickListner。

+0

感謝做到根據休息,但我已經試過了,還是不行.. – 2013-03-15 07:17:22

0

你爲什麼不使用偏好有複選框的選中狀態還是未選中,並從保存的喜好讀值回值

+0

沒有使用偏好可以解決我說的問題,請問..? – 2013-03-15 07:09:06

相關問題