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> list=(List>)bundle.getSerializable("data");
final SimpleAdapter adapter=new SimpleAdapter(Result.this, list, R.layout.line, new String[]{"word","detail"}, new int[]{R.id.word_list,R.id.detail_list});
listView.setAdapter(adapter);
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView> arg0, View arg1,
final int arg2, long arg3) {
// TODO Auto-generated method stub
final String word=((TextView)arg1.findViewById(R.id.word_list)).getText().toString();
final String detail=((TextView)arg1.findViewById(R.id.detail_list)).getText().toString();
AlertDialog.Builder builder=new Builder(Result.this);
builder.setItems(R.array.array, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
String select[] =getResources().getStringArray(R.array.array);
if(select[which].equals("删除")){
AlertDialog.Builder alert=new Builder(Result.this);
alert.setTitle("确定删除?");
alert.setPositiveButton("确定", new OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
contentResolver=getContentResolver();
contentResolver.delete(Words.Word.DICT_CONTENT_URI, "word like ?",new String[]{"%"+word+"%"} );
list.remove(arg2);
adapter.notifyDataSetChanged();
}
});
alert.setNegativeButton("取消", new OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
Toast.makeText(Result.this, "你已取消", Toast.LENGTH_SHORT).show();
}
});
alert.show();
}
else if(select[which].equals("编辑")){
Bundle bundle=new Bundle();
Intent intent=new Intent();
bundle.putString("word", word);
bundle.putString("detail", detail);
intent.putExtras(bundle);
intent.setClass(Result.this, EditItem.class);
startActivity(intent);
}
}
});
builder.show();
/*
builder.setNegativeButton("取消", new OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
Toast.makeText(Result.this, "你已经取消", Toast.LENGTH_SHORT).show();
}
});
builder.setPositiveButton("确定", new OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg) {
// TODO Auto-generated method stub
contentResolver=getContentResolver();
contentResolver.delete(Words.Word.DICT_CONTENT_URI, "word like ?",new String[]{"%"+word+"%"} );
list.remove(arg2);
adapter.notifyDataSetChanged();
}
});
builder.show();
*/
return false;
}
});
}
/* (non-Javadoc)
* @see android.app.Activity#onResume()
*/
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
System.out.println("刷新");
}
}
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:代码内注释较少,这个会在以后弥补的~