商品展示



一:创建activity_main.xml


二:创建布局文件item.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">


<TextView
    android:text="1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:id="@+id/tvId"
    android:textSize="20sp"
    android:layout_weight="1"/>
<TextView
    android:text="商品名称"
    android:gravity="left"
    android:maxLines="1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/tvName"
    android:textSize="20sp"
    android:layout_weight="2"/>

<TextView
    android:text="金额"
    android:gravity="left"
    android:maxLines="1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/tvAmount"
    android:textSize="20sp"
    android:layout_weight="2"/>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">


    <ImageView
        android:id="@+id/ivUp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@android:drawable/arrow_up_float"/>
    <ImageView
        android:id="@+id/ivDown"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@android:drawable/arrow_down_float"/>
</LinearLayout>
<ImageView
    android:id="@+id/ivDelete"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@android:drawable/ic_menu_delete"/>


</LinearLayout>

三:创建一个数据库DBHelper

public class DBHelper extends SQLiteOpenHelper {
public DBHelper(Context context) {
super(context,"item.db",null,2);
}

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
System.out.println("DB Est");
String sql="create table account (id int primary key,name varchar(20),price int)";
sqLiteDatabase.execSQL(sql);
}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int ov, int nv) {
System.out.println("DB Upgrade !");
}
}

四:界面交互MainActivity


public void onClick(View view) {
String name=et_name.getText().toString().trim();
String price=et_price.getText().toString().trim();
Account a=new Account(name,price.equals("")?0:Integer.parseInt(price));
accountManager.Insert(a);
list.add(a);
myAdapter.notifyDataSetChanged();
listView.setSelection(listView.getCount()-1);
et_name.setText("");
et_price.setText("");
}
});
listView.setOnItemClickListener(new MyOnClickListener());
accountManager=new AccountManager(this);
list=accountManager.Query();
myAdapter=new MyAdapter();
listView.setAdapter(myAdapter);
}
private class MyOnClickListener implements AdapterView.OnItemClickListener{
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
Account a =(Account)adapterView.getItemAtPosition(position);
Toast.makeText(getApplicationContext(),a.toString(),Toast.LENGTH_LONG).show();

}
}
private class MyAdapter extends BaseAdapter{
@Override
public int getCount() {
return list.size();
}

@Override
public Object getItem(int position) {
return list.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View view, ViewGroup viewGroup) {
View item=view!=null?view:View.inflate(getApplicationContext(),R.layout.item,null);
TextView idTV=(TextView)item.findViewById(R.id.num);
TextView nameTV=(TextView)item.findViewById(R.id.tv_name);
TextView priceTV=(TextView)item.findViewById(R.id.tv_price);
final Account a=list.get(position);
idTV.setText(position+1+"");
nameTV.setText(a.getName()+"");
priceTV.setText(a.getPrice()+"");

ImageView iv_up,iv_down,iv_gar;
iv_up=(ImageView)item.findViewById(R.id.up);
iv_down=(ImageView)item.findViewById(R.id.down);
iv_gar=(ImageView)item.findViewById(R.id.garbage);

iv_up.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.i("-------->","aa");
a.setPrice(a.getPrice()+1);
notifyDataSetChanged();
accountManager.Update(a);
}
});
iv_down.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.i("-------->","bb");
a.setPrice(a.getPrice()-1);
notifyDataSetChanged();
accountManager.Update(a);
}
});
iv_gar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder builder =new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Waring");
builder.setMessage("This operate will NOT be canceled,would you like Continue?");
builder.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//执行删除方法
list.remove(a);
accountManager.Delete(a.getId());
notifyDataSetChanged();
dialogInterface.dismiss();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
//勿忘最后一定要create&show!
builder.create().show();
}
});
return item;
}

}
}
五:定义一个实体类Account,作为适配器的适配类型
package com.example.bz0209.shop_manager;

/**
* Created by Administrator on 2017/4/13.
*/

public class Account {
private long id;
private String name;
private int price;

public Account(long id, String name, int price) {
super();
this.id = id;
this.name = name;
this.price = price;
}

public Account(String name, int price) {
super();
this.name = name;
this.price = price;
}
public Account() {
super();
}
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 int getPrice() {
return price;
}

public void setPrice(int price) {
this.price = price;
}

@Override
public String toString() {
return "Account{" +
"id=" + id +
", name='" + name + '\'' +
", price=" + price +
'}';
}
}
}
六:创建数据操作逻辑类
创建一个AccountAdapter适配器
public class AccountManager {
private MyHelper myHelper;
public AccountManager(Context context){
myHelper=new MyHelper(context);
}
public void Insert(Account account){
SQLiteDatabase db=myHelper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put("name",account.getName());
values.put("price",account.getPrice());
long id=db.insert("account",null,values);
account.setId(id);
}
public int Delete(long id){
SQLiteDatabase db=myHelper.getWritableDatabase();
int count=db.delete("account","id=?",new String[]{id+""});
db.close();
Log.i("--------->","SSSS");
return count;
}
public int Update(Account account){
SQLiteDatabase db= myHelper.getWritableDatabase();
ContentValues values =new ContentValues();
values.put("name",account.getName());
values.put("price",account.getPrice());
int count=db.delete("account","id=?",new String[] {account.getId()+""});
db.close();
return count;
}
public List<Account> Query(){
SQLiteDatabase db= myHelper.getReadableDatabase();
Cursor cursor =db.query("account",null,null,null,null,null,"price DESC");
List<Account> list =new ArrayList<Account>();
while(cursor.moveToNext()){
long id =cursor.getLong(cursor.getColumnIndex("id"));
String name =cursor.getString(1);
int price=cursor.getInt(2);
list.add(new Account(id,name,price));
}
cursor.close();
db.close();
return list;
}

}

七。界面



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值