如果大家想看ContentPRovider的实现流程,请关注博主查看博主博客,感谢大家的支持,
ContentResolver的实现没有ContentProvider复杂,不需要配置清单文件(除访问系统的权限要添加外,自己模拟底层的就不用了)不多说了 直接上代码
MainActivity的布局文件
<EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="ID" android:id="@+id/et_main_id"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="获取数据" android:id="@+id/bt_mainsss_button" /> <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/lv_list" ></ListView>由于长按修改和Listview自己定义的布局。所以也加上来
先上长按修改的布局
<TextView android:id="@+id/tv_dialog_list_id" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="30sp" /> <EditText android:id="@+id/et_dialog_list_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="30sp" /> <EditText android:id="@+id/et_dialog_list_age" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="30sp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/btn_dialog_list_ok" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="确定修改" /> <Button android:id="@+id/btn_dialog_list_nook" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="取消修改" /> </LinearLayout>然后是ListView的布局文件
<TextView android:id="@+id/tv_list_id" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" /> <TextView android:id="@+id/tv_list_name" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" /> <TextView android:id="@+id/tv_list_age" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" />MainActivity的主要代码
public class MainActivity extends AppCompatActivity { private EditText et_main_id; private Button bt_mainsss_button; private Uri uri; private ContentResolver cr; private ListView lv_list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_main_id = (EditText) findViewById(R.id.et_main_id); bt_mainsss_button = (Button) findViewById(R.id.bt_mainsss_button); lv_list = (ListView) findViewById(R.id.lv_list); //获得内容访问者 cr = getContentResolver(); //点击事件 bt_mainsss_button.setOnClickListener(new View.OnClickListener() { private SimpleCursorAdapter simpleCursorAdapter; @Override public void onClick(View view) { if (TextUtils.isEmpty(et_main_id.getText().toString())) { //查询所有 uri = Uri.parse("content://com.example.contentprovider.PERSON/student"); } else { //查询单个 String id = et_main_id.getText().toString(); uri = Uri.parse("content://com.example.contentprovider.PERSON/student/" + id); } Cursor cursor = cr.query(uri, null, null, null, null); //SimleCursorAdapter while (cursor.moveToNext()) { int id = cursor.getInt(cursor.getColumnIndex("_id")); String name = cursor.getString(cursor.getColumnIndex("name")); int age = cursor.getInt(cursor.getColumnIndex("age")); Log.i("test", id + " " + name + " " + age);// simpleCursorAdapter.changeCursor(cursor); } simpleCursorAdapter = new SimpleCursorAdapter(MainActivity.this, R.layout.item_list, cursor, new String[]{"_id", "name", "age"}, new int[]{R.id.tv_list_id, R.id.tv_list_name, R.id.tv_list_age}); lv_list.setAdapter(simpleCursorAdapter); } }); lv_list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { private EditText et_dialog_list_name; private EditText et_dialog_list_age; private TextView tv_dialog_list_id; @Override public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) { LinearLayout root = (LinearLayout) view; String id = ((TextView) root.findViewById(R.id.tv_list_id)).getText().toString(); String name = ((TextView) root.findViewById(R.id.tv_list_name)).getText().toString(); String age = ((TextView) root.findViewById(R.id.tv_list_age)).getText().toString(); final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); final AlertDialog alertDialog = builder.create(); //点击空白处,不取消对话框 alertDialog.setCancelable(false); View v = View.inflate(MainActivity.this, R.layout.dialog, null); tv_dialog_list_id = (TextView) v.findViewById(R.id.tv_dialog_list_id); et_dialog_list_name = (EditText) v.findViewById(R.id.et_dialog_list_name); et_dialog_list_age = (EditText) v.findViewById(R.id.et_dialog_list_age); tv_dialog_list_id.setText(id); et_dialog_list_name.setText(name); et_dialog_list_age.setText(age); ((Button) v.findViewById(R.id.btn_dialog_list_ok)).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String _id=tv_dialog_list_id.getText().toString(); String _name=et_dialog_list_name.getText().toString(); String _age=et_dialog_list_age.getText().toString(); Toast.makeText(MainActivity.this, "_id"+_id+"_name"+_name+"_age"+_age, Toast.LENGTH_SHORT).show();//如果要实现修改后刷新,只要在关闭前在查一次就行 ContentValues values=new ContentValues(); values.put("name",""+_name); values.put("age",""+_age); cr.update(uri,values,"_id=?",new String[]{_id}); alertDialog.dismiss(); } }); ((Button) v.findViewById(R.id.btn_dialog_list_nook)).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //关闭当前对话框 alertDialog.dismiss(); } }); alertDialog.setView(v); alertDialog.show(); return true; } }); }}
新闻热点
疑难解答