In the last blog, I wrote about displaying the data from another application on the console. In this blog, I will talk about how to display the data on the listView. If the data on listView is modified, other deletions and additions can be done in the opposite way. Before that, we add another knowledge point, Uri matcher. In order to show its use, we will make a distinction between all the buttons we got before, and query them according to the condition, whether they are all or single. This is the class in the content provider project. We can see how the Uri matcher is used. First we instantiate a matcher and then add rules to it, where com.ccf.android_sqlite.PERSON. PERSON can be named at will.
public class MyContentPprovider extends ContentProvider {
private SQLiteDatabase sqLiteDatabase;
private UriMatcher uriMatcher;
@Override
public boolean onCreate() {
Log.i("test","onCreate");
DbUtil dbUtil=new DbUtil(getContext(),"student.db",null,1);
sqLiteDatabase = dbUtil.getReadableDatabase();
//Instantiate Uri Matcher
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
//Add rules
//The first rule is to query all
uriMatcher.addURI("com.ccf.android_sqlite.PERSON","student",1);
//The second rule is to query individual
uriMatcher.addURI("com.ccf.android_sqlite.PERSON","student/#",2);//# Values of generic parameters
return false;
}
@Nullable
@Override
public Cursor query(Uri uri, String[] strings, String s, String[] strings1, String s1) {
//Start matching Uri based on the URI matcher
int code=uriMatcher.match(uri);
switch (code) {
case 1:
//Query all
Log.i("test","query All");
//Get all the data in the database
return sqLiteDatabase.query(false,"student",strings,s,strings1,null,null,s1,null);
case 2:
//Query single
//Get the value of #.
long id=ContentUris.parseId(uri);
Log.i("test","query single");
return sqLiteDatabase.rawQuery("select * from student where _id=?",new String[]{id+""});
}
return null;
}
@Nullable
@Override
public String getType(Uri uri) {
Log.i("test","getType");
return null;
}
@Nullable
@Override
public Uri insert(Uri uri, ContentValues values) {
Log.i("test","insert");
return null;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
Log.i("test","delete");
return 0;
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
sqLiteDatabase.update("student",values,selection,selectionArgs);
Log.i("test","update");
return 0;
}
}
Then change the query method of the content visitor's MainActivity to the following
public void getAllData(View view){//The method invoked here will also be invoked by the content provider
//Determine whether the value of the input box is empty
if(TextUtils.isEmpty(et_main_id.getText().toString())){
//Query all
//Must conform to content protocol
Toast.makeText(this, "dds", Toast.LENGTH_SHORT).show();
uri = Uri.parse("content://com.ccf.android_sqlite.PERSON/student");
}else{
//Query single
//Uri matcher
Toast.makeText(this, "111", Toast.LENGTH_SHORT).show();
String id=et_main_id.getText().toString();
uri = Uri.parse("content://com.ccf.android_sqlite.PERSON/student/"+id);
}
cursor=cr.query(uri,null,null,null,null);
simpleCursorAdapter.changeCursor(cursor);
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);
}
}
Then the knowledge points of the matcher will be finished. Then the data is displayed on the listView. First, we need to change the id of the listView into the system in the layout file, which is convenient to call. Then, by changing the inheritance of Activity to the inheritance of ListActivity, you can get it directly through getListView(), and instantiate the adapter.
//Instance adapter
simpleCursorAdapter = new SimpleCursorAdapter(this, R.layout.item_list,cursor,new String[]{"_id","name","age"},new int[]{R.id.tv_item_list_id,R.id.tv_item_list_name,R.id.tv_item_list_age});
listView.setAdapter(simpleCursorAdapter);
Then set a long press event for item
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long l) {
LinearLayout root= (LinearLayout) view;
String id=((TextView)root.findViewById(R.id.tv_item_list_id)).getText().toString();
String name=((TextView)root.findViewById(R.id.tv_item_list_name)).getText().toString();
String age=((TextView)root.findViewById(R.id.tv_item_list_age)).getText().toString();
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
final AlertDialog alertDialog=builder.create();
//Click on the blank and do not cancel the dialog box
alertDialog.setCancelable(false);
// View v= LayoutInflater.from(MainActivity.this).inflate(R.layout.dialog_list,null);
View v = View.inflate(MainActivity.this, R.layout.dialog_list, null);
final TextView tv_dialog_list_id = (TextView) v.findViewById(R.id.tv_dialog_list_id);
final EditText et_dialog_list_name = (EditText) v.findViewById(R.id.et_dialog_list_name);
final EditText et_dialog_list_age = (EditText) v.findViewById(R.id.et_dialog_list_age);
((Button)v.findViewById(R.id.btn_dialog_list_ok)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String id=(String)tv_dialog_list_id.getText().toString().substring(3);
String name=(String)et_dialog_list_name.getText().toString();
String age=(String)et_dialog_list_age.getText().toString();
ContentValues values=new ContentValues();
values.put("name",name);
values.put("age",age);
uri = Uri.parse("content://com.ccf.android_sqlite.PERSON/student");
cr.update(uri,values,"_id=?",new String[]{id});
//Toast.makeText(MainActivity.this, "Execute modification statements", Toast.LENGTH_SHORT).show();
//Close the current dialog box
alertDialog.dismiss();
}
});
((Button)v.findViewById(R.id.btn_dialog_list_nook)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Close the current dialog box
alertDialog.dismiss();
}
});
tv_dialog_list_id.setText("Amendment:"+id);
et_dialog_list_name.setText(name);
et_dialog_list_age.setText(age);
alertDialog.setView(v);
alertDialog.show();
return true;
}
});
Basically, all the key codes are here, hoping to help people in need, if there are any good ideas can also be put forward, and progress together! __________