一、设计主界面
使用系统自带的图片作为drawable使用的图片
主布局:activity_main
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="10dp"> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:id="@+id/idTV" android:layout_weight="1" android:text="13" android:textColor="#000000" android:textSize="20sp" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:id="@+id/nameTV" android:layout_weight="2" android:text="PQ" android:singleLine="true" android:textColor="#000000" android:textSize="20sp" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:id="@+id/balanceTV" android:layout_weight="2" android:text="12345" android:singleLine="true" android:textColor="#000000" android:textSize="20sp" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/upIV" android:layout_marginBottom="2dp" android:src="@android:drawable/arrow_up_float"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/downIV" android:layout_marginBottom="2dp" android:src="@android:drawable/arrow_down_float"/> </LinearLayout> <ImageView android:layout_width="25dp" android:layout_height="25dp" android:id="@+id/deleteIV" android:src="@android:drawable/ic_menu_delete"/> </LinearLayout>
二、创建ListView Item布局
创建了三个TextView和三个ImageView
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="10dp"> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:id="@+id/idTV" android:layout_weight="1" android:text="13" android:textColor="#000000" android:textSize="20sp" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:id="@+id/nameTV" android:layout_weight="2" android:text="PQ" android:singleLine="true" android:textColor="#000000" android:textSize="20sp" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:id="@+id/balanceTV" android:layout_weight="2" android:text="12345" android:singleLine="true" android:textColor="#000000" android:textSize="20sp" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/upIV" android:layout_marginBottom="2dp" android:src="@android:drawable/arrow_up_float"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/downIV" android:layout_marginBottom="2dp" android:src="@android:drawable/arrow_down_float"/> </LinearLayout> <ImageView android:layout_width="25dp" android:layout_height="25dp" android:id="@+id/deleteIV" android:src="@android:drawable/ic_menu_delete"/> </LinearLayout>
三、创建数据库
在系统包下创建一个名为dao的包 定义一个MyHelper类继承SQLiteOpenHelper
代码如下:
public class MyHelper extends SQLiteOpenHelper { public MyHelper(Context context) { super(context,"18Lab10.db", null, 2); } @Override public void onCreate(SQLiteDatabase db) { System.out.println("onCreate"); db.execSQL("CREATE TABLE account(_id INTEGER PRIMARY KEY AUTOINCREMENT,name VARCHAR(20),balance INTEGER)"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { System.out.println("onUpgrade"); } }四、创建Account类
在操作数据库时将数据存放在一个对象中操作会更方便,因此需要创建一个bean包来存放Javabean类然后再定义一个类Account
public class Account { private Long id; private String name; private Integer balance; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getBalance() { return balance; } public void setBalance(Integer balance) { this.balance = balance; } public Account(long id, String name, Integer balance) { super(); this.id=id; this.name = name; this.balance = balance; } public Account(String name, Integer balance) { super(); this.name = name; this.balance = balance; } public Account() { super(); } @Override public String toString() { return "[序号:" + id + ", 商品名称'" + name + '\'' + ", 余额:" + balance + ']'; } }五、创建数据操作逻辑类在dao包下创建一个AccountDao类用于操作数据public class AccountDao { private MyHelper helper; SQLiteDatabase db=helper.getWritableDatabase(); public AccountDao(Context context) { helper=new MyHelper(context); } public void insert(Account account){ ContentValues values =new ContentValues(); values.put("name",account.getName()); values.put("balance",account.getBalance()); long id =db.insert("account",null,values); account.setId(id); db.close(); } public int delete(long id){ int count=db.delete("account","_id=?",new String[] {id+""}); db.close(); return count; } public int update(Account account){ ContentValues values=new ContentValues(); values.put("name",account.getName()); values.put("balance",account.getBalance()); int count=db.update("account",values,"_id=?",new String[] {account.getId()+""}); db.close(); return count; } public List<Account> queryAll(){ Cursor c=db.query("account",null,null,null,null,null,"balance DESC"); List<Account> list=new ArrayList<Account>(); while (c.moveToNext()){ long id =c.getLong(c.getColumnIndex("_id")); String name=c.getString(1); int balance=c.getInt(2); list.add(new Account(id,name,balance)); } c.close(); db.close(); return list; } }六、编写界面交互代码(main_activity)public class MainActivity extends AppCompatActivity { private List<Account> list; private AccountDao dao; private EditText nameET; private EditText balanceET; private MyAdapter adapter; private ListView accountLV; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); dao = new AccountDao(this); list = dao.queryAll(); adapter = new MyAdapter(); accountLV.setAdapter( adapter); } private void initView() { accountLV = (ListView) findViewById(R.id.accountLV); nameET = (EditText) findViewById(R.id.nameET); balanceET = (EditText) findViewById(R.id.balanceET); accountLV.setOnItemClickListener(new MyOnItemClickListener()); } public void add(View v){ String name=nameET.getText().toString().trim(); String balance=balanceET.getText().toString().trim(); Account a=new Account(name,balance.equals("") ? 0:Integer.parseInt(balance)); dao.insert(a); list.add(a); adapter.notifyDataSetChanged(); accountLV.setSelection(accountLV.getCount()-1); nameET.setText(""); balanceET.setText(""); } public class MyAdapter extends BaseAdapter { public int getCount(){ return list.size(); } public Object getItem(int position){ return list.get(position); } public long getItemId(int position){ return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { View item=convertView!=null?convertView:View.inflate(getApplicationContext(),R.layout.item,null); TextView idTV=(TextView)item.findViewById(R.id.idTV); TextView nameTV=(TextView)item.findViewById(R.id.nameTV); TextView balanceTV=(TextView)item.findViewById(R.id.balanceTV); final Account a =list.get(position); idTV.setText(a.getId()+""); nameTV.setText(a.getName()); balanceTV.setText(a.getBalance()+""); ImageView upIV=(ImageView)item.findViewById(R.id.upIV); ImageView downIV=(ImageView)item.findViewById(R.id.downIV); ImageView deleteIV=(ImageView)item.findViewById(R.id.deleteIV); upIV.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { a.setBalance(a.getBalance()+1); notifyDataSetChanged(); dao.update(a); } }); downIV.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { a.setBalance(a.getBalance()+1); notifyDataSetChanged(); dao.update(a); } }); deleteIV.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { DialogInterface.OnClickListener listener=new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialog,int which){ list.remove(a); dao.delete(a.getId()); notifyDataSetChanged(); } }; AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this); builder.setTitle("确定要删除吗?"); builder.setNegativeButton("取消",null); builder.setPositiveButton("确定",listener); builder.show(); } }); return item; } } private class MyOnItemClickListener implements AdapterView.OnItemClickListener { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Account a =(Account)parent.getItemAtPosition(position); Toast.makeText(getApplicationContext(),a.toString(),Toast.LENGTH_SHORT).show(); } } }上面的代码中我们引用了一个AlertDialog,并用Builder方法形成了一个对象链,通过一系列的设置方法,构造出我们需要的对话框,然 后调用show方法显示出来,
在onCreate方法中调用,只需传入this即 可。