安卓简单记事本实例

                记事本实例

1.功能需求

a.实现能够添加文字,图片,视频

b.能够在listView中将文字,图片,视频呈现

c.能够进行删除操作

2.需要的知识点

a.数据库

b.页面跳转及传递数据

c.ListView适配器

d.拍照,拍视频

e.缩略图

 

3.开发步骤

A.创建数据库

B.设计布局

C.添加获取文字内容

D.添加获取图片

E.添加获取视频

F.设置适配器

G.ListView添加点击事件增加删除操作

H.详情页设计

4.项目代码

package com.example.lrnotes;

 

import android.content.Context;

import android.database.sqlite.SQLiteDatabase;

import android.database.sqlite.SQLiteDatabase.CursorFactory;

import android.database.sqlite.SQLiteOpenHelper;

//创建数据库

public class NotesDB extends SQLiteOpenHelper{

 

public static final String CREATE_BOOK="create table notes ("

+"id integer primary key autoincrement, "

+"content text, "

+"Photo text, "

+"Video text, "

+"time text )";

public NotesDB(Context context) {

super(context, "note", null, 1);

}

 

@Override

public void onCreate(SQLiteDatabase db) {

db.execSQL(CREATE_BOOK);

}

 

@Override

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

// TODO Auto-generated method stub

}

 

}

 

 

package com.example.lrnotes;

 

 

import java.text.SimpleDateFormat;

import java.util.Date;

 

import android.support.v7.app.ActionBarActivity;

import android.support.v7.app.ActionBar;

import android.support.v4.app.Fragment;

import android.content.Intent;

import android.database.Cursor;

import android.database.sqlite.SQLiteDatabase;

import android.os.Bundle;

import android.util.Log;

import android.view.LayoutInflater;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.ViewGroup;

import android.widget.AdapterView;

import android.widget.AdapterView.OnItemClickListener;

import android.widget.Button;

import android.widget.ListView;

import android.os.Build;

 

public class MainActivity extends ActionBarActivity implements OnClickListener{

 

  private Button text,img,video;

  private ListView lv;

  private MyAdapter adapter;

  private SQLiteDatabase db;

  private NotesDB notesdb;

  private Cursor cursor;

  private Intent i;

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.fragment_main);

        initView();

    }

   public void initView(){

   text=(Button) findViewById(R.id.text);

   img=(Button) findViewById(R.id.img);

   video=(Button) findViewById(R.id.video);

   lv=(ListView) findViewById(R.id.list);

   text.setOnClickListener(this);

   img.setOnClickListener(this);

   video.setOnClickListener(this);

   notesdb=new NotesDB(this);

   db=notesdb.getReadableDatabase();

     lv.setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView parent, View view, int position,

  long id) {

Cursor cursor=db.query("notes",null, null,null, null, null, null);

             cursor.moveToPosition(position);

Intent i=new Intent(MainActivity.this,SelectAct.class);

i.putExtra("id", cursor.getInt(cursor.getColumnIndex("id")));

i.putExtra("content", cursor.getString(cursor.getColumnIndex("content")));

i.putExtra("Video", cursor.getString(cursor.getColumnIndex("Video")));

i.putExtra("Photo", cursor.getString(cursor.getColumnIndex("Photo")));

startActivity(i);

}

});

   }

//点击主界面按钮跳转页面

public void onClick(View v) {

Intent i=new Intent(this,AddContent.class);

switch (v.getId()) {

case R.id.text:

i.putExtra("flag", "1");

startActivity(i);

break;

       case R.id.img:

        i.putExtra("flag", "2");

startActivity(i);

break;

       case R.id.video:

        i.putExtra("flag", "3");

startActivity(i);

break;

default:

break;

}

}

//进行数据查询并将数据加载到ListView中

public void selectDB(){

Cursor cursor=db.query("notes",null, null,null, null, null, null);

adapter=new MyAdapter(this,cursor);

lv.setAdapter(adapter);

}

@Override

//onResume方法是Activity第一次创建时 重新加载实例时调用

