studentPDY

第十次作业

package cn.itcase.shopping;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.shopping.R;

import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity  implements View.OnClickListener{

    private EditText et_name,et_price,et_number;
    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_main);
        et_name = (EditText)findViewById(R.id.et_name);
        et_price =  (EditText)findViewById(R.id.et_price);
        et_number =  (EditText) findViewById(R.id.et_number);
        listView = (ListView)findViewById(R.id.listView);
        Button add = (Button)findViewById(R.id.add);
        Button query =(Button) findViewById(R.id.query);
        Button update =(Button) findViewById(R.id.update);
        Button delete = (Button)findViewById(R.id.delete);
        add.setOnClickListener(this);
        query.setOnClickListener(this);
        update.setOnClickListener(this);
        delete.setOnClickListener(this);
        myHelper = new MyHelper(this);
    }
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.add:
                db = myHelper.getWritableDatabase();
                name = et_name.getText().toString();
                price = et_price.getText().toString();
                number = et_number.getText().toString();
                ContentValues values = new ContentValues();        //创建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.query:
                Log.e("yanwenhua","123");
                List<CartBean> 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()) {
                        CartBean cartBean = new CartBean();
                        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);
                        cartBean.setName(name);
                        cartBean.setPrice(price);
                        cartBean.setNumber(number);
                        list.add(cartBean);
                    }
                    CartAdapter adapter = new CartAdapter(MainActivity.this,list);
                    listView.setAdapter((ListAdapter) adapter);
                    adapter.notifyDataSetChanged();
                }
                cursor.close();
                db.close();
                break;
            case  R.id.update:
                name = et_name.getText().toString();
                price = et_price.getText().toString();
                number = et_number.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.delete:
                db = myHelper.getWritableDatabase();
                db.delete("cart", null, null);
                List<CartBean> list2 = new ArrayList();
                CartAdapter adapter = new CartAdapter(MainActivity.this,list2);
                listView.setAdapter((ListAdapter) adapter);
                adapter.notifyDataSetChanged();
                db.close();
                Toast.makeText(this, "信息已删除", Toast.LENGTH_SHORT).show();
                break;
        }

    }
}
package cn.itcase.shopping;
import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteOpenHelper;
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) {
    }
}

package cn.itcase.shopping;
class CartBean {
    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 cn.itcase.shopping;
import android.content.Context;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.TextView;

import com.example.shopping.R;

import java.util.List;
class CartAdapter extends BaseAdapter {
    private List<CartBean> list;
    private LayoutInflater layoutInflater;
    public CartAdapter(Context context, List<CartBean> 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.listview_item,null);
            viewHolder=new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }else {
            viewHolder=(ViewHolder) convertView.getTag();
        }
        CartBean cartBean = list.get(position);
        viewHolder.tv_name.setText("商品名称:"+cartBean.getName());
        viewHolder.tv_price.setText("商品价格:"+cartBean.getPrice());
        viewHolder.tv_number.setText("商品数量:"+cartBean.getNumber());
        Log.e("yanwenhua","cartBean.getName()-"+cartBean.getName()+"  "+cartBean.getPrice()+"  "+cartBean.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.tv_name);
            tv_price = (TextView) view.findViewById(R.id.tv_price);
            tv_number = (TextView) view.findViewById(R.id.tv_number);
        }
    }
}
<?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: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/et_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:background="@null"
            android:padding="10dp"
            android:maxLines="1"/>
    </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/et_price"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:background="@null"
            android:padding="10dp"
            android:maxLines="1"/>
    </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/et_number"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:background="@null"
            android:padding="10dp"
            android:maxLines="1"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="10dp">
        <Button
            android:id="@+id/add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="添加"/>
        <Button
            android:id="@+id/query"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="查询"/>
        <Button
            android:id="@+id/update"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="修改"/>
        <Button
            android:id="@+id/delete"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="删除"/>
    </LinearLayout>
    <ListView
        android:id="@+id/listView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ffffff">
    </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"
    android:padding="10dp">
    <TextView
        android:id="@+id/tv_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="15sp"/>
    <TextView
        android:id="@+id/tv_price"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="3dp"
        android:textSize="15sp"/>
    <TextView
        android:id="@+id/tv_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="3dp"
        android:textSize="15sp"/>

</LinearLayout>

 

posted on 2019-11-14 09:19  studentPDY  阅读(...)  评论(...编辑  收藏

My Links

Blog Stats