You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
rjgc/ui/NotesListItem.java

165 lines
5.9 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/*
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.micode.notes.ui;
import android.content.Context;
import android.database.Cursor;
import android.text.TextUtils;
import android.text.format.DateFormat;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import net.micode.notes.R;
import net.micode.notes.data.Notes;
import net.micode.notes.data.Notes.NoteColumns;
import java.text.SimpleDateFormat;
import java.util.Date;
// ... existing code ...
/**
* 笔记列表项视图
*
* 【功能说明】
* 1. 自定义LinearLayout用于显示笔记列表中的每一项
* 2. 根据笔记类型(普通笔记/文件夹)显示不同的内容
* 3. 支持多选模式和勾选框显示
* 4. 显示提醒图标、标题、时间等信息
*/
public class NotesListItem extends LinearLayout {
private ImageView mAlert;
private TextView mTitle;
private TextView mTime;
private CheckBox mCheckBox;
private NoteItemData mItemData;
/**
* 构造函数:初始化视图组件
* @param context 上下文对象
*/
public NotesListItem(Context context) {
super(context);
inflate(context, R.layout.note_item, this);
mAlert = (ImageView) findViewById(R.id.iv_alert_icon);
mTitle = (TextView) findViewById(R.id.tv_title);
mTime = (TextView) findViewById(R.id.tv_time);
mCheckBox = (CheckBox) findViewById(android.R.id.checkbox);
}
/**
* 绑定数据到视图
* @param context 上下文对象
* @param data 笔记项数据
* @param choiceMode 是否为多选模式
* @param checked 是否被选中
*/
public void bind(Context context, NoteItemData data, boolean choiceMode, boolean checked) {
if (data == null) return;
mItemData = data;
// 处理多选模式下的勾选框显示
if (choiceMode && data.getType() == Notes.TYPE_NOTE) {
mCheckBox.setVisibility(View.VISIBLE);
mCheckBox.setChecked(checked);
} else {
mCheckBox.setVisibility(View.GONE);
}
// 根据笔记类型显示不同内容
if (data.getType() == Notes.TYPE_NOTE) {
// 普通笔记:显示提醒图标、内容摘要和时间
mAlert.setVisibility(data.hasAlert() ? View.VISIBLE : View.GONE);
if (!TextUtils.isEmpty(data.getSnippet())) {
mTitle.setText(data.getSnippet());
} else {
mTitle.setText(context.getString(R.string.notelist_string_info));
}
// 根据修改日期和提醒日期决定显示哪个时间
if (data.getModifiedDate() - data.getAlertDate() >= 0) {
// 显示修改时间
if (DateFormat.is24HourFormat(context)) {
mTime.setText(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date(data.getModifiedDate())));
} else {
mTime.setText(DateFormat.format("yyyy-MM-dd hh:mm aa", data.getModifiedDate()));
}
} else {
// 显示提醒时间
if (DateFormat.is24HourFormat(context)) {
mTime.setText(new SimpleDateFormat("MM-dd HH:mm").format(new Date(data.getAlertDate())));
} else {
mTime.setText(DateFormat.format("MM-dd hh:mm aa", data.getAlertDate()));
}
}
} else if (data.getType() == Notes.TYPE_FOLDER) {
// 文件夹:隐藏提醒图标,显示文件夹名称和笔记数量
mAlert.setVisibility(View.GONE);
mTitle.setText(data.getSnippet());
mTime.setText("(" + data.getNotesCount() + ")");
}
}
/**
* 获取绑定的笔记项数据
* @return 笔记项数据对象
*/
public NoteItemData getItemData() {
return mItemData;
}
/**
* 笔记项数据内部类(简化版)
* 注意此内部类与外部的NoteItemData类重复建议删除
*/
public static class NoteItemData {
private long mId;
private long mAlertDate;
private int mBgColorId;
private int mType;
private String mSnippet;
private long mModifiedDate;
private long mParentId;
private int mNotesCount;
public NoteItemData(Context context, Cursor cursor) {
mId = cursor.getLong(0);
mSnippet = cursor.getString(1);
mType = cursor.getInt(2);
mModifiedDate = cursor.getLong(3);
mAlertDate = cursor.getLong(4);
mBgColorId = cursor.getInt(5);
mParentId = cursor.getLong(7);
// 简单处理数量,避免引用报错的方法
mNotesCount = (mType == Notes.TYPE_FOLDER) ? 0 : 1;
}
public long getId() { return mId; }
public long getAlertDate() { return mAlertDate; }
public int getBgColorId() { return mBgColorId; }
public int getType() { return mType; }
public String getSnippet() { return mSnippet; }
public long getModifiedDate() { return mModifiedDate; }
public long getParentId() { return mParentId; }
public int getNotesCount() { return mNotesCount; }
public boolean hasAlert() { return mAlertDate > 0; }
}
}