`

sql

 
阅读更多

package com.example.xiaomimall;

import java.util.ArrayList;
import java.util.HashMap;

import com.wuxifu.utils.Constant;
import com.wuxifu.utils.DowmImage;
import com.wuxifu.utils.MySqlite;

import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.view.View.OnCreateContextMenuListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class ShoppingCart extends Activity
{

    private static final int THREE = 3;
    private static final int TWO = 2;
    private static final int ONE = 1;
    private MySqlite mySqlite;
    private SQLiteDatabase db;
    private Cursor cursor;
    ArrayList<String>  urls=new ArrayList<String>();
    private HashMap<Integer, Bitmap> hs=new HashMap<Integer, Bitmap>();
    private SimpleCursorAdapter simpleCursorAdapter;
    private CharSequence product_name;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
       // setContentView(R.layout.activity_shopping_cart);
        mySqlite = new MySqlite(this, Constant.DB_NAME, null, 1);
        db = mySqlite.getWritableDatabase();
       cursor = db.query(Constant.TABLE_NAME, null, null, null, null, null, null);
        boolean succ = cursor.moveToFirst();
       
        if(!succ)
        {
            toFailedView();
        }
        else
        {
        toSucceedView();
        }
    }

    private void toSucceedView()
    {
        setContentView(R.layout.activity_shopping_cart2);
        ListView listView1 = (ListView) findViewById(R.id.listView1);
        simpleCursorAdapter = new SimpleCursorAdapter(this,R.layout.item_shoppingcart, cursor,new String[]{ "bigTitle","price","number"},new int[]{R.id.tv_bigTitle,R.id.tv_price,R.id.tv_num});
        listView1.setAdapter(simpleCursorAdapter);

//长按listview的某一个item就会出现上下文菜单,this就是当前activity监听了该事件,

//由activity显示上下文菜单(1)
        listView1.setOnCreateContextMenuListener(this);

    }

    private void toFailedView()
    {
        setContentView(R.layout.activity_shopping_cart);
        Button tomain = (Button) findViewById(R.id.button1);
        tomain.setOnClickListener(new OnClickListener()
        {
           
            @Override
            public void onClick(View v)
            {
                      startActivity(new Intent(ShoppingCart.this, MainMall.class));    
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_shopping_cart, menu);
        return true;
    }

//建一个上下文菜单(2)
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo)
    {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.add(0, 1, ONE, "增加");
        menu.add(0, 2, TWO, "删除");
        menu.add(0, 3, THREE, "修改");
        //menu.add(0, 4, 4, "");
        Toast.makeText(this, v.getClass()+"", Toast.LENGTH_SHORT).show();
    }
    @Override
    public boolean onContextItemSelected(MenuItem item)
    {

//获取长按了listview的某一item的信息 menuInfo 
        AdapterView.AdapterContextMenuInfo menuInfo = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();

//获取item的.position
       int position= menuInfo.position;

//获取item某一子view的内容menuInfo.targetView.findViewById(R.id.tv_bigTitle);
       TextView text = (TextView)menuInfo.targetView.findViewById(R.id.tv_bigTitle);
       product_name = text.getText();
    Toast.makeText(this, position+" "+product_name, Toast.LENGTH_SHORT).show();
       switch (item.getOrder())
    {
    case ONE:
        add(product_name);
        break;
    case TWO:
        delete(product_name);
        break;
    case THREE:
        update(product_name);
        break;

    default:
        break;
    }
        return super.onContextItemSelected(item);
    }

    private void add(CharSequence product_name)
    {
      showDialog(0, null); 
    }

    private void delete(CharSequence product_name)
    {
      db.delete(Constant.TABLE_NAME, "bigTitle=?", new String[]{product_name+""});
      //重新查数据库,更新listview
      simpleCursorAdapter.changeCursor(getCursor());
      simpleCursorAdapter.notifyDataSetChanged();
    }

    private void update(CharSequence product_name)
    {
        showDialog(0, null);        
    }
    private Cursor getCursor()
    {
        Cursor cursor=db.query(Constant.TABLE_NAME, null, null, null, null, null, null);
        boolean moveToFirst = cursor.moveToFirst();
        return cursor;
    }
    @Override
    protected void onStop()
    {
        //该activity退出后关闭数据库
        db.close();
        super.onStop();
    }
    @Override
    protected Dialog onCreateDialog(int id, Bundle args)
    {
        Dialog dialog = new Dialog(this);
       
        View view=getLayoutInflater().inflate(R.layout.dialog, null);
        Spinner spinner =(Spinner) view.findViewById(R.id.spinner1);
        Button confirm =(Button) view.findViewById(R.id.button1);
        iniSpinner(spinner);
        iniButton( confirm,id);
        dialog.setContentView(view);
        dialog.setTitle("输入你想购买的数量:");
        return dialog;
    }
    private void iniButton(Button confirm, int id)
    {
      confirm.setOnClickListener(new OnClickListener()
    {
       
        @Override
        public void onClick(View v)
        {
         ContentValues values=new ContentValues();
         values.put("number",number );
        db.update(Constant.TABLE_NAME, values, "bigTitle=?", new String[]{product_name+""});
        simpleCursorAdapter.changeCursor(getCursor());
        simpleCursorAdapter.notifyDataSetChanged();
        dismissDialog(0);
        }
    });       
    }
    int number=1;
    private void iniSpinner(Spinner spinner)
    {
            ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                    this, R.array.number, android.R.layout.simple_spinner_item);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinner.setAdapter(adapter);
            spinner.setOnItemSelectedListener(
                    new OnItemSelectedListener() {
                      

                        public void onItemSelected(
                                AdapterView<?> parent, View view, int position, long id) {
                            String[] num = getResources().getStringArray(R.array.number);
                            number=Integer.valueOf(num[position]);
                            showToast("购买 数量"+number);
                        }

                        private void showToast(String string)
                        {
                         Toast.makeText(ShoppingCart.this, string,Toast.LENGTH_LONG).show();                           
                        }

                        public void onNothingSelected(AdapterView<?> parent) {
                            showToast("Spinner1: unselected");
                        }
                    });

           
    }

}

////////////////////////////////////////////////////

package com.wuxifu.utils;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class MySqlite extends SQLiteOpenHelper
{

    public MySqlite(Context context, String name, CursorFactory factory,
            int version)
    {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db)
    {
      String sql="CREATE TABLE IF NOT EXISTS  " +Constant.TABLE_NAME+" (_id INTEGER PRIMARY KEY AUTOINCREMENT " +
        "                        ,url VATCHAR(50),bigTitle VATCHAR(20)" +
        "                        ,subTitle VATCHAR(20),price VATCHAR(20),number INTEGER NOT NULL DEFAULT 1)";
    db.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {
        String sql="CREATE TABLE IF NOT EXISTS  " +Constant.TABLE_NAME+" (_id INTEGER PRIMARY KEY AUTOINCREMENT " +
                "                        ,url VATCHAR(50),bigTitle VATCHAR(20)" +
                "                        ,subTitle VATCHAR(20),price VATCHAR(20),number INTEGER NOT NULL DEFAULT 1)";
        db.execSQL(sql);

    }

}

分享到:
评论

相关推荐

    通过SqlCmd执行超大SQL文件

    ##通过sqlcmd执行sql文件 由于sql文件过大,超过了100M,再数据库的窗口执行,结果超出内存了,对于特别大的sql文件可以使用sqlcmd进行执行 ###1.打开cmd窗口 运行–cmd–进入到sql文件所在的文件夹。 如果是win7可...

    java sql操作工具类 java sql操作工具类

    java sql操作工具类 java sql操作工具类java sql操作工具类 java sql操作工具类java sql操作工具类 java sql操作工具类java sql操作工具类 java sql操作工具类java sql操作工具类 java sql操作工具类java sql操作...

    SQLServer_2000-2008_R2查询智能分析器RedGate_SQL_Prompt_V5.3.4.1_Crack_Keygen破解教程注册机免费

    在我个人编写SQL脚本时,至少会把SQL的格式排列成易于阅读的,因为其他人会阅读到你的SQL,无论是在程序中或是脚本文件中,良好的排版不仅让人看起来赏心悦目,在和他人之间做交流时也省时省力,不会因为揉成一团的...

    SQL优化 SQL优化软件 SQL优化工具

    SQL优化是数据库管理中的关键环节,它涉及到提升查询性能、减少资源消耗以及改善系统整体效率。SQL优化软件和工具能够帮助数据库管理员(DBA)和开发人员找出性能瓶颈,优化查询逻辑,从而提高数据库系统的响应速度...

    SQLPrompt5.3破解

    本人在Windows7 64位+SQL Server 2012环境下测试通过(系统是全新安装) 使用方法: 1,安装SQLPrompt v5.3,这个不多说。 2,安装完毕后,断开网络连接。 3,打开Visual Studio或者SQL Server Management Studio(版本...

    SQLPrompt for SQLServer2016 智能提示插件 SQL2016 提示

    SQLPrompt for SQLServer2016 智能提示插件 SQL2016 提示 SQLPrompt最新版本 绿色版 SQL Prompt 是一款拥有SQL智能提示功能的SQL Server和VS插件。SQL Prompt能根据数据库的对象名称,语法和用户编写的代码片段自动...

    sqlserver自动生成sql语句工具sqlserver转oracle

    在IT行业中,数据库管理系统是核心组成部分,SQL Server和Oracle分别是微软和甲骨文公司推出的两款广泛应用的关系型数据库系统。在企业级应用中,有时需要在不同的数据库系统间进行数据迁移或兼容性处理,这就涉及到...

    sql server 导入超大SQL脚本文件

    SQL Server 导入超大 SQL 脚本文件 SQL Server 是一种关系型数据库管理系统,广泛应用于各种行业。然而,在实际应用中,我们经常会遇到导入超大 SQL 脚本文件的问题。本文将介绍如何使用 osql 工具来导入超大 SQL ...

    Android通过webservice连接Sqlserver实例

    在Android开发中,有时我们需要与远程数据库进行交互,例如SQLServer。这个场景通常是通过Web服务,如WebService来实现。本文将详细介绍如何在Android应用中利用WebService接口连接到SQLServer数据库,实现数据的增...

    SQL SQLPrompt 9 SQL 2016/2017可用

    SQL Prompt是Redgate Software开发的一款高效SQL代码编辑工具,它为SQL Server的开发人员提供了智能提示、格式化、重构和代码分析等功能,极大地提升了编写和维护SQL代码的效率。SQL Prompt 9是该系列的最新版本,...

    SQL 语法 SQL 总结 SQL教程

    SQL 基础 SQL 首页 SQL 简介 SQL 语法 SQL select SQL distinct SQL where SQL AND & OR SQL Order By SQL insert SQL update SQL delete SQL 高级 SQL Top SQL Like SQL 通配符 SQL In SQL Between ...

    SQLMonitor oracle跟踪SQL工具

    《SQLMonitor:Oracle数据库SQL跟踪与分析利器》 在IT行业中,数据库的高效管理与优化是至关重要的。针对Oracle数据库,有一款名为SQLMonitor的工具,它专为跟踪和监控SQL语句而设计,帮助开发者和DBA们找出程序...

    AI自动生成SQL语句的开源代码 sqlcoder-main.zip

    开源的AI自动生成SQL语句源代码,这款SQLCoder-70B-Alpha在文本到SQL的转换能力上超越了包括GPT-4在内的所有通用模型,它能更准确地理解你的需求,并生成相应的SQL查询。SQLCoder2和SQLCoder-7B模型已经向公众开放,...

    sqlserver驱动包 jdbc驱动 sqljdbc.jar和sqljdbc4.jar

    SQL Server驱动包是用于Java应用程序通过JDBC(Java Database Connectivity)接口与Microsoft SQL Server数据库进行交互的必备组件。本文将详细介绍这两个重要的驱动文件——sqljdbc.jar和sqljdbc4.jar,以及如何...

    sqlserver驱动包:sqljdbc4.jar

    SQL Server驱动包`sqljdbc4.jar`是微软官方提供的Java数据库连接器(JDBC),用于在Java应用程序中与Microsoft SQL Server进行通信。JDBC是Java编程语言中的一个标准API,它使得开发人员能够以标准化的方式访问各种...

    sqlservr32和sqlservr64.zip

    标题中的"sqlservr32和sqlservr64.zip"指的是SQL Server 2005服务中的两个关键组件,`sqlservr32.exe`和`sqlservr64.exe`。这两个文件是SQL Server服务的核心执行文件,分别对应于32位和64位操作系统。在Windows 8和...

    SQLTracker,抓取sql语句的工具

    SQLTracker是一款专为数据库操作监控设计的工具,它在IT领域中主要用于跟踪和记录SQL语句的执行情况。SQL(Structured Query Language)是用于管理关系数据库的编程语言,包括查询、更新、插入和删除数据等操作。SQL...

    oracle sqldeveloper连接mysql、SQLServer第三方dll

    解决oracle sqldeveloper无法连接mysql、SQLServer问题,sqlDeveloper是ORACLE数据库开发工具,自带的是无法连接MS SQL Server以及mysql的,想连接的话需要第三方工具。 使用方法: 解压出来后将2个jar放入jlib...

    Oracle Sql语句转换成Mysql Sql语句

    在数据库管理领域,Oracle SQL和MySQL SQL是两种广泛使用的SQL方言,它们在语法和功能上存在一定的差异。当需要将一个基于Oracle SQL的应用程序迁移到MySQL环境时,就需要进行SQL语句的转换工作。本项目提供了一个...

    sql server2019安装包

    SQL Server 2019是Microsoft推出的一款关系型数据库管理系统,是SQL Server系列中的一个重要版本。它提供了强大的数据存储、处理和分析能力,广泛应用于企业级数据库应用开发和数据分析。在本安装包中,主要包含的是...

Global site tag (gtag.js) - Google Analytics