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數(shù)據(jù)");
setListAdapter(adapter);
refreshListView();
//listview 添加長按事件
getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
//彈出框,title設(shè)置彈出框的標(biāo)題,message設(shè)置彈出框的內(nèi)容,negative設(shè)置取消按鈕,positive設(shè)置確定按鈕及事件
new AlertDialog.Builder(ListViewActivity.this).setTitle("提醒")
.setMessage("您確定刪除該項(xiàng)嗎?")
.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+""});//在數(shù)據(jù)庫中刪除數(shù)據(jù)
refreshListView();//刷新
}
}).show();
return true;
}
});
}
//刷新
public void refreshListView(){
Cursor c = dbRead.query("user",null,null,null,null,null,null,null);
adapter.changeCursor(c);
}
}
新聞標(biāo)題:androidsqlite增刪查demo
瀏覽地址:http://www.chinadenli.net/article14/gpdige.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、微信公眾號(hào)、動(dòng)態(tài)網(wǎng)站、網(wǎng)站內(nèi)鏈、外貿(mào)網(wǎng)站建設(shè)、Google
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)