安卓增删改查
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="hhh.com.can"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="点餐管理系统" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> </activity> <activity android:name=".find"> </activity> <activity android:name=".login"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> <activity android:name=".register"> </activity> <activity android:name=".add"> </activity> <activity android:name=".alt"> </activity> </application> </manifest>
import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; public class add extends AppCompatActivity implements View.OnClickListener { private EditText et_name,et_price; private String name,price; private SharedPreferences sp; private SharedPreferences.Editor editor; private int num = 0; private RelativeLayout back_layout; private SQLiteDatabase db; private static String TABLE_NAME = "cais"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_add); initView(); } private void initView(){ et_price = (EditText) findViewById(R.id.et_price); et_name = (EditText) findViewById(R.id.et_name); Button bt_submit = (Button) findViewById(R.id.bt_submit); bt_submit.setOnClickListener(this); sp = getSharedPreferences("user",MODE_PRIVATE); editor = sp.edit(); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.bt_submit: price = et_price.getText().toString().trim(); name = et_name.getText().toString().trim(); if(price.equals("")||name.equals("")){ Toast.makeText(this,"信息不完整!",Toast.LENGTH_SHORT).show(); return; }else{ MySqlite mySQLite = new MySqlite(this, 1); db= mySQLite.getWritableDatabase(); //使用ContentValues添加数据 ContentValues values=new ContentValues(); values.put("name",name); values.put("price",price); db.insert(TABLE_NAME, null, values); db.close(); Toast.makeText(this,"添加成功",Toast.LENGTH_SHORT).show(); finish(); } break; } } }
package hhh.com.can; import android.content.ContentValues; import android.content.SharedPreferences; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.RelativeLayout; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; public class alt extends AppCompatActivity implements View.OnClickListener { EditText et_name,et_price; Button bt_alt; SharedPreferences sp; SharedPreferences.Editor editor; String name,price,id; RelativeLayout back_layout; private static String TABLE_NAME="cais"; private MySqlite mySqlite; private SQLiteDatabase db; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_alt); sp = getSharedPreferences("user",MODE_PRIVATE); editor = sp.edit(); et_name = (EditText) findViewById(R.id.et_name); et_price = (EditText) findViewById(R.id.et_price); name = sp.getString("name",""); id = sp.getString("id",""); price = sp.getString("price",""); et_name.setText(name); et_price.setText(price); bt_alt = (Button) findViewById(R.id.bt_alt); bt_alt.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.bt_alt: price = et_price.getText().toString().trim(); name = et_name.getText().toString().trim(); if(price.equals("")||name.equals("")){ Toast.makeText(this,"信息不完整!",Toast.LENGTH_SHORT).show(); return; } mySqlite=new MySqlite(this,1); db=mySqlite.getWritableDatabase(); ContentValues values=new ContentValues(); values.put("name",name); values.put("price",price); db.update(TABLE_NAME,values,"id=?",new String[]{id}); Toast.makeText(this,"修改成功",Toast.LENGTH_SHORT).show(); finish(); break; } } package hhh.com.can;
import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.TextView; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class find extends AppCompatActivity { private SharedPreferences sp; private TextView tv_name,tv_price; private List<Map<String,String>> list; private MyAdapter adapter; private SharedPreferences.Editor editor; private ListView lv_user; private static String TABLE_NAME = "cais"; private MySqlite mySqlite; private SQLiteDatabase db; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_find); sp = getSharedPreferences("user",MODE_PRIVATE); editor = sp.edit(); lv_user = (ListView) findViewById(R.id.lv_user); list = new ArrayList<>(); getData(); adapter = new MyAdapter( find.this, list, R.layout.item, new String[]{"name","price"}, new int[]{R.id.tv_name,R.id.tv_price} ); lv_user.setAdapter(adapter); lv_user.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) { deleteStudent(list.get(i).get("id")); Toast.makeText(find.this, "删除成功", Toast.LENGTH_SHORT).show(); list.remove(i); adapter.notifyDataSetChanged(); return true; } }); lv_user.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { editor.putString("name",list.get(i).get("name")); editor.putString("price",list.get(i).get("price")); editor.putString("id", list.get(i).get("id")); editor.commit(); Intent intent = new Intent(find.this,alt.class); startActivity(intent); } }); } public void deleteStudent(String i){ mySqlite=new MySqlite(this,1); db=mySqlite.getWritableDatabase(); db.delete("cais","id=?",new String[]{i}); db.close(); } @Override protected void onResume() { super.onResume(); getData(); adapter = new MyAdapter( find.this, list, R.layout.item, new String[]{"name","price"}, new int[]{R.id.tv_name,R.id.tv_price} ); lv_user.setAdapter(adapter); } private class MyAdapter extends SimpleAdapter { public MyAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) { super(context, data, resource, from, to); } @Override public View getView(final int position, View convertView, ViewGroup parent) { View v = super.getView(position, convertView, parent); tv_name = v.findViewById(R.id.tv_name); tv_price = v.findViewById(R.id.tv_price); tv_name.setText(list.get(position).get("name")); tv_price.setText(list.get(position).get("price")); return v; } } public List<Map<String,String>> getData(){ list.clear(); MySqlite mySQLite = new MySqlite(this, 1); SQLiteDatabase database = mySQLite.getReadableDatabase(); Cursor cursor = database.rawQuery("select * from cais", null); System.out.println(cursor.getCount()); while (cursor.moveToNext()) { String name = cursor.getString(cursor.getColumnIndex("name")); String price = cursor.getString(cursor.getColumnIndex("price")); String id = cursor.getString(cursor.getColumnIndex("id")); Map<String,String> map = new HashMap<>(); map.put("name",name); map.put("price",price); map.put("id",id); list.add(map); } return list; }
}