Import java.util.ArrayList;
Import Java.util.HashMap;
Import Java.util.Map;
Import Android.os.Bundle;
Import Android.provider.UserDictionary.Words;
Import android.app.Activity;
Import Android.content.ContentResolver;
Import android.content.ContentValues;
Import android.content.Intent;
Import Android.database.Cursor;
Import Android.view.Menu;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.EditText;
Import Android.widget.Toast;
public class Dictresolver extends Activity {
Contentresolver Contentresolver;
Button Insert;
Button search;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_dict_resolver);
Gets the Contentresolver object for the system
Contentresolver = Getcontentresolver ();
Insert = (Button) Findviewbyid (R.id.insert);
Search = (Button) Findviewbyid (R.id.search);
To bind an event listener for the Click event of the Insert button
Insert.setonclicklistener (New Onclicklistener () {
@Override
public void OnClick (View v) {
Get user input
String Word = ((EditText) Findviewbyid (R.id.word)). GetText (). toString ();
String detail = ((EditText) Findviewbyid (R.id.detail)). GetText (). toString ();
Insert a new word record
Contentvalues values = new Contentvalues ();
Values.put (mediaprovidertest. Words.Word.WORD, Word);
Values.put (mediaprovidertest. Words.Word.DETAIL, DETAIL);
Contentresolver.insert (mediaprovidertest. Words.Word.DICT_CONTENT_URI, values);
Display the prompt information
Toast.maketext (Dictresolver.this, "Add word success! ", 8000). Show ();
}
});
To bind event listeners to the click event for the Search button
Search.setonclicklistener (New Onclicklistener () {
@Override
public void OnClick (View v) {
Get user input
String key = ((EditText) Findviewbyid (R.id.key)). GetText (). toString ();
Execute Query
cursor cursor = Contentresolver.query (
Mediaprovidertest. Words.Word.DICT_CONTENT_URI,
NULL, "Word like?" Or detail like? ",
New string[]{"%" +key+ "%", "%" +key+ "%"}, NULL);
Create a bundle
Bundle data = new bundle ();
Data.putserializable ("Data", convercursortolist (cursor));
Create a intent
Intent Intent = new Intent (dictresolver.this, Resultactivity.class);
Intent.putextras (data);
Start activity
StartActivity (Intent);
}
});
}
Private arraylist<map<string, string>> convercursortolist (cursor cursor) {
arraylist<map<string, string>> result = new arraylist<map<string,string>> ();
Traversing the cursor result set
while (Cursor.movetonext ()) {
Storing data in a result set in ArrayList
map<string, string> map = new hashmap<string, string> ();
Remove values from column 2nd, column 3rd in the query record
Map.put (mediaprovidertest. Words.Word.WORD, cursor.getstring (1));
Map.put (mediaprovidertest. Words.Word.DETAIL, Cursor.getstring (2));
Result.add (map);
}
return result;
}
}
Use Contentresolver to add data, query data