android商品展示
来源:互联网 发布:ztree check源码 编辑:程序博客网 时间:2024/05/16 11:14
在实际开发中,可能基础的界面都会做,但是让它实行并且运行一些功能的操作,想必还是有点懵,比如说,一些数据库的存储,操作数据库中的增删改查。例如,开发一个购物车,需要将购物车中的商品以列表的形式展示,并且还需要对购物车中的商品进行增、删、改、查操作。要实现这些功能就需要使用ListView和SQLite数据库。接下来通过一个“”商品展示“”案例结合ListView下
如图可以看出 前期工作我们需要activity— main.xml,ListView,Account,AccountDao,MyHelper等布局以及类操作
一.创建程序
首先创建一个名为“商品展示”的应用程序,设计用户交互界面,如图所示
“商品展示”程序的代码如下所示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="bzu.edhu.cn.product.MainActivity">
<LinearLayout
android:id="@+id/addLL"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/et1"
android:hint="商品名称"
android:inputType="textPersonName"
/>
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/et2"
android:hint="金额"
android:inputType="number"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="add"
android:id="@+id/addIV"
android:src="@android:drawable/ic_input_add"
/>
</LinearLayout>
<ListView
android:id="@+id/accountLV"
android:layout_below="@id/addLL"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</ListView
>
</LinearLayout>
2.创建一个ListView Item布局
此布局文件主要对Item进行布局调整,代码如下:
<?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="match_parent"><TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1" android:textColor="#000000" android:layout_weight="1" android:textSize="20sp" android:id="@+id/t1"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="耳机" android:textColor="#000000" android:layout_weight="2" android:textSize="20sp" android:id="@+id/t2"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="20" android:textColor="#000000" android:layout_weight="2" android:textSize="20sp" android:id="@+id/t3"/> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/addup" android:src="@android:drawable/arrow_up_float" android:layout_marginBottom="2dp"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/adddown" android:src="@android:drawable/arrow_down_float" android:layout_marginBottom="2dp"/> </LinearLayout> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/delete" android:src="@android:drawable/ic_menu_delete"/></LinearLayout>
3.创建数据库public class MyHelpr extends SQLiteOpenHelper { public MyHelpr(Context context) { super(context,"itcast.db",null,2); } @Override public void onCreate(SQLiteDatabase db) { System.out.println("onCreate"); db.execSQL("CREATE TABLE account(_id INTEGER PRIMARY KEY AUTOINCREMENT,name VARCHAR(20),balance INTEGER)"); } @Override public void onUpgrade(SQLiteDatabase db, int i, int i1) { System.out.println("onUpgrade"); }}
4.创建Accounty类
public class Account { private Long id ; private String name ; private Integer balamce; 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 getBalamce() { return balamce; } public void setBalamce(Integer balamce) { this.balamce = balamce; } public Account(Long id, String name, Integer balamce) { super(); this.id = id; this.name = name; this.balamce = balamce; } public Account(String name, Integer balamce) { super(); this.name = name; this.balamce = balamce; } @Override public String toString() { return "{" + "序号=" + id + ", 商品名称='" + name + '\'' + ", 余额=" + balamce + '}'; }}5.创建数据库操作类
package com.example.bz0209.shangpin.cn.itcast.product.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.Collection;import java.util.List;/** * Created by Administrator on 2017/4/17. */public class AccountDao {//数据库操作类 private MyHelpr helper; public AccountDao(Context context) { helper=new MyHelpr(context); } public void insert(Account account){ SQLiteDatabase DB=helper.getWritableDatabase(); ContentValues values=new ContentValues(); values.put("name",account.getName()); values.put("balance",account.getBalamce()); 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 values=new ContentValues(); values.put("name",account.getName()); values.put("balance",account.getBalamce()); int count=db.update("account",values,"_id=?",new String[]{account.getId()+""}); db.close(); return count; } public List<Account>queryAll(){ SQLiteDatabase db=helper.getWritableDatabase(); Cursor cursor=db.query("account",null,null,null,null,null,"balance DESC"); List<Account> list=new ArrayList<Account>(); while (cursor.moveToNext()){ //可以根据列名获取索引 long id=cursor.getLong(cursor.getColumnIndex("id")); String name=cursor.getString(1); int balance=cursor.getInt(2); list.add(new Account(id,name,balance)); } cursor.close(); db.close(); return list; }}6.编写界面交互代码package com.example.bz0209.shangpin;import android.app.Activity;import android.content.DialogInterface;import android.support.v7.app.AlertDialog;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.view.ViewGroup;import android.widget.Adapter;import android.widget.AdapterView;import android.widget.BaseAdapter;import android.widget.EditText;import android.widget.ImageView;import android.widget.ListAdapter;import android.widget.ListView;import android.widget.TextView;import android.widget.Toast;import com.example.bz0209.shangpin.cn.itcast.product.dao.Account;import com.example.bz0209.shangpin.cn.itcast.product.dao.AccountDao;import java.util.List;public class MainActivity extends AppCompatActivity { private List<Account> list; private AccountDao dao; private EditText nameET; private EditText balabceET; private MyAdapter adapter; private ListView accountLV; @Override 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((ListAdapter) adapter); } private void initView(){ accountLV=(ListView) findViewById(R.id.accountLV); nameET=(EditText) findViewById(R.id.ETName); balabceET=(EditText) findViewById(R.id.ETBalance); accountLV.setOnItemClickListener(new MyOnItemClickListener()); } public void add(View v){ String name=nameET.getText().toString().trim(); String balance=balabceET.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(""); balabceET.setText(""); } 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; } //获取一个条目视图 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.textView1); TextView nameTV=(TextView) findViewById(R.id.textView2); TextView balanceTV=(TextView) findViewById(R.id.textView3); final Account a=list.get(position); idTV.setText(a.getId()+""); nameTV.setText(a.getName()); balanceTV.setText(a.getBalamce()+""); ImageView upIV=(ImageView)item.findViewById(R.id.imageView1); ImageView downTV=(ImageView)item.findViewById(R.id.imageView2); ImageView deleteTV=(ImageView)item.findViewById(R.id.imageView); upIV.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { a.setBalamce(a.getBalamce()+1); notifyDataSetChanged(); dao.update(a); } }); downTV.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { a.setBalamce(a.getBalamce()-1); notifyDataSetChanged(); dao.update(a); } }); deleteTV.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; } } //ListView 的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(); } }}运行结果如下:
- Android-商品展示
- Android商品展示实验
- Android案例-商品展示
- Android商品展示案例
- Android案例-商品展示
- android商品展示
- Android案例:商品展示
- android商品展示
- Android 案例:商品展示
- Android-商品展示案例
- Android商品展示小案例
- android商品展示案例FruitStore
- android SQLite案例(商品展示)
- Android案例之商品展示
- Android中商品展示案例
- Android中商品展示案例
- android中商品展示案例
- 商品展示
- 第二篇
- 找重复:拈花微笑,飞叶伤人
- 题目1442:A sequence of numbers 九度OJ
- Java开发基础——Maven入门
- 非系统表空间损坏,rman备份恢复
- android商品展示
- 【IMWeb训练营团队作业】模拟微信朋友圈
- JavaScript 实现图片横向自动滚动
- 软件磁盘阵列RAID的设置
- TPshop商城怎么样添加插件
- hadoop-2.7+hive-2.1.1+mysql 集群配置
- 【NumPy基础】100道numpy练习——初学与入门篇
- JS-箭头函数中的this的指向
- Algolia Search