Listing 917 srccomcookbookdatastorageDisplay Diariesjava

package com.cookbook.datastorage;

import java.text.DateFormat; import java.util.ArrayList; import java.util.Date;

import android.app.ListActivity;

import android.content.Context; import android.database.Cursor;

import android.os.Bundle;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup; import android.widget.BaseAdapter;

import android.widget.TextView;

import com.cookbook.data.Constants; import com.cookbook.data.MyDB;

public class DisplayDiaries extends ListActivity { MyDB dba;

DiaryAdapter myAdapter; private class MyDiary{

public MyDiary(String t, String c, String r){ title=t; content=c; recorddate=r;

public String title; public String content; public String recorddate;

^Override protected void onCreate(Bundle savedlnstanceState) { dba = new MyDB(this); dba.open();

setContentView(R.layout.diaries);

super.onCreate(savedlnstanceState); myAdapter = new DiaryAdapter(this); this.setListAdapter(myAdapter);

private class DiaryAdapter extends BaseAdapter { private Layoutlnflater mInflater; private ArrayList<MyDiary> diaries; public DiaryAdapter(Context context) {

mInflater = Layoutlnflater.from(context); diaries = new ArrayList<MyDiary>(); getdata();

public void getdata(){

Cursor c = dba.getdiaries(); startManagingCursor(c); if(c.moveToFirst()){ do{

String title =

c.getString(c.getColumnIndex(Constants.TITLE_NAME));

String content =

c.getString(c.getColumnIndex(Constants.CONTENT_NAME)); DateFormat dateFormat =

DateFormat.getDateTimeInstance(); String datedata = dateFormat.format(new Date(c.getLong(c.getColumnIndex(

Constants.DATE_NAME))).getTime()); MyDiary temp = new MyDiary(title,content,datedata); diaries.add(temp); } while(c.moveToNext());

@Override public int getCount() {return diaries.size();} public MyDiary getItem(int i) {return diaries.get(i);} public long getItemId(int i) {return i;}

public View getView(int arg0, View argl, ViewGroup arg2) { final ViewHolder holder; View v = argl;

v = mInflater.inflate(R.layout.diaryrow, null); holder = new ViewHolder();

holder.mTitle = (TextView)v.findViewById(R.id.name); holder.mDate = (TextView)v.findViewById(R.id.datetext); v.setTag(holder); } else {

holder.mdiary = getItem(argO);

holder.mTitle.setText(holder.mdiary.title);

holder.mDate.setText(holder.mdiary.recorddate);

v.setTag(holder);

return v;

public class ViewHolder { MyDiary mdiary; TextView mTitle; TextView mDate;

testl

May 11, 2010 10:41:34 PM

hello

May 11, 2010 11:53:12 PM

new content

May 16, 2010 10:21:20 AM

Watching TV

May 16,2010 1:55:10 PM

Watching TV

May 16,2010 1:55:10 PM

Figure 9.4 The ListView of diary entries.
Character Building Thought Power

Character Building Thought Power

Character-Building Thought Power by Ralph Waldo Trine. Ralph draws a distinct line between bad and good habits. In this book, every effort is made by the writer to explain what comprises good habits and why every one needs it early in life. It draws the conclusion that habits nurtured in early life concretize into impulses in future for the good or bad of the subject.

Get My Free Ebook


Post a comment