您好,登錄后才能下訂單哦!
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); } }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。