最后一次安卓作业
<?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"
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
}
}
package com.example.test;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
public class MyProvider extends ContentProvider {
public static UriMatcher um=new UriMatcher(-1);
static{
//添加uri匹配规则
um.addURI("com.gd.db.infodb", "query", 1);
um.addURI("com.gd.db.infodb", "delete", 2);
um.addURI("com.gd.db.infodb", "update", 3);
um.addURI("com.gd.db.infodb", "add", 4);
//content://com.gd.db.infodb/query 查询
//content://com.gd.db.infodb/delete 删除
//content://com.gd.db.infodb/update 修改
//content://com.gd.db.infodb/add 添加
}
@Override
public int delete(Uri arg0, String arg1, String[] arg2) {
// TODO Auto-generated method stub
return 0;
}
@Override
public String getType(Uri arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public Uri insert(Uri arg0, ContentValues arg1) {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean onCreate() {
// TODO Auto-generated method stub
return false;
}
@Override
public Cursor query(Uri uri, String[] a, String b, String[] c,
String d) {
//uri:用户操作的路径
int i=um.match(uri);
if(i==1){
MyHelper helper=new MyHelper(getContext());
SQLiteDatabase db=helper.getWritableDatabase();
return db.query("cart", a, b, c, null,null,d);
}else{
throw new RuntimeException("uri路径错误");
}
}
@Override
public int update(Uri arg0, ContentValues arg1, String arg2, String[] arg3) {
// TODO Auto-generated method stub
return 0;
}
}package com.example.other;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//使用内容提供者去访问数据
ContentResolver resolver=getContentResolver();
//利用解析器去获取数据
Uri uri=Uri.parse("content://com.gd.db.infodb/query");
Cursor cursor=resolver.query(uri, null, null, null, null);
while(cursor.moveToNext()){
int id=cursor.getInt(cursor.getColumnIndex("_id"));
String name=cursor.getString(cursor.getColumnIndex("name"));
System.out.println(id+"-"+name);
}
cursor.close();
}
}
package com.example.test;
import android.content.ContentProvider;import android.content.ContentValues;import android.content.UriMatcher;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.net.Uri;
public class MyProvider extends ContentProvider {public static UriMatcher um=new UriMatcher(-1);static{//添加uri匹配规则um.addURI("com.gd.db.infodb", "query", 1);um.addURI("com.gd.db.infodb", "delete", 2);um.addURI("com.gd.db.infodb", "update", 3);um.addURI("com.gd.db.infodb", "add", 4);//content://com.gd.db.infodb/query 查询//content://com.gd.db.infodb/delete 删除//content://com.gd.db.infodb/update 修改//content://com.gd.db.infodb/add 添加}
@Overridepublic int delete(Uri arg0, String arg1, String[] arg2) {// TODO Auto-generated method stubreturn 0;}
@Overridepublic String getType(Uri arg0) {// TODO Auto-generated method stubreturn null;}
@Overridepublic Uri insert(Uri arg0, ContentValues arg1) {// TODO Auto-generated method stubreturn null;}
@Overridepublic boolean onCreate() {// TODO Auto-generated method stubreturn false;}
@Overridepublic Cursor query(Uri uri, String[] a, String b, String[] c,String d) {//uri:用户操作的路径int i=um.match(uri);if(i==1){MyHelper helper=new MyHelper(getContext());SQLiteDatabase db=helper.getWritableDatabase();return db.query("cart", a, b, c, null,null,d);}else{throw new RuntimeException("uri路径错误");}}
@Overridepublic int update(Uri arg0, ContentValues arg1, String arg2, String[] arg3) {// TODO Auto-generated method stubreturn 0;}
}