1.准备图片:
2.创建商品展示应用程序。先设计布局文件activity_main.xml代码:
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_margin="8dp"
tools:context="cn.edu.bzu.showproduct.MainActivity">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="商品名称"
android:inputType="textPersonName"/>
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="金额"
android:inputType="number"/>
android:layout_width="30dp"
android:layout_height="32dp"
android:src="@drawable/input" />
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/addLL">
使用ImageView的属性Android:src显示图片,只会显示图片原来的大小,不能修改。若用Android:background属性,就可以将使用的图片拉伸。
3.创建ListView布局需要建item.xml文件,代码:
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="13"
android:textColor="@color/colorAccent"
android:textSize="20sp"/>
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="PQ"
android:textColor="@color/colorAccent"
android:textSize="20sp"/>
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="12345"
android:textColor="@color/colorAccent"
android:textSize="20sp"/>
android:layout_height="wrap_content"
android:orientation="vertical">
android:layout_width="18dp"
android:layout_height="18dp"
android:layout_marginBottom="2dp"
android:src="@drawable/up"/>
android:layout_width="18dp"
android:layout_height="18dp"
android:src="@drawable/down"/>
android:layout_width="28dp"
android:layout_height="40dp"
android:src="@drawable/delete"/>
创建三个TextView分别显示数据库中的某条数据的id、商品名称、金额,三个ImageView用于增加金额、减少金额、删除数据。
4.在dao包创建MyHelper类继承SQLiteOpenHelper,代码:
package cn.edu.bzu.showproduct.dao;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class MyHelper extends SQLiteOpenHelper {
public MyHelper(Context context){
super(context,"itcast.db",null,2);
}
public void onCreate(SQLiteDatabase db){
System.out.println("onCreate");
db.execSQL("CREATE TABLE account(_id INTEGER PRIMARY KEY AUTOINCREMENT,name VARCHAR(20),BALANCE INTEGER)");
}
public void onUpgrade(SQLiteDatabase db,int oldVersion,int newViersion){
System.out.println("onUpgrade");
}
}
5.创建Account类,将此类放在新建bean包下利于操作数据库。代码:
package cn.edu.bzu.showproduct.bean;
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();
}
public String toString(){
return "[序号:"+id+",商品名称:"+name+",余额:"+balance+"]";
}
}
6.创建dao包在其下建AccountDao类用于数据逻辑操作。代码:
package cn.edu.bzu.showproduct.dao;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import java.util.ArrayList;
import java.util.List;
import cn.edu.bzu.showproduct.bean.Account;
public class AccountDao {
private MyHelper helper;
public AccountDao(Context context) {
helper = new MyHelper(context);
}
public void insert(Account account) {
SQLiteDatabase db = helper.getWritableDatabase();
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) {
SQLiteDatabase db = helper.getWritableDatabase();
int count = db.delete("account", "_id=?", new String[]{id + ""});
db.close();
return count;
}
public int update(Account account){
SQLiteDatabase db=helper.getWritableDatabase();
ContentValues valuse=new ContentValues();
valuse.put("name",account.getName());
valuse.put("balance",account.getBalance());
int count=db.update("account",valuse,"_id=?",new String[]{account.getId()+""});
db.close();
return count;
}
public List
SQLiteDatabase db=helper.getReadableDatabase();
Cursor c=db.query("account",null,null,null,null,null,"balance DESC");
List
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;
}
}
此类创建了对数据进行增、删、改、 查操作方法。
7.到编写界面交互代码MainActivity,代码:
package cn.edu.bzu.showproduct;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;
import cn.edu.bzu.showproduct.bean.Account;
import cn.edu.bzu.showproduct.dao.AccountDao;
public class MainActivity extends AppCompatActivity {
private List
private AccountDao dao;
private EditText nameET;
private EditText balanceET;
private MyAdapter adapter;
private ListView accountLV;
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);
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
}
private void initView() {
accountLV = (ListView) findViewById(R.id.accoutLV);
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("");
}
private 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;
}
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) {
android.content.DialogInterface.OnClickListener listener = new android.content.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.setPositiveButton("确定", listener);
builder.setNegativeButton("取消", null);
builder.show();
}
});
return item;
}
}
private class MyOnItemClickListener implements AdapterView.OnItemClickListener {
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();
}
}
}