androidsqlite增删查demo
package com.example.wtw.learnlayout; import android.app.AlertDialog; import android.app.ListActivity; import android.content.ContentValues; import android.content.DialogInterface; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.Button; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.SimpleCursorAdapter; import android.widget.Toast; import com.example.wtw.common.Db; public class ListViewActivity extends ListActivity { private SimpleCursorAdapter adapter; private EditText edName; //private EditText edSex; RadioGroup edSex; private Button add; private Db db; private SQLiteDatabase dbRead; private SQLiteDatabase dbWrite; //添加按钮事件 private View.OnClickListener addbtn = new View.OnClickListener() { @Override public void onClick(View v) { ContentValues cv = new ContentValues(); if (edName.getText().toString().isEmpty() //|| edSex.getText().toString().isEmpty() || edSex.getCheckedRadioButtonId()==-1 ){ Toast.makeText(v.getContext(),"不能为空",Toast.LENGTH_SHORT).show(); }else{ String name = edName.getText().toString(); //String sex = edSex.getText().toString(); RadioButton r = (RadioButton) findViewById(edSex.getCheckedRadioButtonId()); Log.i("mylog","radioID="+edSex.getCheckedRadioButtonId()); String sex = r.getText().toString(); cv.put("name",name); cv.put("sex",sex); dbWrite.insert("user",null,cv); Toast.makeText(v.getContext(),"成功",Toast.LENGTH_SHORT).show(); Log.i("mylog","新增成功"); refreshListView(); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_list_view); edName = (EditText) findViewById(R.id.edName); //edSex = (EditText) findViewById(R.id.edSex); edSex = (RadioGroup) findViewById(R.id.edSex); add = (Button) findViewById(R.id.btn_add); add.setOnClickListener(addbtn); db = new Db(this); dbRead = db.getReadableDatabase(); dbWrite = db.getWritableDatabase(); adapter = new SimpleCursorAdapter(this,R.layout.user_list,null,new String[]{"name","sex"},new int[]{R.id.tvName,R.id.tvSex}); Log.i("mylog","正在加载LISTVIEW数据"); setListAdapter(adapter); refreshListView(); //listview 添加长按事件 getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView> parent, View view, final int position, long id) { //弹出框,title设置弹出框的标题,message设置弹出框的内容,negative设置取消按钮,positive设置确定按钮及事件 new AlertDialog.Builder(ListViewActivity.this).setTitle("提醒") .setMessage("您确定删除该项吗?") .setNegativeButton("取消",null) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Cursor c = adapter.getCursor(); //c.moveToPosition(position);//从LISTVIEW 中移除 int itemid = c.getInt(c.getColumnIndex("_id")); Log.i("mylog","删除ID="+itemid); dbWrite.delete("user","_id=?",new String[]{itemid+""});//在数据库中删除数据 refreshListView();//刷新 } }).show(); return true; } }); } //刷新 public void refreshListView(){ Cursor c = dbRead.query("user",null,null,null,null,null,null,null); adapter.changeCursor(c); } }
新闻名称:androidsqlite增删查demo
URL标题:http://ybzwz.com/article/gpdige.html