• 第十次作业


    <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"
        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"
        android:background="#e6e6e6"
        tools:context="com.example.shopping.MainActivity" >
       <LinearLayout 
           android:layout_width="match_parent"
           android:layout_height="30dp"
           android:orientation="horizontal"
           android:background="#ffffff"
           android:layout_marginTop="10dp">
           <TextView 
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="名称:"
               android:textSize="20sp"
               android:layout_gravity="center"
              />  
            <EditText 
                android:id="@+id/ed_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="20sp"
                android:layout_gravity="center"
                android:background="#ffffff"/>    
       </LinearLayout>
       <LinearLayout 
           android:layout_width="match_parent"
           android:layout_height="30dp"
           android:orientation="horizontal"
           android:background="#ffffff"
           android:layout_marginTop="10dp">
           <TextView 
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="价格:"
               android:textSize="20sp"
               android:layout_gravity="center"
              />  
            <EditText 
                android:id="@+id/ed_price"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="20sp"
                android:layout_gravity="center"
                android:background="#ffffff"/>    
       </LinearLayout>
       <LinearLayout 
           android:layout_width="match_parent"
           android:layout_height="30dp"
           android:orientation="horizontal"
           android:background="#ffffff"
           android:layout_marginTop="10dp">
           <TextView 
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="数量:"
               android:textSize="20sp"
               android:layout_gravity="center"
              />  
            <EditText 
                android:id="@+id/ed_amount"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="20sp"
                android:layout_gravity="center"
                android:background="#ffffff"/>    
       </LinearLayout>
       <LinearLayout 
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:orientation="horizontal"
           android:layout_marginTop="10dp"
           >
           <Button 
               android:id="@+id/bu_add"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="增加"
               android:layout_marginLeft="15dp"/>
           <Button 
               android:id="@+id/bu_delete"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="删除"/>
           <Button 
               android:id="@+id/bu_updata"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="修改"/>
           <Button 
               android:id="@+id/bu_query"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="查询"/>
       </LinearLayout>
       <ListView 
           android:id="@+id/lv"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           ></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="match_parent"
        android:orientation="vertical" >
        <LinearLayout 
           android:layout_width="match_parent"
           android:layout_height="30dp"
           android:orientation="horizontal"
           android:background="#ffffff"
           android:layout_marginTop="10dp">
           <TextView 
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="名称:"
               android:textSize="20sp"
               android:layout_gravity="center"
              />  
            <TextView 
                android:id="@+id/tv_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="20sp"
                android:layout_gravity="center"
                android:background="#ffffff"/>    
       </LinearLayout>
       <LinearLayout 
           android:layout_width="match_parent"
           android:layout_height="30dp"
           android:orientation="horizontal"
           android:background="#ffffff"
           android:layout_marginTop="10dp">
           <TextView 
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="价格:"
               android:textSize="20sp"
               android:layout_gravity="center"
              />  
            <TextView 
                android:id="@+id/tv_price"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="20sp"
                android:layout_gravity="center"
                android:background="#ffffff"/>    
       </LinearLayout>
       <LinearLayout 
           android:layout_width="match_parent"
           android:layout_height="30dp"
           android:orientation="horizontal"
           android:background="#ffffff"
           android:layout_marginTop="10dp">
           <TextView 
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="数量:"
               android:textSize="20sp"
               android:layout_gravity="center"
              />  
            <TextView 
                android:id="@+id/tv_amount"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="20sp"
                android:layout_gravity="center"
                android:background="#ffffff"/>    
       </LinearLayout>
    
    </LinearLayout>
    package com.example.shopping;
    
    import java.util.ArrayList;
    
    import com.example.adapter.shopAdapter;
    import com.example.bean.shopBean;
    
    import Database.SQLiteHelpe;
    import android.app.Activity;
    import android.content.Context;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.ListView;
    import android.widget.Toast;
    
    public class MainActivity extends Activity implements View.OnClickListener {
         
        private static final Context Context = null;
        private EditText mName;
        private EditText mPrice;
        private EditText mAmount;
        private ArrayList<shopBean> list;
        ListView mLv;
        private SQLiteHelpe mSQLiteHelpe = new SQLiteHelpe(this);
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            init();
        }
        public void init() {
            mName = (EditText)findViewById(R.id.ed_name);
            mPrice = (EditText)findViewById(R.id.ed_price);
            mAmount = (EditText)findViewById(R.id.ed_amount);
            Button add = (Button) findViewById(R.id.bu_add);
            Button delect = (Button) findViewById(R.id.bu_delete);
            Button updata = (Button) findViewById(R.id.bu_updata);
            Button query = (Button) findViewById(R.id.bu_query);
            add.setOnClickListener(this);
            delect.setOnClickListener(this);
            updata.setOnClickListener(this);
            query.setOnClickListener(this);
            mLv = (ListView) findViewById(R.id.lv);
            list = mSQLiteHelpe.Query();
            shopAdapter adapter = new shopAdapter(list);
            mLv.setAdapter(adapter);
            
            
        }
        @Override
        public void onClick(View v) {
            String name = mName.getText().toString();
            String price = mPrice.getText().toString();
            String amounts = mAmount.getText().toString();
            
            switch (v.getId()) {
            case R.id.bu_add:
                if(mSQLiteHelpe.Insert(name, price, amounts)){
                    Toast.makeText(this, "添加成功", 0).show();
                    list = mSQLiteHelpe.Query();
                    shopAdapter adapter = new shopAdapter(list);
                    mLv.setAdapter(adapter);
                }else {
                    Toast.makeText(this, "添加失败", 0).show();
                }
                break;
            case R.id.bu_delete:
                if(mSQLiteHelpe.Delect(name)) {
                    Toast.makeText(this, "删除成功", 0).show();
                    list = mSQLiteHelpe.Query();
                    shopAdapter adapter = new shopAdapter(list);
                    mLv.setAdapter(adapter);
                }else {
                    Toast.makeText(this, "删除失败", 0).show();
                }
                break;
            case R.id.bu_updata:
                if(mSQLiteHelpe.Upadta(name, price, amounts)) {
                    Toast.makeText(this, "修改成功", 0).show();
                    list = mSQLiteHelpe.Query();
                    shopAdapter adapter = new shopAdapter(list);
                    mLv.setAdapter(adapter);
                }else {
                    Toast.makeText(this, "修改失败", 0).show();
                }
                break;
            case R.id.bu_query:
                list = mSQLiteHelpe.Query();
                shopAdapter adapter = new shopAdapter(list);
                mLv.setAdapter(adapter);
                break;
            }
        }
    }
    package com.example.utils;
    
    import android.content.Context;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteDatabase.CursorFactory;
    import android.database.sqlite.SQLiteOpenHelper;
    import android.provider.BaseColumns;
    
    public class DBUtils extends SQLiteOpenHelper implements BaseColumns {
    
        private String DATABASE_NAME="Shopping";
        public static final String DATABASE_TABLE="Shop";
        public static final int  DATABASE_VERSION=1;
    //    数据库中的列名
        public static final String SHOP_NAME="name";
        public static final String SHOP_PRICE="price";
        public static final String SHOP_AMOUNTS="amounts";
        public DBUtils(Context context) {
            super(context, DATABASE_TABLE, null, DATABASE_VERSION);
        }
    
        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL("create table "+DATABASE_TABLE+""
                    + "("+_ID+" integer primary Key autoincrement, "+SHOP_NAME+" text,"+SHOP_PRICE+" text,"+SHOP_AMOUNTS+" text)");
             
        }
    
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub
    
        }
    
    }
    package Database;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import com.example.bean.shopBean;
    import com.example.utils.DBUtils;
    
    import android.content.ContentValues;
    import android.content.Context;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.util.Log;
    
    
    public class SQLiteHelpe {
    
        private static DBUtils mDBUtils;
        
        public SQLiteHelpe(Context c) {
            mDBUtils = new DBUtils(c);
        }
    //    保存
        static public boolean Insert (String name,String price,String amounts) {
            SQLiteDatabase  db =mDBUtils.getWritableDatabase();
            ContentValues values  = new ContentValues();
            values.put(DBUtils.SHOP_NAME, name);
            values.put(DBUtils.SHOP_PRICE, price);
            values.put(DBUtils.SHOP_AMOUNTS, amounts);
            long Insertid =db.insert(DBUtils.DATABASE_TABLE, null, values);
            return Insertid!=0;
        }
    //    删除
        static public boolean Delect (String name) {
            SQLiteDatabase  db =mDBUtils.getWritableDatabase();
            int DeleteId =db.delete(DBUtils.DATABASE_TABLE, DBUtils.SHOP_NAME+"=?", new String[] {name});
            return DeleteId!=0;
        }
    //    修改
        static public boolean Upadta(String name,String price ,String amounts) {
            SQLiteDatabase  db =mDBUtils.getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put("price", price);
            values.put("amounts", amounts);
            int UpdataId =db.update(DBUtils.DATABASE_TABLE, values , DBUtils.SHOP_NAME+"=?", new String[] {name});
            return UpdataId!=0;
        }
    //    查询
        static public ArrayList<shopBean> Query() {
            ArrayList<shopBean> list = new ArrayList<shopBean>();
            SQLiteDatabase  db =mDBUtils.getReadableDatabase();
            Cursor cursor =db.query(DBUtils.DATABASE_TABLE, null, null, null, null, null, null);
            if(cursor!=null) {
                while (cursor.moveToNext()) {
                    shopBean bean = new shopBean();
                     String id =cursor.getString(cursor.getColumnIndex(DBUtils._ID));
                     String name = cursor.getString(cursor.getColumnIndex(DBUtils.SHOP_NAME));
                     String price =cursor.getString(cursor.getColumnIndex(DBUtils.SHOP_PRICE));
                     String amounts =cursor.getString(cursor.getColumnIndex(DBUtils.SHOP_AMOUNTS));
                     bean.setId(id);
                     bean.setName(name);
                     bean.setPrice(price);
                     bean.setAmounts(amounts);
                     list.add(bean);
                }
                cursor.close();
            }
            return list;
            
        }
        
    }
    package com.example.bean;
    
    public class shopBean {
        private String id;
        
        private String name;
        private String price;
        private String amounts;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getPrice() {
            return price;
        }
        public void setPrice(String price) {
            this.price = price;
        }
        public String getAmounts() {
            return amounts;
        }
        public void setAmounts(String amounts) {
            this.amounts = amounts;
        }
        public String getId() {
            return id;
        }
        public void setId(String id) {
            this.id = id;
        }
        
    }
    package com.example.adapter;
    
    import java.util.ArrayList;
    
    import com.example.bean.shopBean;
    import com.example.shopping.R;
    
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.TextView;
    
    
    public class shopAdapter extends BaseAdapter {
    
        private ArrayList<shopBean> mList;
        public shopAdapter(ArrayList<shopBean> list) {
            // TODO Auto-generated constructor stub
            mList = list;
        }
    
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return mList!=null?mList.size():0;
        }
    
        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return null;
        }
    
        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }
        class ViewHoder{
            TextView name;
            TextView price;
            TextView amounts;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHoder hoder =null;
            if(convertView==null) {
                convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_main, null);
                hoder = new ViewHoder();
                hoder.name = (TextView) convertView.findViewById(R.id.tv_name);
                hoder.price = (TextView) convertView.findViewById(R.id.tv_price);
                hoder.amounts = (TextView) convertView.findViewById(R.id.tv_amount);
                convertView.setTag(hoder);
            }else {
                hoder = (ViewHoder) convertView.getTag();
            }
            hoder.name.setText(mList.get(position).getName());
            hoder.price.setText(mList.get(position).getPrice());
            hoder.amounts.setText(mList.get(position).getAmounts());;
    
            return convertView;
        }
    
    }
  • 相关阅读:
    S3C6410移植uboot2013.01
    linux设备驱动中的并发控制
    明年我多大?(20060119 16:38:41)(新浪)
    冲动&当机立断(20060119 16:58:32)(新浪)
    不能老是雜感,老婆說(20060901 13:14:50)(新浪)
    最近比较烦(20061014 13:14:48)(新浪)
    结婚(20060221 16:31:33)(新浪)
    坐井观天的蛙(20060913 14:19:51)(新浪)
    酒喝大了(20060105 18:41:55)(新浪)
    不可越俎代庖(20060211 21:24:49)(新浪)
  • 原文地址:https://www.cnblogs.com/zqxxx/p/11843778.html
Copyright © 2020-2023  润新知