<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <ListView android:id="@+id/good" android:layout_width="wrap_content" android:layout_height="wrap_content" tools:ignore="MissingConstraints"></ListView> </androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/imgood" android:layout_width="150dp" android:layout_height="131dp" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:layout_marginStart="2dp" android:layout_marginTop="4dp" android:layout_weight="2" android:layout_alignParentLeft="true" android:layout_marginLeft="2dp"></ImageView> <TextView android:id="@+id/name" android:layout_width="255dp" android:layout_height="57dp" android:layout_alignParentStart="true" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_alignParentEnd="true" android:layout_alignParentRight="true" android:layout_marginStart="157dp" android:layout_marginLeft="157dp" android:layout_marginTop="28dp" android:layout_marginEnd="-2dp" android:layout_marginRight="-2dp" android:layout_weight="3" android:text="TextView" android:textSize="20dp" /> <TextView android:id="@+id/price" android:layout_width="253dp" android:layout_height="69dp" android:layout_alignParentTop="true" android:layout_alignParentEnd="true" android:layout_alignParentRight="true" android:layout_marginTop="95dp" android:layout_marginEnd="-5dp" android:layout_marginRight="-5dp" android:text="TextView" android:textSize="12dp" /> </RelativeLayout>
package com.example.listview; public class Goods { private int id; private String name; private int price; private int imageId; public Goods() { } public Goods(String name, int price, int imageId) { this.name = name; this.price = price; this.imageId = imageId; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } public int getImageId() { return imageId; } public void setImageId(int imageId) { this.imageId = imageId; } }
package com.example.listview; import androidx.appcompat.app.AppCompatActivity; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { List<Goods> list=new ArrayList<Goods>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ShopHelper shopHelper=new ShopHelper(this); SQLiteDatabase db = shopHelper.getReadableDatabase(); Cursor cursor = db.rawQuery("select * from goodinfo", null); if (cursor.getCount() != 0) { while (cursor.moveToNext()) { // s += cursor.getInt(0) + " " + cursor.getString(1) + " " // + cursor.getInt(2) + "\n"; Goods goods=new Goods(); goods.setId(cursor.getInt(0)); goods.setName(cursor.getString(1)); goods.setPrice(cursor.getInt(2)); goods.setImageId(R.drawable.good); list.add(goods); ListView lv=(ListView) findViewById(R.id.good); lv.setAdapter(new myadapter()); } } } private class myadapter 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 i, View view, ViewGroup viewGroup) { View view2 = View.inflate(MainActivity.this, R.layout.list_item, null); TextView name=(TextView)view2.findViewById(R.id.name); TextView price=(TextView)view2.findViewById(R.id.price); ImageView image = (ImageView) view2.findViewById(R.id.imgood); name.setText(list.get(i).getName()); price.setText(list.get(i).getPrice()+""); image.setImageResource(R.drawable.good); return view2; } } }
package com.example.listview; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import androidx.annotation.Nullable; public class ShopHelper extends SQLiteOpenHelper { public ShopHelper(@Nullable Context context) { super(context, "Goods.db", null, 1); } @Override public void onCreate(SQLiteDatabase sqLiteDatabase) { sqLiteDatabase.execSQL("create table goodinfo(_id integer primary key autoincrement,name varchar(20),price integer)"); sqLiteDatabase.execSQL("insert into goodinfo (name,price) values('香蕉',10)"); sqLiteDatabase.execSQL("insert into goodinfo (name,price) values('苹果',20)"); sqLiteDatabase.execSQL("insert into goodinfo (name,price) values('西瓜',30)"); } @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { } }