package com.example.demo01;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
private EditText etName, etPrice, etNumber;
private ListView listView;
private String name, price, number;
private MyHelper myHelper;
private SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main1111);
myHelper = new MyHelper(this);
init();
}
public void click(View v) {
ContentValues values; // 创建ContentValues对象
switch (v.getId()) {
case R.id.button1:
name = etPrice.getText().toString();
price = etName.getText().toString();
number = etNumber.getText().toString();
db = myHelper.getWritableDatabase();
values = new ContentValues();
values.put("name", name); // 将数据添加到ContentValues对象
values.put("price", price);
values.put("number", number);
db.insert("cart", null, values);
db.close();
Toast.makeText(this, "信息已添加", Toast.LENGTH_SHORT).show();
break;
case R.id.button2:
Log.e("yanwenhua", "123");
List<Commodity> list = new ArrayList();
db = myHelper.getWritableDatabase();
Cursor cursor = db
.query("cart", null, null, null, null, null, null);
if (cursor.getCount() == 0) {
Toast.makeText(this, "没有数据", Toast.LENGTH_SHORT).show();
} else {
while (cursor.moveToNext()) {
Commodity commodity = new Commodity();
int nameIndex = cursor.getColumnIndex("name");
int priceIndex = cursor.getColumnIndex("price");
int numberIndex = cursor.getColumnIndex("number");
String name = cursor.getString(nameIndex);
String price = cursor.getString(priceIndex);
String number = cursor.getString(numberIndex);
Log.e("yanwenhua",
"cursor.getCount();--" + cursor.getCount()
+ "name-" + name + " " + price + " "
+ number);
commodity.setName(name);
commodity.setPrice(price);
commodity.setNumber(number);
list.add(commodity);
}
MyBaseAdapter adapter = new MyBaseAdapter(MainActivity.this,
list);
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
cursor.close();
db.close();
break;
case R.id.button3:
name = etPrice.getText().toString();
price = etName.getText().toString();
number = etNumber.getText().toString();
db = myHelper.getWritableDatabase();
values = new ContentValues(); // 要修改的数据
values.put("number",number);
values.put("price",price);
db.update("cart", values, "name=?",
new String[]{name}); // 更新并得到行数
db.close();
Toast.makeText(this, "信息已修改", Toast.LENGTH_SHORT).show();
break;
case R.id.button4:
db = myHelper.getWritableDatabase();
db.delete("cart", null, null);
List<Commodity> list2 = new ArrayList();
MyBaseAdapter adapter = new MyBaseAdapter(MainActivity.this,list2);
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
db.close();
Toast.makeText(this, "信息已删除", Toast.LENGTH_SHORT).show();
break;
}
}
private void init() {
setContentView(R.layout.activity_main1111);
etName = (EditText) findViewById(R.id.editText2);
etPrice = (EditText) findViewById(R.id.editText1);
etNumber = (EditText) findViewById(R.id.editText3);
listView = (ListView) findViewById(R.id.listView);
Button add = (Button) findViewById(R.id.button1);
Button query = (Button) findViewById(R.id.button2);
Button update = (Button) findViewById(R.id.button3);
Button delete = (Button) findViewById(R.id.button4);
}
class MyBaseAdapter extends BaseAdapter {
private List<Commodity> list;
private LayoutInflater layoutInflater;
public MyBaseAdapter(Context context, List<Commodity> list){
this.layoutInflater = LayoutInflater.from(context);
this.list = list;
}
@Override
public int getCount() {
Log.e("yanwenhua","list.size()--"+list.size());
return list.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView==null){
convertView=layoutInflater.inflate(R.layout.list_item,null);
viewHolder=new ViewHolder(convertView);
convertView.setTag(viewHolder);
}else {
viewHolder=(ViewHolder) convertView.getTag();
}
Commodity commodity = list.get(position);
viewHolder.tv_name.setText("商品名称:"+commodity.getName());
viewHolder.tv_price.setText("商品价格:"+commodity.getPrice());
viewHolder.tv_number.setText("商品数量:"+commodity.getNumber());
Log.e("yanwenhua","cartBean.getName()-"+commodity.getName()+" "+commodity.getPrice()+" "+commodity.getNumber());
return convertView;
}
class ViewHolder{
TextView tv_name;
TextView tv_price;
TextView tv_number;
public ViewHolder(View view){
tv_name = (TextView) view.findViewById(R.id.textView1);
tv_price = (TextView) view.findViewById(R.id.textView2);
tv_number = (TextView) view.findViewById(R.id.textView3);
}
}
}
}
package com.example.demo01;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class Commodity {
private String name;
private String price;
private String number;
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 getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
}
package com.example.demo01;
import android.support.v7.app.ActionBarActivity;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MyHelper extends SQLiteOpenHelper {
public MyHelper(Context context) {
super(context, "shoppingcart.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE cart(_id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(20), price VARCHAR(20), number VARCHAR(20))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#E6E6E6"
android:orientation="vertical"
android:padding="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:background="@android:color/white"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="名称:"
android:textColor="#000"
android:textSize="20sp" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:background="@null"
android:maxLines="1"
android:padding="10dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:background="@android:color/white"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="价格:"
android:textColor="#000"
android:textSize="20sp" />
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:background="@null"
android:maxLines="1"
android:padding="10dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@android:color/white"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="数量:"
android:textColor="#000"
android:textSize="20sp" />
<EditText
android:id="@+id/editText3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:background="@null"
android:maxLines="1"
android:padding="10dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click"
android:text="添加" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click"
android:text="查询" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click"
android:text="修改" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click"
android:text="删除" />
</LinearLayout>
<ListView
android:id="@+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffffff" >
</ListView>
</LinearLayout>
<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="10dp">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:textSize="15sp"/>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:textSize="15sp"/>
</LinearLayout>