您的位置:首页 > 其它

商品展示

2017-06-15 13:09 148 查看
一:创建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(Acc
4000
ount 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;

}

}

七。界面

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: