今天按计划没什么大事,所以去学习制作了一下记事本,有三种功能,可以添加文字、图文、视频+文字,还有删除功能。源码下载地址:http://download.csdn.net/detail/sdksdk0/9338679。
首先看到MainActivity:
初始化界面
public void initView() { lv = (ListView) findViewById(R.id.list); textbtn = (Button) findViewById(R.id.text); imgbtn = (Button) findViewById(R.id.img); videobtn = (Button) findViewById(R.id.video); textbtn.setOnClickListener(this); imgbtn.setOnClickListener(this); videobtn.setOnClickListener(this); notesDB = new NoteDB(this); dbReader = notesDB.getReadableDatabase(); lv.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { cursor.moveToPosition(position); Intent i = new Intent(MainActivity.this, SelectAct.class); i.putExtra(NoteDB.ID, cursor.getInt(cursor.getColumnIndex(NoteDB.ID))); i.putExtra(NoteDB.CONTENT, cursor.getString(cursor .getColumnIndex(NoteDB.CONTENT))); i.putExtra(NoteDB.TIME, cursor.getString(cursor.getColumnIndex(NoteDB.TIME))); i.putExtra(NoteDB.PATH, cursor.getString(cursor.getColumnIndex(NoteDB.PATH))); i.putExtra(NoteDB.VIDEO, cursor.getString(cursor.getColumnIndex(NoteDB.VIDEO))); startActivity(i); } }); }
三个按钮的点击,用1-3来表示三个按钮的响应code。
@Override public void onClick(View v) { 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; } }
在MyAdapter.java中,
获取文本内容:
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 timetv = (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); cursor.moveToPosition(position); String content = cursor.getString(cursor.getColumnIndex("content")); String time = cursor.getString(cursor.getColumnIndex("time")); String url = cursor.getString(cursor.getColumnIndex("path")); String urlvideo = cursor.getString(cursor.getColumnIndex("video")); contenttv.setText(content); timetv.setText(time); videoiv.setImageBitmap(getVideoThumbnail(urlvideo, 200, 200, MediaStore.Images.Thumbnails.MICRO_KIND)); imgiv.setImageBitmap(getImageThumbnail(url, 200, 200)); 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; }
在Select.java中:
删除功能:
if (getIntent().getStringExtra(NoteDB.PATH).equals("null")) { s_img.setVisibility(View.GONE); } else { s_img.setVisibility(View.VISIBLE); } if (getIntent().getStringExtra(NoteDB.VIDEO).equals("null")) { s_video.setVisibility(View.GONE); } else { s_video.setVisibility(View.VISIBLE); }
删除写的内容
public void deleteDate() { dbWriter.delete(NoteDB.TABLE_NAME, "_id=" + getIntent().getIntExtra(NoteDB.ID, 0), null); }
之后还有一些返回结果之内的东西,这里就不一一说明了,布局文件可以去看源码。记得在清单文件中添加权限哦!