其他分享
首页 > 其他分享> > 11.11

11.11

作者:互联网

购物车程序

<?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"
    android:padding="16dp" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="商品名称 :"
            android:textSize="18sp" />

        <EditText
            android:id="@+id/et_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入商品名称"
            android:textSize="16sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="价格:"
            android:textSize="18sp" />

        <EditText
            android:id="@+id/et_price"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="输入价格"
            android:textSize="16sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:layout_marginBottom="10dp" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="数量:"
            android:textSize="18sp" />

        <EditText
            android:id="@+id/et_num"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="输入数量"
            android:textSize="16sp" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/btn_add"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="2dp"
            android:layout_weight="1"
            android:background="#B9B9FF"
            android:onClick="add"
            android:text="添加"
            android:textSize="18sp" />

        <Button
            android:id="@+id/btn_query"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="2dp"
            android:layout_weight="1"
            android:background="#DCB5FF"
            android:onClick="search"
            android:text="查询"
            android:textSize="18sp" />

        <Button
            android:id="@+id/btn_update"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="2dp"
            android:layout_weight="1"
            android:background="#E6CAFF"
            android:onClick="update"
            android:text="修改"
            android:textSize="18sp" />

        <Button
            android:id="@+id/btn_delete"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#ACD6FF"
            android:onClick="delete"
            android:text="删除"
            android:textSize="18sp" />
    </LinearLayout>



        <ListView
            android:id="@+id/tv_show"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="25dp"
            android:textSize="20sp"
            />


</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"
    android:padding="10dp">
    <ImageView
        android:layout_width="100dp"
        android:layout_height="120dp"
        android:src="@drawable/ic_launcher_background"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:orientation="vertical">
        <TextView
            android:id="@+id/tv_id"
            android:layout_marginLeft="20dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="20sp"/>
        <TextView
            android:id="@+id/tv_name"
            android:layout_marginLeft="20dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="20sp"/>
        <TextView
            android:id="@+id/tv_price"
            android:layout_marginLeft="20dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="3dp"
            android:textSize="20sp"/>
        <TextView
            android:id="@+id/tv_num"
            android:layout_marginLeft="20dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="3dp"
            android:textSize="20sp"/>
    </LinearLayout>



</LinearLayout>
package com.example.myapplication;

import android.os.Bundle;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

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

public class MainActivity extends Activity {
    List<Shopping> list  = new ArrayList<Shopping>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void add(View v) {
        StuOpenHelper helper = new StuOpenHelper(this);
        SQLiteDatabase db = helper.getWritableDatabase();
        String name = ((EditText) findViewById(R.id.et_name)).getText()
                .toString();
        int price = Integer.parseInt(((EditText) findViewById(R.id.et_price))
                .getText().toString());
        int num = Integer.parseInt(((EditText) findViewById(R.id.et_num))
                .getText().toString());
        // 原生sql操作,
        // db.execSQL("insert into stuinfo (name,age) values('lisi',22)");
        // db.execSQL("insert into stuinfo (name,age) values(?,?)",new
        // Object[]{"kitty",30});
        db.execSQL("insert into shoppinfo (name,price,num) values(?,?,?)", new Object[] {
                name, price,num });
        Toast.makeText(this, "ok", 0).show();

    }

    public void delete(View view) {
        StuOpenHelper helper = new StuOpenHelper(this);
        SQLiteDatabase db = helper.getWritableDatabase();
        String name = ((EditText) findViewById(R.id.et_name)).getText()
                .toString();
        db.execSQL("delete from shoppinfo where name=?", new Object[] { name });
        Toast.makeText(this, "删除成功", 0).show();

    }

    public void update(View view) {
        StuOpenHelper helper = new StuOpenHelper(this);
        SQLiteDatabase db = helper.getWritableDatabase();
        String name = ((EditText) findViewById(R.id.et_name)).getText()
                .toString();

        int price = Integer.parseInt(((EditText) findViewById(R.id.et_price))
                .getText().toString());
        int num = Integer.parseInt(((EditText) findViewById(R.id.et_num))
                .getText().toString());
        db.execSQL("update shoppinfo set price=?,num=? where name=?", new Object[] {
                price,num,name });
        Toast.makeText(this, "修改成功", 0).show();

    }

