<?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"> <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="35dp" android:background="#EDC73E" android:gravity="center" android:text="购物商城" android:textColor="#0E0E0E" android:textSize="25sp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="vertical"> <ListView android:id="@+id/hh_h" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:id="@+id/imageView" android:layout_width="146dp" android:layout_height="129dp" app:srcCompat="@drawable/maoyi" /> <LinearLayout android:layout_width="match_parent" android:layout_height="129dp" android:gravity="left|center_vertical" android:orientation="vertical" android:scrollbarSize="8dp"> <TextView android:id="@+id/textName" android:layout_width="74dp" android:layout_height="39dp" android:text="毛衣" android:textColor="#2D7DF6" /> <TextView android:id="@+id/textPrice" android:layout_width="143dp" android:layout_height="36dp" android:text="价格:500元" android:textColor="#E83333" android:textSize="16sp" /> </LinearLayout> </LinearLayout>
package com.example.myapplication; import android.app.Activity; import android.os.Bundle; import android.widget.ListView; import java.util.ArrayList; import java.util.List; public class MainActivity extends Activity { final List<hh> hhList = new ArrayList<hh>(); // private Object h; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); i(); z adapter = new z(this,R.layout.h, hhList); ListView lv = (ListView)findViewById(R.id.hh_h); lv.setAdapter(adapter); } public void i(){ hh bao = new hh("手提袋","108元",R.drawable.bao); hhList.add(bao); hh mojing = new hh("墨镜","630元",R.drawable.mojing); hhList.add(mojing); hh maozi = new hh("帽子","166元",R.drawable.maozi); hhList.add(maozi); hh maoyi = new hh("毛衣","500元",R.drawable.maoyi); hhList.add(maoyi); hh xiezi = new hh("鞋子","200元",R.drawable.xiezi); hhList.add(xiezi); } }
package com.example.myapplication; public class hh { // private static String Name; private int imageId; private String Name; private String Price; public hh(String Name, String Price, int imageId) { super(); this.imageId = imageId; this.Name = Name; this.Price = Price; } public int getImageId() { return imageId; } public void setImageId(int imageId) { this.imageId = imageId; } public String getName() { return Name; } public void setName(String name) { Name = name; } public String getPrice() { return Price; } public void setPrice(String price) { Price = price; } public Object geth(int position){ return position; } }
package com.example.myapplication; import android.widget.ArrayAdapter; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import java.util.List; public class z extends ArrayAdapter { int resourceId; public z(Context context, int resource, List<hh> objects){ super(context, resource,objects); resourceId=resource; } @Override public View getView(int position, View convertView, ViewGroup parent){ hh f = (hh)getItem(position); View view = LayoutInflater.from(getContext()).inflate(resourceId,null); ImageView iv = (ImageView)view.findViewById(R.id.imageView); TextView tv = (TextView)view.findViewById(R.id.textName); TextView pv = (TextView)view.findViewById(R.id.textPrice); iv.setImageResource(f.getImageId()); tv.setText((CharSequence) f.getName()); pv.setText((CharSequence)f.getPrice()); return view; } }