public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public int getCount() {
if(data==null) return 0;
return data.size();
}
}
接着在编写NoteAdapter类继承上类,初始化view, 将Note条目的各项信息展示出来,其中还自定义了一个viewHolder类,方便对xml文件中的各项组件进行绑定编辑。
public class NoteAdapter extends MyBaseAdapter {
public NoteAdapter(Context context, List data) {
super(context, data);
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder viewHolder;
if(view==null){
viewHolder=new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(context);
view = inflater.inflate(R.layout.item_main_list, viewGroup, false);
viewHolder.tv_lv_month = (TextView) view.findViewById(R.id.tv_lv_month);
viewHolder.tv_lv_day= (TextView) view.findViewById(R.id.tv_lv_day);
viewHolder.tv_lv_title = (TextView) view.findViewById(R.id.tv_lv_title);
viewHolder.tv_lv_content = (TextView) view.findViewById(R.id.tv_lv_content);
viewHolder.layout = (LinearLayout) view.findViewById(R.id.Linelayout);
view.setTag(viewHolder);
}
else{
viewHolder = (ViewHolder) view.getTag();
}
viewHolder.tv_lv_month.setText(data.get(i).getUpdate_date().split("-")[1]);
viewHolder.tv_lv_day.setText(data.get(i).getUpdate_date().split("-")[2]);
viewHolder.tv_lv_title.setText(data.get(i).getTitle());
viewHolder.tv_lv_content.setText(data.get(i).getText());
if(data.get(i).getBackground()!=null){
viewHolder.layout.setBackgroundColor(Color.parseColor(data.get(i).getBackground()));
}
return view;
}
class ViewHolder{
TextView tv_lv_month;
TextView tv_lv_day;
TextView tv_lv_title;
TextView tv_lv_content;
LinearLayout layout;
}
}
3.5 记事本的背景色设置
进入到上述提到的编辑模式2中,也就是ContentActivity中,在其底部设置更改背景色功能。这里运用到了会话框的弹出AlertDialog。设置三种颜色。点击确认后,更改数据文件中相应记事条目Note的Background属性。接着进行调用onResume();使其重新载入数据文件中的记事条目得到背景色的更改。功能截图如下:
| 背景选项 | 设置成功 |
| — | — |
部分关键代码如下:
ivSettings.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(ContentActivity.this);
builder.setIcon(R.drawable.fab_settings);
builder.setTitle(“选择一个背景色”);
// 指定下拉列表的显示数据
final String[] colors = {“护眼色”, “薰衣淡紫”, “粉粉粉”};
builder.setItems(colors, new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
Toast.makeText(ContentActivity.this, “选择的背景色为:” + colors[which], Toast.LENGTH_SHORT).show();
switch (which){
case 0:
new NoteManager(mContext).updateBackground(title,"#C7EDCC");
onResume();
break;
case 1:
new NoteManager(mContext).updateBackground(title,"#E6E6FA");
onResume();
break;
case 2:
new NoteManager(mContext).updateBac