1.SQLite數據庫的DAO標準CRUD操做:android
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微信項目的第三板塊git
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); } }
碼雲地址:https://gitee.com/zhcay/move_job數據庫