protected void onResume() {

// TODO Auto-generated method stub

super.onResume();

selectDB();

}

}

 

 

 

package com.example.lrnotes;

 

import java.io.File;

import java.text.SimpleDateFormat;

import java.util.Date;

 

import android.support.v7.app.ActionBarActivity;

import android.support.v7.app.ActionBar;

import android.support.v4.app.Fragment;

import android.content.ContentValues;

import android.content.Intent;

import android.database.sqlite.SQLiteDatabase;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.net.Uri;

import android.os.Bundle;

import android.os.Environment;

import android.view.LayoutInflater;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.ViewGroup;

import android.widget.Button;

import android.widget.EditText;

import android.widget.ImageView;

import android.widget.VideoView;

import android.os.Build;

import android.provider.MediaStore;

 

public class AddContent extends ActionBarActivity implements OnClickListener{

 

private String val;

private Button save,delete;

private ImageView c_img;

private VideoView c_video;

private EditText c_edit;

private  NotesDB notesdb;

private SQLiteDatabase db;

//创建File对象用于存储拍照后和摄像后的的图片和视频

private File phoneFile,videoFile;

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.fragment_add_content);

//获取上一个活动传递的数据

val = getIntent().getStringExtra("flag");

         save=(Button) findViewById(R.id.save);

         delete=(Button) findViewById(R.id.delete);

         c_img=(ImageView) findViewById(R.id.c_img);

         c_video=(VideoView) findViewById(R.id.c_video);

         c_edit=(EditText) findViewById(R.id.ettext);

         save.setOnClickListener(this);

         delete.setOnClickListener(this);

         c_img.setOnClickListener(this);

         c_video.setOnClickListener(this);

         notesdb=new NotesDB(this);

          db=notesdb.getWritableDatabase();

          initView();

}

public void initView(){

if(val.equals("1")){

c_img.setVisibility(View.GONE);

c_video.setVisibility(View.GONE);

}

if(val.equals("2")){

c_img.setVisibility(View.VISIBLE);

c_video.setVisibility(View.GONE);

Intent iimg = new Intent("android.media.action.IMAGE_CAPTURE");

phoneFile = new File(getExternalCacheDir(),"photo.jpg");

iimg.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(phoneFile));

startActivityForResult(iimg, 1);

}

if(val.equals("3")){

c_video.setVisibility(View.VISIBLE);

c_img.setVisibility(View.GONE);

Intent video = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);

videoFile = new File(Environment.getExternalStorageDirectory()

.getAbsoluteFile() + "/" + getTime() + ".mp4");

video.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(videoFile));

startActivityForResult(video, 2);

}

}

@Override

//点击保存或取消按钮返回上一界面

public void onClick(View v) {

// TODO Auto-generated method stub

switch (v.getId()) {

case R.id.save:

addDB();

finish();

break;

        case R.id.delete:

finish();

break;

default:

break;

}

}

//添加数据

public void addDB(){

ContentValues values=new ContentValues();

values.put("content", c_edit.getText().toString());

values.put("time",getTime());

values.put("Photo",phoneFile+"");

values.put("Video",videoFile+"" );

db.insert("notes", null, values);

}

//获取当期时间

public String getTime(){

SimpleDateFormat format=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");

Date date=new Date();

String str=format.format(date);

return str;

}

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

// TODO Auto-generated method stub

super.onActivityResult(requestCode, resultCode, data);

if(requestCode==1){

//将拍的照片显示出来

Bitmap bitmap=BitmapFactory.decodeFile(phoneFile.getAbsolutePath());

c_img.setImageBitmap(bitmap);

}

if(requestCode==2){

c_video.setVideoURI(Uri.fromFile(videoFile));

c_video.start();

}

}

}

 

 

 

 

package com.example.lrnotes;

 

import android.content.Context;

import android.database.Cursor;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.media.Image;

import android.media.ThumbnailUtils;

import android.provider.MediaStore;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.BaseAdapter;

import android.widget.ImageView;

import android.widget.LinearLayout;

import android.widget.TextView;

//设置适配器

public class MyAdapter extends BaseAdapter{

 

