Word:
package com.ssdut.dictprovider;import android.net.Uri;import android.provider.BaseColumns;public final class Words { public static final String AUTHORITY="com.ssdut.provider.dictprovider";//定义contentprovider的Authority //数据列的列名 public static final class Word implements BaseColumns{ public final static String _ID="_id"; public final static String WORD="word"; public final static String DETAIL="detail"; //提供服务的Uri public final static Uri DICT_CONTENT_URI=Uri.parse("content://"+AUTHORITY+"/words"); public final static Uri WORD_CONTENT_URI=Uri.parse("content://"+AUTHORITY+"/word"); }}
Result:
package com.ssdut.dictprovider;import java.util.List;import java.util.Map;import android.app.Activity;import android.app.AlertDialog;import android.app.AlertDialog.Builder;import android.content.ContentResolver;import android.content.DialogInterface;import android.content.DialogInterface.OnClickListener;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.AdapterView.OnItemLongClickListener;import android.widget.ListView;import android.widget.SimpleAdapter;import android.widget.TextView;import android.widget.Toast;public class Result extends Activity{ ContentResolver contentResolver; /* (non-Javadoc) * @see android.app.Activity#onCreate(android.os.Bundle) */ @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.result); final ListView listView=(ListView)findViewById(R.id.show); Intent intent=getIntent(); Bundle bundle=intent.getExtras(); @SuppressWarnings("unchecked") final List
EditItem:
package com.ssdut.dictprovider;import android.app.Activity;import android.content.ContentResolver;import android.content.ContentValues;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class EditItem extends Activity{ String word; String detail; EditText wordeEditText; EditText detailEditText; Button save; ContentResolver contentResolver; /* (non-Javadoc) * @see android.app.Activity#onCreate(android.os.Bundle) */ @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.edit); Intent intent=getIntent(); Bundle bundle=intent.getExtras(); wordeEditText=(EditText)findViewById(R.id.word_list_edit); detailEditText=(EditText)findViewById(R.id.detail_list_edit); word=bundle.getString("word"); detail=bundle.getString("detail"); wordeEditText.setText(word); detailEditText.setText(detail); save=(Button)findViewById(R.id.positive); save.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub contentResolver=getContentResolver(); ContentValues values=new ContentValues(); wordeEditText=(EditText)findViewById(R.id.word_list_edit); detailEditText=(EditText)findViewById(R.id.detail_list_edit); word=wordeEditText.getText().toString(); detail=detailEditText.getText().toString(); values.put(Words.Word.WORD, word); values.put(Words.Word.DETAIL, detail); contentResolver.update(Words.Word.DICT_CONTENT_URI, values, "word like ? or detail like ?",new String[]{"%"+word+"%","%"+detail+"%"}); EditItem.this.finish(); Toast.makeText(EditItem.this, "修改成功", Toast.LENGTH_SHORT).show(); } }); }}
到此我的简易单词本就大功告成了。希望大神多多指点,同时也希望能对刚入门的新手有一定帮助。
Ps:代码内注释较少,这个会在以后弥补的~