1.SQLite数据库的DAO标准CRUD操作:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private MyDAO myDAO; //数据库访问对象 private ListView listView; private List<Map<String,Object>> listData; private Map<String,Object> listItem; private SimpleAdapter listAdapter; private EditText et_name; //数据表包含3个字段,第1字段为自增长类型 private EditText et_age; private String selId=null; //选择项id @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button bt_add= (Button) findViewById(R.id.bt_add);bt_add.setOnClickListener(this); Button bt_modify=(Button)findViewById(R.id.bt_modify);bt_modify.setOnClickListener(this); Button bt_del=(Button)findViewById(R.id.bt_del);bt_del.setOnClickListener(this); et_name=(EditText)findViewById(R.id.et_name); et_age=(EditText)findViewById(R.id.et_age); myDAO = new MyDAO(this); //创建数据库访问对象 if(myDAO.getRecordsNumber()==0) { //防止重复运行时重复插入记录 myDAO.insertInfo("xuhuan", 21); //插入记录 myDAO.insertInfo("xill", 21); //插入记录 } displayRecords(); //显示记录 } public void displayRecords(){ //显示记录方法定义 listView = (ListView)findViewById(R.id.listView); listData = new ArrayList<Map<String,Object>>(); Cursor cursor = myDAO.allQuery(); while (cursor.moveToNext()){ int id=cursor.getInt(0); //获取字段值 String name=cursor.getString(1); //int age=cursor.getInt(2); int age=cursor.getInt(cursor.getColumnIndex("age"));//推荐此种方式 listItem=new HashMap<String,Object>(); //必须在循环体里新建 listItem.put("_id", id); //第1参数为键名,第2参数为键值 listItem.put("name", name); listItem.put("age", age); listData.add(listItem); //添加一条记录 } listAdapter = new SimpleAdapter(this, listData, R.layout.list_item, //自行创建的列表项布局 new String[]{"_id","name","age"}, new int[]{R.id.tv_id,R.id.tvname,R.id.tvage}); listView.setAdapter(listAdapter); //应用适配器 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { //列表项监听 @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Map<String,Object> rec= (Map<String, Object>) listAdapter.getItem(position); //从适配器取记录 et_name.setText(rec.get("name").toString()); //刷新文本框 et_age.setText(rec.get("age").toString()); Log.i("ly",rec.get("_id").toString()); selId=rec.get("_id").toString(); //供修改和删除时使用 } }); } @Override public void onClick(View v) { //实现的接口方法 if(selId!=null) { //选择了列表项后,可以增加/删除/修改 String p1 = et_name.getText().toString().trim(); int p2 = Integer.parseInt(et_age.getText().toString()); switch (v.getId()){ case R.id.bt_add: myDAO.insertInfo(p1,p2); break; case R.id.bt_modify: myDAO.updateInfo(p1,p2,selId); Toast.makeText(getApplicationContext(),"更新成功!",Toast.LENGTH_SHORT).show(); break; case R.id.bt_del: myDAO.deleteInfo(selId); Toast.makeText(getApplicationContext(),"删除成功!",Toast.LENGTH_SHORT).show(); et_name.setText(null);et_age.setText(null); selId=null; //提示 } }else{ //未选择列表项 if(v.getId()==R.id.bt_add) { //单击添加按钮 String p1 = et_name.getText().toString(); String p2=et_age.getText().toString(); if(p1.equals("")||p2.equals("")){ //要求输入了信息 Toast.makeText(getApplicationContext(),"姓名和年龄都不能空!",Toast.LENGTH_SHORT).show(); }else{ myDAO.insertInfo(p1, Integer.parseInt(p2)); //第2参数转型 } } else{ //单击了修改或删除按钮 Toast.makeText(getApplicationContext(),"请先选择记录!",Toast.LENGTH_SHORT).show(); } } displayRecords();//刷新ListView对象 } }
2.as微信项目的第三板块
package com.example.myweixin; import androidx.appcompat.app.AppCompatActivity; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentTransaction; import android.content.DialogInterface; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.Window; import android.widget.FrameLayout; import android.widget.ImageButton; import android.widget.LinearLayout; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Fragment mtab01=new weixinFragment(); private Fragment mtab02=new friendFragment(); private Fragment mtab03=new contactFragment(); private Fragment mtab04=new setFragment(); private FragmentManager fm; private LinearLayout mtabweixin; private LinearLayout mtabfrd; private LinearLayout mtabaddress; private LinearLayout mtabset; private ImageButton mimgweixin; private ImageButton mimgfrd; private ImageButton mimgaddress; private ImageButton mimgset; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initFragment(); initEvent(); selectfragment(0); } private void initFragment(){ fm=getSupportFragmentManager(); FragmentTransaction transaction=fm.beginTransaction(); transaction.add(R.id.id_content,mtab01); transaction.add(R.id.id_content,mtab02); transaction.add(R.id.id_content,mtab03); transaction.add(R.id.id_content,mtab04); transaction.commit(); } private void initView(){ mtabweixin=(LinearLayout) findViewById(R.id.weixin); mtabfrd=(LinearLayout) findViewById(R.id.frd); mtabaddress=(LinearLayout) findViewById(R.id.contact); mtabset=(LinearLayout) findViewById(R.id.setting); mimgweixin=findViewById(R.id.imageButton1); mimgfrd=findViewById(R.id.imageButton2); mimgaddress=findViewById(R.id.imageButton3); mimgset=findViewById(R.id.imageButton4); } private void initEvent(){ mtabweixin.setOnClickListener(this); mtabfrd.setOnClickListener(this); mtabaddress.setOnClickListener(this); mtabset.setOnClickListener(this); } private void hidefragment(FragmentTransaction transaction){ transaction.hide(mtab01); transaction.hide(mtab02); transaction.hide(mtab03); transaction.hide(mtab04); } private void selectfragment(int i){ FragmentTransaction transaction=fm.beginTransaction(); hidefragment(transaction); switch (i){ case 0: transaction.show(mtab01); mimgweixin.setImageResource(R.drawable.tab_weixin_pressed); break; case 1: transaction.show(mtab02); mimgfrd.setImageResource(R.drawable.tab_find_frd_pressed); break; case 2: transaction.show(mtab03); mimgaddress.setImageResource(R.drawable.tab_address_pressed); break; case 3: transaction.show(mtab04); mimgset.setImageResource(R.drawable.tab_settings_pressed); break; default: break; } transaction.commit(); } @Override public void onClick(View v){ resetimg(); switch (v.getId()){ case R.id.imageButton1: selectfragment(0); System.out.println(v.getId()); break; case R.id.imageButton2: selectfragment(1); break; case R.id.imageButton3: selectfragment(2); break; case R.id.imageButton4: selectfragment(3); break; default: break; } } public void resetimg(){ mimgweixin.setImageResource(R.drawable.tab_weixin_normal); mimgfrd.setImageResource(R.drawable.tab_find_frd_normal); mimgaddress.setImageResource(R.drawable.tab_address_normal); mimgset.setImageResource(R.drawable.tab_settings_normal); } }