   private Context context;

   private Cursor cursor;

   private LinearLayout layout;

   public MyAdapter(Context context,Cursor cursor){

   this.context=context;

   this.cursor=cursor;

   }

public int getCount() {

// TODO Auto-generated method stub

return cursor.getCount();

}

 

@Override

public Object getItem(int position) {

// TODO Auto-generated method stub

return cursor.getPosition();

}

 

@Override

public long getItemId(int position) {

// TODO Auto-generated method stub

return position;

}

 

@Override

public View getView(int position, View convertView, ViewGroup parent) {

LayoutInflater inflater=LayoutInflater.from(context);

layout=(LinearLayout) inflater.inflate(R.layout.cell, null);

TextView contenttv=(TextView) layout.findViewById(R.id.list_content);

TextView timeiv=(TextView) layout.findViewById(R.id.list_time);

ImageView imgiv=(ImageView) layout.findViewById(R.id.list_img);

ImageView videoiv=(ImageView) layout.findViewById(R.id.list_video);

//遍历数据并填充到cell布局中

cursor.moveToPosition(position);

String content=cursor.getString(cursor.getColumnIndex("content"));

String time=cursor.getString(cursor.getColumnIndex("time"));

String url=cursor.getString(cursor.getColumnIndex("Photo"));

String urlvideo=cursor.getString(cursor.getColumnIndex("Video"));

contenttv.setText(content);

timeiv.setText(time);

imgiv.setImageBitmap(getImageThumbnail(url, 200, 200));

videoiv.setImageBitmap(getVideoThumbnail(urlvideo, 200, 200,

MediaStore.Images.Thumbnails.MICRO_KIND));

return layout;

}

//获取缩列图

public Bitmap getImageThumbnail(String uri,int width,int height){

Bitmap bitmap = null;

BitmapFactory.Options options = new BitmapFactory.Options();

options.inJustDecodeBounds = true;

bitmap = BitmapFactory.decodeFile(uri, options);

options.inJustDecodeBounds = false;

int beWidth = options.outWidth / width;

int beHeight = options.outHeight / height;

int be = 1;

if (beWidth < beHeight) {

be = beWidth;

} else {

be = beHeight;

}

if (be <= 0) {

be = 1;

}

options.inSampleSize = be;

bitmap = BitmapFactory.decodeFile(uri, options);

bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height,

ThumbnailUtils.OPTIONS_RECYCLE_INPUT);

return bitmap;

}

private Bitmap getVideoThumbnail(String uri, int width, int height, int kind) {

Bitmap bitmap = null;

bitmap = ThumbnailUtils.createVideoThumbnail(uri, kind);

bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height,

ThumbnailUtils.OPTIONS_RECYCLE_INPUT);

 

return bitmap;

}

 

}

 

 

 

 

 

package com.example.lrnotes;

 

import android.support.v7.app.ActionBarActivity;

import android.support.v7.app.ActionBar;

import android.support.v4.app.Fragment;

import android.database.sqlite.SQLiteDatabase;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.net.Uri;

import android.os.Bundle;

import android.view.LayoutInflater;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.ViewGroup;

import android.widget.Button;

import android.widget.ImageView;

import android.widget.TextView;

import android.widget.VideoView;

import android.os.Build;

 

public class SelectAct extends ActionBarActivity implements OnClickListener{

 

private Button s_delete,s_back;

private ImageView s_img;

private TextView s_tv;

private VideoView s_video;

private NotesDB notesdb;

private SQLiteDatabase db;

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.fragment_select);

        s_delete=(Button) findViewById(R.id.s_delete);

        s_back=(Button) findViewById(R.id.s_back);

        s_img=(ImageView) findViewById(R.id.s_img);

        s_tv=(TextView) findViewById(R.id.s_tv);

        s_video=(VideoView) findViewById(R.id.s_video);

        notesdb=new NotesDB(this);

        db=notesdb.getWritableDatabase();

        s_back.setOnClickListener(this);

        s_delete.setOnClickListener(this);

        if(getIntent().getStringExtra("Photo").equals("null")){

         s_img.setVisibility(View.GONE);

        }else{

         s_img.setVisibility(View.VISIBLE);

        }

        if(getIntent().getStringExtra("Video").equals("null")){

         s_video.setVisibility(View.GONE);

        }else{

         s_video.setVisibility(View.VISIBLE);

        }

        s_tv.setText(getIntent().getStringExtra("content"));

        //将图片,视频显示在界面上

        Bitmap bitmap=BitmapFactory.decodeFile(getIntent().getStringExtra("Photo"));

        s_img.setImageBitmap(bitmap);

        s_video.setVideoURI(Uri.parse(getIntent().getStringExtra("Video")));

        s_video.start();

}

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

