先贴效果图
这是主界面,把我存在Sqlite里的留言信息读取出来。当点击删除时,会删除那一条留言的所有信息;已读按钮只有未读信息才有显示,点击一下,会把未读信息标记为已读;点击新增按钮,跳转到添加信息界面;点击全部清除按钮,会把当前显示的所有信息全部删除。添加信息界面如下:
点击开始录音,将会录音,并把音频文件保存起来,文件路径存进Sqlite,在主界面点击播放语音,会播放相应的录音。程序整体差不多就这些。
主界面最主要的就是ListView里的button点击事件,其实用一个holder就可以解决。
这是我的Adapter:
public class ListAdapter extends BaseAdapter {
Context context;
ArrayList<MsgInfo> itemlist;
MySqliteHelper sqlitehelper;
public ListAdapter(Context context, ArrayList<MsgInfo> infolist,MySqliteHelper sqlitehelper) {
this.itemlist = infolist;
this.context = context;
this.sqlitehelper=sqlitehelper;
}
public void clearList(){
itemlist.clear();
ListAdapter.this.notifyDataSetChanged();
}
public int getCount() {
return itemlist.size();
}
public MsgInfo getItem(int position) {
return itemlist.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
final Holder holder;
LayoutInflater inflater = LayoutInflater.from(context);
MsgInfo info = itemlist.get(position);
if (convertView != null) {
holder = (Holder) convertView.getTag();
} else {
holder = new Holder();
convertView = inflater.inflate(R.layout.listitem, null);
holder.deletebtn = (Button) convertView
.findViewById(R.id.item_deletebtn);
holder.hadreadbtn = (Button) convertView
.findViewById(R.id.item_readbtn);
if(info.getStatues().equals(Const.MSG_READ)){
holder.hadreadbtn.setVisibility(View.INVISIBLE);
}
holder.msgtext = (TextView) convertView.findViewById(R.id.item_text);
if(!info.getPath().equals(Const.NoPath)){
holder.msgtext.setCompoundDrawablesWithIntrinsicBounds(context.getResources().getDrawable(R.drawable.sound), null, null, null);
}
holder.timetext = (TextView) convertView
.findViewById(R.id.item_timetext);
String msg = "";
if (info.getName().equals("")) {
msg = info.getContent();
} else {
msg = info.getName() + ":" + info.getContent();
}
holder.msgtext .setText(msg);
holder.timetext.setText(info.getCtime());
convertView.setTag(holder);
}
OnClickListener listener = new OnClickListener() {
public void onClick(View v) {
if (v == holder.deletebtn) {
SQLiteDatabase db = sqlitehelper.getWritableDatabase();
int index = position;
MsgInfo info=itemlist.get(index);
String delid=String.valueOf(info.getId());
db.delete("recode_info", "id=?", new String[]{delid});
db.close();
itemlist.remove(index);
ListAdapter.this.notifyDataSetChanged();
updateWidget();
}
if (v == holder.hadreadbtn) {
SQLiteDatabase db = sqlitehelper.getWritableDatabase();
ContentValues cv=new ContentValues();
int index = position;
MsgInfo info=itemlist.get(index);
String delid=String.valueOf(info.getId());
cv.put("statues", Const.MSG_READ);
db.update("recode_info", cv, "id=?", new String[]{delid});
db.close();
itemlist.remove(index);
ListAdapter.this.notifyDataSetChanged();
updateWidget();
}
if(v==holder.msgtext){
MsgInfo info=itemlist.get(position);
String path=info.getPath();
if(!path.equals(Const.NoPath)){
MediaPlayer mPlayer = new MediaPlayer();
try {
mPlayer.setDataSource(path);
mPlayer.prepare();
mPlayer.start();
} catch (IOException e) {
}
}
}
}
};
holder.deletebtn.setOnClickListener(listener);
holder.hadreadbtn.setOnClickListener(listener);
holder.msgtext.setOnClickListener(listener);
return convertView;
}
public void updateWidget(){
RemoteViews views = MessageNoteWidget.getRemoteViews(context);
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int[] appids = appWidgetManager.getAppWidgetIds(new ComponentName(context,
MessageNoteWidget.class));
MessageNoteWidget.updatewidget(context);
appWidgetManager.updateAppWidget(appids, views);
}
class Holder {
public Button deletebtn;
public Button hadreadbtn;
public TextView msgtext;
public TextView timetext;
}
}
对Sqlite的操作主要分这几部:
public class MySqliteHelper extends SQLiteOpenHelper {
public MySqliteHelper(Context context, String name, CursorFactory factory,int version) {
super(context, name, factory, version);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table if not exists recode_info("+ "id integer primary key,"
+ "content varchar,"+"name varchar,"+"path varchar,"+"statues varchar," +"ctime varchar)");
}
public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
}
}
写一个自己的SqliteHelper,建造一个recode_info表
MySqliteHelper sqlitehelper = new MySqliteHelper(this, "message.db", null, 1);
向Sqlite存储数据
private void insertToDb() {
SQLiteDatabase db = sqlitehelper.getWritableDatabase();
ContentValues values = new ContentValues();
String content = contentedit.getEditableText().toString();
values.put("content", content);
String name = towhoedit.getEditableText().toString();
values.put("name", name);
values.put("path", mFileName);
values.put("statues", Const.MSG_NOT_READ);
String nowtime = new DateFormat().format(Const.Format,
Calendar.getInstance(Locale.CHINA)).toString();
values.put("ctime", nowtime);
db.insert("recode_info", "id", values);
values.clear();
db.close();
}
查询Sqlite数据
private void queryData(MySqliteHelper sqlitehelper) {
msgreadList = new ArrayList<MsgInfo>();
msgnotreadList = new ArrayList<MsgInfo>();
SQLiteDatabase db = sqlitehelper.getReadableDatabase();
Cursor cursor = db.query("recode_info", null, null, null, null, null,
"id asc");
int idIndex = cursor.getColumnIndex("id");
int nameIndex = cursor.getColumnIndex("name");
int contentIndex = cursor.getColumnIndex("content");
int pathIndex = cursor.getColumnIndex("path");
int statuesIndex = cursor.getColumnIndex("statues");
int ctimeIndex = cursor.getColumnIndex("ctime");
for (cursor.moveToFirst(); !(cursor.isAfterLast()); cursor.moveToNext()) {
int id = cursor.getInt(idIndex);
String name = cursor.getString(nameIndex);
String content = cursor.getString(contentIndex);
String path = cursor.getString(pathIndex);
String statues = cursor.getString(statuesIndex);
String ctime = cursor.getString(ctimeIndex);
MsgInfo msginfo = new MsgInfo(id, name, content, path, statues,
ctime);
if (statues.equals(Const.MSG_NOT_READ)) {
msgnotreadList.add(msginfo);
}
if (statues.equals(Const.MSG_READ)) {
msgreadList.add(msginfo);
}
}
cursor.close();
db.close();
}
MsgInfo 是自己定义的一个存贮信息的类,删除和修改的代码在Listadapter里。嗯差不多就这些了。