    public void search(View view) {
        StuOpenHelper helper = new StuOpenHelper(this);
        SQLiteDatabase db = helper.getWritableDatabase();
//        String s = "";
        Cursor cursor = db.rawQuery("select * from shoppinfo", null);
        if (cursor.getCount() != 0) {
            while (cursor.moveToNext()) {
                list.add(new Shopping(cursor.getInt(0),cursor.getString(1),cursor.getInt(2),cursor.getInt(3)));
            }
        }

        // Toast.makeText(this, s, 0).show();
//        ((TextView) (findViewById(R.id.tv_show))).setText(s);
        ListView li = (ListView) findViewById(R.id.tv_show);
        li.setAdapter(new shopAdapter());
    }
    class  shopAdapter  extends BaseAdapter{


        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int position, View converView, ViewGroup parent) {
            View view = View.inflate(MainActivity.this,R.layout.list_item,null);
            TextView id = (TextView) view.findViewById(R.id.tv_id);
            TextView name = (TextView) view.findViewById(R.id.tv_name);
            TextView price = (TextView) view.findViewById(R.id.tv_price);
            TextView num = (TextView) view.findViewById(R.id.tv_num);
            id.setText(list.get(position).getId()+"   ");
            name.setText(list.get(position).getName()+"   ");
            price.setText(list.get(position).getPrice()+"   ");
            num.setText(list.get(position).getNum()+"   ");
            return view;
        }

        }
    }
package com.example.myapplication;

public class Shopping {
    private  int id;
    private  String name;
    private  int price;
    private  int num;

    public Shopping(){

    }
    public Shopping(int id,String name,int price,int num){
        this.id=id;
        this.name=name;
        this.price=price;
        this.num=num;
    }
    public void setId(int id) {
        this.id = id;
    }

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

    public void setPrice(int price) {
        this.price = price;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public int getNum() {
        return num;
    }

    public String getName() {
        return name;
    }

    public int getPrice() {
        return price;
    }

    public int getId() {
        return id;
    }
}
package com.example.myapplication;

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

public class StuOpenHelper extends SQLiteOpenHelper {

    public StuOpenHelper(Context context) {
        super(context, "shop.db", null, 1);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL("create table shoppinfo(_id integer primary key autoincrement,name varchar(20),price integer,num integer)");

    }

    @Override
    public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
        // TODO Auto-generated method stub

    }

}

 

 

用内容提供者读取信息

<RelativeLayout 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"
    tools:context=".MainActivity"     >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="查询" 
        android:onClick="click"/>
    
</RelativeLayout>
package com.example.vbn;

import java.io.File;
import java.io.FileOutputStream;

import org.xmlpull.v1.XmlSerializer;

import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.database.Cursor;
import android.util.Xml;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    public void click(View v) {
        // 1.获取XmlSerializer的实例
        XmlSerializer serializer = Xml.newSerializer();
        // 2.设置序列化参数
        String pathString = Environment.getExternalStorageDirectory()
                .getPath();
        File file = new File(pathString, "smsbackup.xml");
        try {
            FileOutputStream fos = new FileOutputStream(file);
            serializer.setOutput(fos, "utf-8");
            // 3.写xml文档开头
            serializer.startDocument("utf-8", true);
            // 4.写xml根节点
            serializer.startTag(null, "smss");
            // 5.构造uri
            Uri uri = Uri.parse("content://sms/");
            // 6.通过内容解析者查询
            Cursor cursor = getContentResolver().query(uri,
                    new String[] { "address", "date", "body" }, null, null,
                    null);

            if (cursor != null && cursor.getCount() > 0) {
                while (cursor.moveToNext()) {
                    String addressString = cursor.getString(0);
                    String dateString = cursor.getString(1);
                    String bodyString = cursor.getString(2);
                    
                    //7.写入sms节点
                    serializer.startTag(null, "sms");
                    //8.写入address节点
                    serializer.startTag(null, "address");
                    serializer.text(addressString);
                    serializer.endTag(null, "address");
                    //9.写入date节点
                    serializer.startTag(null, "date");
                    serializer.text(dateString);
                    serializer.endTag(null, "date");
                    //10.写入body节点
                    serializer.startTag(null, "body");
                    serializer.text(bodyString);
                    serializer.endTag(null, "body");
                    
                    serializer.endTag(null, "sms");
                }
            }
            serializer.endTag(null, "smss");
            serializer.endDocument();
            fos.close();

        } catch (Exception e) {

            e.printStackTrace();
        }
    }
}

 

标签:name,int,public,import,android,11.11,id
来源: https://www.cnblogs.com/M1223631418/p/13967129.html