主布局:
item布局:
注册文件:
主活动代码:
Msg类代码:
openhelper代码:
observer类代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/et"
android:hint="请输入"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn"
android:onClick="click"
android:text="点击"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</LinearLayout>
item布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/msg_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="message"
android:padding="10dp" />
</LinearLayout>
注册文件:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.lhj.asus.store">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name="com.lhj.asus.store.MainActivity">
<!--<intent-filter>-->
<!--<action android:name="android.intent.action.MAIN"/>-->
<!--<category android:name="android.intent.category.LAUNCHER"/>-->
<!--</intent-filter>-->
</activity>
<activity android:name="com.lhj.asus.store.MessageObserverActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<provider
android:authorities="com.lhj.asus.store"
android:name="com.lhj.asus.store.MsgProvider"></provider>
</application>
</manifest>
主活动代码:
package com.lhj.asus.store;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import com.lhj.asus.store.db.Message;
import com.lhj.asus.store.db.Msg;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.Inflater;
/**
* Created by asus on 2016/5/13.
* 点击按钮向数据库里插入数据,并且在前台的listview中显示
* 每次打开的时候数据都可以直接查询显示
* ContentProvider,内容提供者,可以操作自身的数据库进行增删该查
*
*/
public class MessageObserverActivity extends AppCompatActivity {
private EditText et;
private ListView lv;
private List<String> list = new ArrayList<String>();
private ContentResolver resolver;
private String uri = "content://com.lhj.asus.store/message";
private MyAdapter adapter;
private Handler handler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(android.os.Message msg) {
if (msg.what == 200){
list.add(0,msg.obj.toString());
adapter.notifyDataSetChanged();
}
return true;
}
});
private MsgObserver observer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_message_observer);
et = (EditText)findViewById(R.id.et);
observer = new MsgObserver(handler,this);
lv = (ListView)findViewById(R.id.lv);
resolver = getContentResolver();
//注册观察者
resolver.registerContentObserver(Uri.parse(uri),true,observer);
//查询数据库获取内容
initDatas();
//适配器初始化
adapter = new MyAdapter();
lv.setAdapter(adapter);
}
@Override
protected void onDestroy() {
super.onDestroy();
resolver.unregisterContentObserver(observer);
}
private void initDatas() {
Cursor cursor = resolver.query(Uri.parse(uri), null, null, null, "_id desc");
if (cursor!=null){
while (cursor.moveToNext()){
//取出数据
String msg = cursor.getString(cursor.getColumnIndex("msg"));
list.add(msg);
}
//关闭cursor
cursor.close();
}
}
//点击按钮的方法
public void click(View v){
ContentValues values = new ContentValues();
values.put("msg",et.getText().toString());
resolver.insert(Uri.parse(uri),values);
}
class MyAdapter extends BaseAdapter{
@Override
public int getCount() {
return list.size();
}
@Override
public String getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
报错信息提示是 getView这里下边赋值那行有问题。。。可是我真不知道哪儿错误了
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView==null){
convertView = LayoutInflater.from(MessageObserverActivity.this).inflate(R.layout.activity_message_observer, null);
holder = new ViewHolder();
holder.message = (TextView) convertView.findViewById(R.id.msg_tv);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
//赋值
holder.message.setText(list.get(position));
return convertView;
}
}
class ViewHolder {
public TextView message;
}
}
Msg类代码:
package com.lhj.asus.store.db;
/**
* Created by asus on 2016/5/14.
*/
public class Msg {
private int id;
private String msg;
public Msg(int id, String msg) {
this.id = id;
this.msg = msg;
}
public Msg(){
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
openhelper代码:
package com.lhj.asus.store.db;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by asus on 2016/5/13.
*/
public class MyOpenHelper extends SQLiteOpenHelper {
public MyOpenHelper(Context context) {
super(context, "message.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "create table message(_id integer primary key autoincrement,msg varchar(100))";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
observer类代码:
package com.lhj.asus.store;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
/**
* Created by asus on 2016/5/14.
*/
public class MsgObserver extends ContentObserver {
private ContentResolver resolver;
private Handler handler;
public MsgObserver(Handler handler, Context context) {
super(handler);
resolver = context.getContentResolver();
this.handler = handler;
}
@Override
public void onChange(boolean selfChange, Uri uri) {
super.onChange(selfChange, uri);
Log.i("TAG","onchange方法接收到的uri:"+uri);
//从Uri中获取末尾的ID
long id = ContentUris.parseId(uri);
//单独查询一条数据
Cursor cursor = resolver.query(uri, null, "_id=" + id, null, "_id desc");
if (cursor!=null){
if (cursor.moveToFirst()){
String info = cursor.getString(cursor.getColumnIndex("msg"));
//消息的发送
Message message = Message.obtain();
message.what = 200;
message.obj = info;
handler.sendMessage(message);
cursor.close();
}
}
}
}