商品展示

一:activity_main.xml
创建一个Containers下的ListView条目




二:创建布局文件item.xml

<TextView
android:layout_marginTop="5dp"
android:id="@+id/num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="99"
android:textSize="25sp"
/>
<TextView
android:id="@+id/tv_name"
android:layout_marginTop="5dp"
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="NNN"
android:textSize="25sp"
android:layout_weight="1"
/>
<TextView
android:id="@+id/tv_price"
android:layout_marginTop="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PPP"
android:textSize="25sp"
android:layout_weight="1"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/up"
android:layout_width="20dp"
android:layout_height="20dp"
android:src="@drawable/up_arrow"/>
<ImageView
android:id="@+id/down"
android:layout_width="20dp"
android:layout_height="20dp"
android:src="@drawable/down_arrow"/>
</LinearLayout>
<ImageView
android:id="@+id/garbage"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/garbage"/>
三:创建一个数据库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 class MainActivity extends AppCompatActivity {

private List<Account> list;
private AccountManager accountManager;
EditText et_name;
EditText et_price;
TextView destory;
private MyAdapter myAdapter;
private ListView listView;
ImageView button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=(ListView)findViewById(R.id.lv_item);
et_name=(EditText)findViewById(R.id.et_name);
et_price=(EditText)findViewById(R.id.et_price);
button=(ImageView) findViewById(R.id.add);
destory=(TextView)findViewById(R.id.title1);
destory.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

}
});
button.setOnClickListener(new View.OnClickListener() {
@Override
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;
}

}



猜你喜欢

转载自blog.csdn.net/lulu17862078553/article/details/71007126