android studio中ListView与SQLite的结合使用
Posted 玉天恒
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android studio中ListView与SQLite的结合使用相关的知识,希望对你有一定的参考价值。
Da.java
public class Db extends SQLiteOpenHelper { public Db(Context context) { super(context, "db", null, 1); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("CREATE TABLE user2(" + "_id INT DEFAULT 0," + "name TEXT DEFAULT \"\"," + "sex TEXT DEFAULT \"\")"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }
MainActivity.java
public class MainActivity extends AppCompatActivity { private SimpleCursorAdapter mAdapter; private ListView listView; private Button btn_add; private Button btn_delete; private Button btn_back; private EditText et_id; private EditText et_name; private EditText et_sex; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn_add = (Button) findViewById(R.id.btn_add); btn_delete = (Button) findViewById(R.id.btn_delete); listView = (ListView) findViewById(R.id.list); btn_add.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { addData(); refleshListView(); } }); btn_delete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { deleteData(); refleshListView(); } }); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // Toast.makeText(MainActivity.this,""+position,Toast.LENGTH_SHORT).show(); int positions = position + 1; Db db = new Db(MainActivity.this); SQLiteDatabase dbWrite = db.getWritableDatabase(); dbWrite.delete("user2","_id=?",new String[]{""+positions}); dbWrite.close(); refleshListView(); } }); } public void addData() { btn_back = (Button) findViewById(R.id.btn_back); et_id = (EditText) findViewById(R.id.et_id); et_name = (EditText) findViewById(R.id.et_name); et_sex = (EditText) findViewById(R.id.et_sex); btn_add = (Button) findViewById(R.id.btn_add); btn_delete = (Button) findViewById(R.id.btn_delete); listView = (ListView) findViewById(R.id.list); btn_add.setVisibility(View.GONE); btn_delete.setVisibility(View.GONE); listView.setVisibility(View.GONE); btn_back.setVisibility(View.VISIBLE); et_id.setVisibility(View.VISIBLE); et_name.setVisibility(View.VISIBLE); et_sex.setVisibility(View.VISIBLE); btn_back.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { btn_add.setVisibility(View.VISIBLE); btn_delete.setVisibility(View.VISIBLE); listView.setVisibility(View.VISIBLE); btn_back.setVisibility(View.GONE); et_id.setVisibility(View.GONE); et_name.setVisibility(View.GONE); et_sex.setVisibility(View.GONE); String id = et_id.getText().toString(), name =et_name.getText().toString(), sex = et_sex.getText().toString(); Db db = new Db(MainActivity.this); SQLiteDatabase dbWrite = db.getWritableDatabase(); ContentValues cv = new ContentValues(); cv.put("_id", id); cv.put("name", name); cv.put("sex", sex); dbWrite.insert("user2", null, cv); dbWrite.close(); refleshListView(); } }); // String id = et_id.getText().toString(), // name =et_name.getText().toString(), // sex = et_sex.getText().toString(); // // // Db db = new Db(MainActivity.this); // SQLiteDatabase dbWrite = db.getWritableDatabase(); // // ContentValues cv = new ContentValues(); // cv.put("_id",id); // cv.put("name", name); // cv.put("sex", sex); // dbWrite.insert("user2", null, cv); // // dbWrite.close(); } public void deleteData() { Db db = new Db(MainActivity.this); SQLiteDatabase dbWrite = db.getWritableDatabase(); dbWrite.delete("user2",null,new String[]{}); dbWrite.close(); } public void refleshListView() { Db db = new Db(MainActivity.this); SQLiteDatabase dbRead = db.getReadableDatabase(); Cursor c = dbRead.query("user2", null, null, null, null, null, null); mAdapter = new SimpleCursorAdapter(MainActivity.this, R.layout.user_info, c, new String[]{"_id","name","sex"}, new int[]{R.id._id,R.id._name,R.id._sex}); listView.setAdapter(mAdapter); dbRead.close(); } }
user_info.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <TextView android:id="@+id/_id" android:textSize="20sp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/_name" android:textSize="20sp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/_sex" android:textSize="20sp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <Button android:id="@+id/btn_add" android:text="Add" android:textSize="20sp" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/btn_delete" android:text="Delete" android:textSize="20sp" android:layout_width="match_parent" android:layout_height="wrap_content" /> <ListView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="wrap_content"/> <Button android:id="@+id/btn_back" android:text="确定" android:textSize="20sp" android:layout_width="match_parent" android:layout_height="wrap_content" /> <EditText android:id="@+id/et_id" android:visibility="gone" android:layout_width="match_parent" android:layout_height="40dp" /> <EditText android:id="@+id/et_name" android:visibility="gone" android:layout_width="match_parent" android:layout_height="40dp" /> <EditText android:id="@+id/et_sex" android:visibility="gone" android:layout_width="match_parent" android:layout_height="40dp" /> </LinearLayout>
以上是关于android studio中ListView与SQLite的结合使用的主要内容,如果未能解决你的问题,请参考以下文章
滚动时,Android Studio自定义ListView与图像滞后
Android Studio 中带有 imageview 的 listview
Android Studio(Kotlin)之RecyclerView