您的位置:首页 > 其它

大学安卓课程,实现模拟商店的构建!

2017-05-04 10:23 337 查看
实现一个商店的形成,有增删改查等!





这是商店的构建,下面实现数据库的代码书写然后在进行数据库的链接和增删改查按钮及代码的书写。

下面我把代码黏贴在下面:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_margin="8dp"
tools:context="com.example.renxiaohen.gz_shop.MainActivity">
<LinearLayout
android:id="@+id/addLL"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/etname"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="商品名称"
android:inputType="textPersonName"/>
<EditText
android:id="@+id/etamount"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="商品金额"
android:inputType="number"/>
<ImageView
android:id="@+id/ivadd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="addGoods"
<
4000
span style="color:#008000;">android:src="@android:drawable/ic_input_add"
/>
</LinearLayout>
<ListView
android:id="@+id/lvGoods"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</ListView>

</LinearLayout>


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/tvId"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1"
android:textSize="20sp"
/>
<TextView
android:id="@+id/tvname"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="left"
android:text="商品名称"
android:textSize="20sp"
/>
<TextView
android:id="@+id/tvamount"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="left"
android:text="商品金额"
android:textSize="20sp"
/>

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

<ImageView
android:id="@+id/ivup"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_marginBottom="2dp"
android:src="@android:drawable/arrow_up_float" />

<ImageView
android:id="@+id/ivdown"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_marginBottom="2dp"
android:src="@android:drawable/arrow_down_float" />
</LinearLayout>

<ImageView
android:id="@+id/ivdelete"
android:layout_width="25dp"
android:layout_height="25dp"
android:src="@android:drawable/ic_menu_delete"/>

</LinearLayout>

package com.example.renxiaohen.gz_shop.ently;

/**
* Created by renxiaohen on 2017/4/28.
*/

public class Goods {
private Long id;
private String name;
private Integer Amount;

public Goods(Long id, String name, Integer amount) {
this.id = id;
this.name = name;
Amount = amount;
}

public Goods(String name, Integer amount) {
this.name = name;
Amount = amount;
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getName() {
<
e00a
span style="color:#000080;">return name;
}

public void setName(String name) {
this.name = name;
}

public Integer getAmount() {
return Amount;
}

public void setAmount(Integer amount) {
Amount = amount;
}

@Override
public String toString() {
return "Goods{" +
"id=" + id +
", name='" + name + '\'' +
", Amount=" + Amount +
'}';
}
}

package com.example.renxiaohen.gz_shop.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.renxiaohen.gz_shop.db.DBHelper;
import com.example.renxiaohen.gz_shop.ently.Goods;

import java.util.ArrayList;
import java.util.List;

/**
* Created by renxiaohen on 2017/4/29.
*/

public class Goodsdao {
private DBHelper dbHelper;
public Goodsdao (Context context){
dbHelper=new DBHelper(context,1);
}
public void add(Goods goods){
SQLiteDatabase sqLiteDatabase=dbHelper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put("name",goods.getName());
values.put("num",goods.getAmount());
long id=sqLiteDatabase.insert("goods",null,values);
goods.setId(id);
sqLiteDatabase.close();
}
public int delete(long id) {
SQLiteDatabase sqLiteDatabase = dbHelper.getWritableDatabase();
int count=sqLiteDatabase.delete("goods","_id",new String[]{id+""});
sqLiteDatabase.close();
return count;
}
public int update(Goods goods){
SQLiteDatabase sqLiteDatabase=dbHelper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put("name",goods.getName());
values.put("num",goods.getAmount());
int count=sqLiteDatabase.update("goods",values,"_id=?",new String[]{goods.getId()+""});
sqLiteDatabase.close();
return count;
}
public List<Goods>queryAll(){
List<Goods>goodsList=new ArrayList<>();
SQLiteDatabase sqLiteDatabase=dbHelper.getReadableDatabase();
Cursor cursor=sqLiteDatabase.query("goods",null,null,null,null,null,"amount desc");
while(cursor.moveToNext()){
long id=cursor.getLong(cursor.getColumnIndex("_id"));
String name=cursor.getString(cursor.getColumnIndex("name"));
int amount=cursor.getInt(cursor.getColumnIndex("amount"));
Goods goods=new Goods(id,name,amount);
goodsList.add(goods);
}
cursor.close();
sqLiteDatabase.close();
return goodsList;
}
}

package com.example.renxiaohen.gz_shop;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.renxiaohen.gz_shop.dao.Goodsdao;
import com.example.renxiaohen.gz_shop.ently.Goods;

import java.util.List;

/**
* Created by renxiaohen on 2017/4/28.
*/

public class GoodsAdapter extends ArrayAdapter<Goods>{
private int resouncelId;
private Goodsdao goodsdao;
private List<Goods> goodsList;
public GoodsAdapter(@NonNull Context context, @LayoutRes int resource, List<Goods> objects,Goodsdao goodsdao) {
super(context, resource, objects);
resouncelId=resource;
goodsList=objects;
this.goodsdao=goodsdao;
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
final Goods goods=getItem(position);
View view=null;
ViewHolder viewHolder;
if(convertView==null){
view=LayoutInflater.from(getContext()).inflate(R.layout.item,null);
viewHolder=new ViewHolder();
viewHolder.tvId= (TextView) view.findViewById(R.id.tvId);
viewHolder.tvname= (TextView) view.findViewById(R.id.tvname);
viewHolder.tvamount= (TextView) view.findViewById(R.id.tvamount);
viewHolder.ivup= (ImageView) view.findViewById(R.id.ivup);
viewHolder.ivdown= (ImageView) view.findViewById(R.id.ivdown);
viewHolder.ivdelete= (ImageView) view.findViewById(R.id.ivdelete);
view.setTag(viewHolder);
}else{
view=convertView;
viewHolder= (ViewHolder) view.getTag();
}
viewHolder.tvId.setText(goods.getId()+"");
viewHolder.tvname.setText(goods.getName());
viewHolder.tvamount.setText(goods.getAmount()+"");
viewHolder.ivdelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder=new AlertDialog.Builder(getContext());
builder.setTitle("你确定要删除吗?");
builder.setPositiveButton("yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
goodsList.remove(goods);
goodsdao.delete(goods.getId());
notifyDataSetChanged();
}
});
builder.setNegativeButton("cancel",null);
builder.show();

}
});
viewHolder.ivup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
goods.setAmount(goods.getAmount()-1);
goodsdao.update(goods);
notifyDataSetChanged();
}
});
viewHolder.ivdown.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
goods.setAmount(goods.getAmount()+1);
goodsdao.update(goods);
notifyDataSetChanged();
}
});
return view;
}
class ViewHolder{
TextView tvId;
TextView tvname;
TextView tvamount;
ImageView ivup;
ImageView ivdown;
ImageView ivdelete;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