博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android:简易单词本(三)
阅读量:6441 次
发布时间:2019-06-23

本文共 9147 字,大约阅读时间需要 30 分钟。

hot3.png

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:代码内注释较少,这个会在以后弥补的~

转载于:https://my.oschina.net/xiaoming2014/blog/263997

你可能感兴趣的文章
linux sed命令
查看>>
浅谈当下网页设计趋势
查看>>
TCP 滑动窗口和 拥塞窗口
查看>>
VS2008调试程序时出现"XXX mutex not created."
查看>>
解决Java连接MySQL存储过程返回参数值为乱码问题
查看>>
c++ 字符检测 TCharacter
查看>>
MalformedObjectNameException: Invalid character '' in value part of property
查看>>
Hadoop格式化HDFS报错java.net.UnknownHostException: localhost.localdomain: localhost.localdomain
查看>>
android 40 Io编程
查看>>
STL之Vector(不定长数组)
查看>>
Ext江湖
查看>>
一起谈.NET技术,实战ASP.NET大规模网站架构:Web加速器
查看>>
RHEL 6.6下Python 2.6.6升级到Python 3.6.6
查看>>
linux 内核启动过程以及挂载android 根文件系统的过程 ( 转)
查看>>
shell每日更新(7)
查看>>
单词的个数
查看>>
从程序员到项目经理(27):怎样给领导汇报工作
查看>>
eclipse工程 'cocostudio/CocoStudio.h' file not found
查看>>
045医疗项目-模块四:采购单模块—采购单提交(Dao,Service,Action三层)
查看>>
dockerfile创建php容器(安装memcached、redis、gd、xdebug扩展)
查看>>