作业11(增删改查listview)

package com.example.listview;






import java.util.ArrayList;

import java.util.List;

import com.example.listview.MyHelper;

import com.example.listview.R;
import com.example.listview.CartAdapter;
import com.example.listview.CartBean;
import com.example.listview.MainActivity;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity implements View.OnClickListener {

private EditText et1,et2,et3;
private ListView listView1;
private Button button1,button2,button3,button4;
private SQLiteDatabase db;
private String name,price,number;
private  MyHelper myHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et1 = (EditText)findViewById(R.id.et1);
        et2 =  (EditText)findViewById(R.id.et2);
        et3 =  (EditText) findViewById(R.id.et3);
        listView1 = (ListView)findViewById(R.id.listView1);
        Button button1 = (Button)findViewById(R.id.button1);
        Button button2 =(Button) findViewById(R.id.button2);
        Button button3 =(Button) findViewById(R.id.button3);
        Button button4 = (Button)findViewById(R.id.button4);
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
        button4.setOnClickListener(this);
        myHelper = new MyHelper(this);
    }

    @Override
    public void onClick(View v) {
        
        switch (v.getId()){
        case R.id.button1:
            db = myHelper.getWritableDatabase();
            name = et1.getText().toString();
            price = et2.getText().toString();
            number = et3.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.button2:
            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);
                listView1.setAdapter(adapter);
                adapter.notifyDataSetChanged();
            }
            cursor.close();
            db.close();
            break;
        case  R.id.button3:
            name = et1.getText().toString();
            price = et2.getText().toString();
            number = et3.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<CartBean> list2 = new ArrayList();
            CartAdapter adapter = new CartAdapter(MainActivity.this,list2);
            listView1.setAdapter(adapter);
            adapter.notifyDataSetChanged();
            db.close();
            Toast.makeText(this, "信息已删除", Toast.LENGTH_SHORT).show();
            break;
            
       }
    
    }

}


package com.example.listview;

public 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 com.example.listview;

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 java.util.List;

public 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);
        }
    }

}
<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="com.example.listview.MainActivity" >

    <TextView
        android:id="@+id/tv1"
        android:layout_width="70dp"
        android:layout_height="30dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="52dp"
        android:text="名称:" 
        android:textSize="20sp"
        />

    <TextView
        android:id="@+id/tv2"
        android:layout_width="70dp"
        android:layout_height="30dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="82dp"
        android:text="价格:" 
        android:textSize="20sp"
        />

    <TextView
        android:id="@+id/tv3"
        android:layout_width="70dp"
        android:layout_height="30dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="112dp"
        android:text="个数:"
        android:textSize="20sp" />

    
    <EditText
         android:id="@+id/et1"
        android:layout_width="150dp"
        android:layout_height="40dp"
         android:layout_alignParentLeft="true"
         android:layout_alignParentTop="true"
        android:layout_marginTop="52dp"
        android:layout_marginLeft="90dp"
        />
    
    <EditText
         android:id="@+id/et2"
        android:layout_width="150dp"
        android:layout_height="40dp"
         android:layout_alignParentLeft="true"
         android:layout_alignParentTop="true"
        android:layout_marginTop="82dp"
        android:layout_marginLeft="90dp"
        />
    <EditText
         android:id="@+id/et3"
        android:layout_width="150dp"
        android:layout_height="40dp"
         android:layout_alignParentLeft="true"
         android:layout_alignParentTop="true"
        android:layout_marginTop="112dp"
        android:layout_marginLeft="90dp"
        />

    <Button
        android:id="@+id/button1"
        android:layout_width="60dp"
        android:layout_height="40dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="152dp"
         android:layout_marginLeft="10dp"
        android:text="添加" />
    <Button
        android:id="@+id/button2"
        android:layout_width="60dp"
        android:layout_height="40dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="152dp"
         android:layout_marginLeft="80dp"
        android:text="查询" />
    <Button
        android:id="@+id/button3"
        android:layout_width="60dp"
        android:layout_height="40dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="152dp"
        android:layout_marginLeft="150dp"
        android:text="修改" />
    <Button
        android:id="@+id/button4"
        android:layout_width="60dp"
        android:layout_height="40dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="152dp"
        android:layout_marginLeft="220dp"
        android:text="删除" />

    <ListView
        android:id="@+id/listView1"
        android:layout_width="300dp"
        android:layout_height="600dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="250dp"
        android:background="#46A3FF"
        >
    </ListView>
    
</RelativeLayout>


<?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 @ 2019-11-10 15:15  郭少锋18840631471  阅读(230)  评论(0编辑  收藏  举报