Android留言

先贴效果图

这是主界面,把我存在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里。嗯差不多就这些了。

留言源码


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 Android 留言板功能的代码示例: 1. 编写布局文件 layout_message.xml: ``` <?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="wrap_content" android:orientation="vertical" android:padding="16dp"> <TextView android:id="@+id/textViewName" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Name" /> <EditText android:id="@+id/editTextName" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Enter your name" /> <TextView android:id="@+id/textViewMessage" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Message" /> <EditText android:id="@+id/editTextMessage" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Enter your message" /> <Button android:id="@+id/buttonSubmit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Submit" /> </LinearLayout> ``` 2. 在 MainActivity.java 中添加以下代码: ```java public class MainActivity extends AppCompatActivity { private EditText editTextName, editTextMessage; private Button buttonSubmit; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editTextName = findViewById(R.id.editTextName); editTextMessage = findViewById(R.id.editTextMessage); buttonSubmit = findViewById(R.id.buttonSubmit); buttonSubmit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String name = editTextName.getText().toString().trim(); String message = editTextMessage.getText().toString().trim(); if (name.isEmpty()) { editTextName.setError("Please enter your name"); editTextName.requestFocus(); return; } if (message.isEmpty()) { editTextMessage.setError("Please enter your message"); editTextMessage.requestFocus(); return; } // Save name and message to database or send to server // ... Toast.makeText(MainActivity.this, "Message submitted", Toast.LENGTH_SHORT).show(); } }); } } ``` 3. 在 AndroidManifest.xml 中添加以下权限: ```xml <uses-permission android:name="android.permission.INTERNET" /> ``` 这个简单的示例演示了如何在 Android 应用程序中创建一个基本的留言板功能。当用户输入他们的名字和留言后,点击提交按钮,应用程序会将这些信息保存到数据库或发送到服务器。注意:这个示例并没有涉及到数据存储或网络通信的实现,这些功能需要自己去实现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值