switch (v.getId()) {

case R.id.s_back:

finish();

break;

        case R.id.s_delete:

         deleteDate();

         finish();

break;

default:

break;

}

}

public void deleteDate(){

db.delete("notes", "id="+getIntent().getIntExtra("id",0), null);

}

}


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    tools:context="com.example.lrnotes.MainActivity$PlaceholderFragment" >

    <LinearLayout 

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:orientation="horizontal"

        >

        <Button 

            android:id="@+id/text"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:text="文字"

            />

         <Button 

            android:id="@+id/img"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:text="图片"

            />

         <Button 

            android:id="@+id/video"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:text="视频"

            />

    LinearLayout>

    <ListView 

       android:id="@+id/list"

       android:layout_width="match_parent"

       android:layout_height="wrap_content"

        

        >ListView>

 

LinearLayout>

 

 

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    tools:context="com.example.lrnotes.AddContent$PlaceholderFragment" >

<ImageView 

    android:id="@+id/c_img"

    android:layout_width="300dp"

    android:layout_height="500dp"

    android:visibility="gone"

    />

<VideoView 

    android:id="@+id/c_video"

    android:layout_width="300dp"

    android:layout_height="500dp"

    android:visibility="gone"

    />

    <EditText 

        android:id="@+id/ettext"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_weight="1"

        android:gravity="top"

        android:background="@null"

        android:hint="欢迎使用记事本"

        />

<LinearLayout 

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:orientation="horizontal"

    >

    <Button 

        android:id="@+id/save"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_weight="1"

        android:text="保存"

        />

      <Button 

        android:id="@+id/delete"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_weight="1"

        android:text="取消"

        />

LinearLayout>

LinearLayout>

 

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    tools:context="com.example.lrnotes.SelectAct$PlaceholderFragment" >

 

    

<ImageView 

    android:id="@+id/s_img"

    android:layout_width="300dp"

    android:layout_height="500dp"

    android:visibility="gone"

    />

<VideoView 

    android:id="@+id/s_video"

    android:layout_width="300dp"

    android:layout_height="500dp"

    android:visibility="gone"

    />

<TextView 

    android:id="@+id/s_tv"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:layout_weight="1"

    />

<LinearLayout 

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

  android:orientation="horizontal"

    >

    <Button 

        android:id="@+id/s_delete"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_weight="1"

        android:text="删除"

        />

      <Button 

        android:id="@+id/s_back"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_weight="1"

        android:text="返回"

        />

    

    

LinearLayout>

LinearLayout>

 

 

xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical" >

    <LinearLayout 

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:orientation="horizontal"

        >

     <ImageView

        android:id="@+id/list_img"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        />

         <ImageView

        android:id="@+id/list_video"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        />

         <LinearLayout 

             android:layout_width="match_parent"

             android:layout_height="100dp"

             android:gravity="center_vertical"

             android:orientation="vertical"

             >

             <TextView 

                 android:id="@+id/list_content"

                 android:layout_width="wrap_content"

                 android:layout_height="wrap_content"

                 android:text="tv"

                 />

              <TextView 

                 android:id="@+id/list_time"

                 android:layout_width="wrap_content"

                 android:layout_height="wrap_content"

                 android:text="tv"

                 />

         LinearLayout>

    LinearLayout>

 

LinearLayout>

 

你可能感兴趣的:(安卓开发实例)