LinearLayout ="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.and_安卓做一个英文水果商城">
当前位置:   article > 正文

android商品展示案例FruitStore_安卓做一个英文水果商城

安卓做一个英文水果商城

1、运行效果图

 

二。实现过程


1.store_main1.xml

 


<?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:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
     android:id="@+id/addLL"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_margin="8dp"
        android:layout_height="match_parent">
        <EditText
            android:id="@+id/nameET"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="商品名称"
            android:inputType="textPersonName"
            />
        <EditText
            android:id="@+id/balanceET"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="金额"
            android:inputType="number"
            />
        <ImageView
            android:onClick="add"
            android:id="@+id/addIV"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
           android:src="@android:drawable/ic_input_add"
            />
    </LinearLayout>
    <RelativeLayout
        android:layout_below="@id/addLL"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <ListView
            android:id="@+id/accountLV"

            android:layout_width="match_parent"
            android:layout_height="match_parent"
            />

    </RelativeLayout>
</LinearLayout>

2.ListView布局文件:item.xml

 

<?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"
    android:padding="10dp">
<TextView
    android:id="@+id/idTV"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="14"
    android:textColor="#000000"
    android:textSize="30dp"
    />
    <TextView
        android:id="@+id/nameTV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:singleLine="true"
        android:text="PQ"

        android:textColor="#000000"
        android:textSize="30dp"
        />
    <TextView
       android:id="@+id/balanceTV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:singleLine="true"
        android:textSize="30dp"
        android:text="123456"
        android:textColor="#000000"
        />
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <ImageView
         android:id="@+id/upIV"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="2dp"
            android:src="@android:drawable/arrow_up_float"
            />
        <ImageView
            android:id="@+id/downIV"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/arrow_down_float"
            />
    </LinearLayout>
    <ImageView
        android:id="@+id/deleteIV"
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:src="@android:drawable/ic_menu_delete"
        />
</LinearLayout>

3.水果对象类Account.java

package com.example.bz0209.fruitstore.bean;

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

public class Account {
    private Long id;
    private String name;
    private Integer balance;

    public Integer getBalance() {
        return balance;
    }

    public void setBalance(Integer balance) {
        this.balance = balance;
    }

    public String getName() {
        return name;
    }

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

    public Long getId() {
        return id;
    }

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

    public Account(Long id, String name, Integer balance) {
        super();
        this.id = id;
        this.name = name;
        this.balance = balance;
    }

    public Account(String name, Integer balance) {
        super();
        this.name = name;
        this.balance = balance;
    }

    public Account() {
        super();
    }

    public String toString() {
        return "[商品号:" + id + ",商品名称:" + name + ",余额:" + balance + "]";
    }
}

4.创建数据操作逻辑类

package com.example.bz0209.fruitstore.dao;

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

import com.example.bz0209.fruitstore.bean.Account;

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

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

public class AccountDao {
    private MyHelper helper;


    public AccountDao(Context context){
        helper=new MyHelper(context);
    }
    public void insert(Account account){
        SQLiteDatabase database=helper.getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put("name",account.getName());
        values.put("balance",account.getBalance());
        long id=database.insert("account",null,values);
        account.setId(id);
        database.close();
    }
    public int delete(long id){
        SQLiteDatabase database=helper.getWritableDatabase();
        int count=database.delete("account","_id=?",new String[] {id+""});
        database.close();
        return count;
    }
    public int update(Account account){
        SQLiteDatabase database=helper.getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put("name",account.getName());
        values.put("balance",account.getBalance());
        int count=database.update("account",values,"_id=?",new String[] {account.getId()+""});
        database.close();
        return count;
    }
    public List<Account>queryAll(){
        SQLiteDatabase database=helper.getReadableDatabase();
        Cursor cursor=database.query("account",null,null,null,null,null,"balance DESE");
        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();
        database.close();
        return list;
    }
}

5.界面交互逻辑类

package com.example.bz0209.fruitstore;

import android.app.Dialog;
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.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.fruitstore.bean.Account;
import com.example.bz0209.fruitstore.dao.AccountDao;

import java.util.List;

public class MainActivity extends AppCompatActivity {
    private List<Account> list;
    private AccountDao accountDao;
    private EditText nameET;
    private EditText balanceET;
    private ListView accountLV;
    MyAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.store_main1);
        accountDao = new AccountDao(this);
        list = accountDao.queryAll();
        adapter = new MyAdapter();
        accountLV.setAdapter(adapter);
    }

    private void initView() {
        accountLV = (ListView) findViewById(R.id.accountLV);
        nameET = (EditText) findViewById(R.id.nameET);
        balanceET = (EditText) findViewById(R.id.balanceET);
        accountLV.setOnItemClickListener(new MyOnItemClickLister());
    }

    public void add(View view) {
        String name = nameET.getText().toString().trim();
        String balance = balanceET.getText().toString().trim();
        Account a = new Account(name, balance, equals("") ? 0 : Integer.parseInt(balance));
        accountDao.insert(a);
        list.add(a);
        adapter.notifyDataSetChanged();
        accountLV.setSelection(accountLV.getCount() - 1);
        nameET.setText("");
        balanceET.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;
        }

        @Override
        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.idTV);
            TextView nameTV = (TextView) item.findViewById(R.id.nameTV);
            TextView balanceTV = (TextView) item.findViewById(R.id.balanceTV);
            final Account a = list.get(position);
            idTV.setText(a.getId() + "");
            nameTV.setText(a.getName() + "");
            balanceTV.setText(a.getBalance() + "");
            ImageView upIV = (ImageView) findViewById(R.id.upIV);
            ImageView downIV = (ImageView) item.findViewById(R.id.downIV);
            ImageView deleteIV = (ImageView) item.findViewById(R.id.deleteIV);
            upIV.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    a.setBalance(a.getBalance() + 1);
                    notifyDataSetChanged();
                    accountDao.update(a);
                }
            });
            downIV.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    a.setBalance(a.getBalance() - 1);
                    notifyDataSetChanged();
                    accountDao.update(a);
                }
            });
            deleteIV.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            list.remove(a);
                            accountDao.delete(a.getId());
                            notifyDataSetChanged();
                        }

                    };
                    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                    builder.setTitle("确定要删除吗?");
                    builder.setPositiveButton("确定", listener);
                    builder.setNegativeButton("取消", null);
                    builder.show();
                }
            });


            return item;
        }
    }

    private class MyOnItemClickLister implements android.widget.AdapterView.OnItemClickListener {
        @SuppressWarnings("deprecation")
        @Override
        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版本不兼容造成的,需要CTS测试可参考博客

http://blog.csdn.net/jianguo_liao19840726/article/details/7222814

(小)


 

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号