zoukankan      html  css  js  c++  java
  • Android.6

    <?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) + "
    ";
                    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;
            }
        }
    }
    

      

      

     

      

     

      

  • 相关阅读:
    angular 实现全选、反选、个别选择的实现
    idea下git版本回退
    统一设置滚动条样式
    vue小知识点
    Java面试题之Oracle 支持哪三种事务隔离级别
    Java面试题之hashmap中用什么hash算法解决碰撞的?
    Java面试题之Integer.valueOf(String s);采用了什么设计模式
    TCP面试题之为什么会有TIME_WAIT状态
    Java面试题之如何防止重复下单问题?
    Java面试题之在多线程情况下,单例模式中懒汉和饿汉会有什么问题呢?
  • 原文地址:https://www.cnblogs.com/Libreation/p/14030573.html
Copyright © 2011-2022 走看看