溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

android sqlite 增刪查 demo

發(fā)布時(shí)間:2020-07-01 08:01:21 來源:網(wǎng)絡(luò) 閱讀:510 作者:hlp666666 欄目:移動(dòng)開發(fā)
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 添加長(zhǎng)按事件
        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);
    }

}


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI