Use Contentresolver to add data, query data

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.