设置item间分割线,分割线高度
android:divider="@android:color/darker_gray"
android:dividerHeight="10dp"
MainActivity.java 代码如下:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Item item1=new Item();
item1.setId(1);
item1.setName("Android ");
item1.setPrice(5000.0);
Item item2=new Item();
item2.setId(2);
item2.setName("iphone");
item2.setPrice(4000.0);
Item[] items=new Item[]{item1,item2};
ItemAdapter adapter =new ItemAdapter(this,R.layout.item_layout,items);
ListView listlv=findViewById(R.id.listlv);
listlv.setAdapter(adapter);
}
Itme_layout.xml代码如下:
<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/itemid"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/itemName"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/itemPrice"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Item.java 代码如下:
public class Item {
private int id;
private String name;
private double price;
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 double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
ItemAdapter.java代码如下:
public class ItemAdapter extends ArrayAdapter <Item>{
private int resource;
public ItemAdapter(Context context, int resource,Item[] items) {
super(context, resource, items);
this.resource=resource;
}
@NonNull
@Override
public View getView(int position, View convertView,ViewGroup parent) {
// return super.getView(position, convertView, parent);
convertView=LayoutInflater.from(getContext()).inflate(this.resource,parent,false);
// TextView itemid=convertView.findViewById(R.id.itemid);
// itemid.setText(getItem(position).getId());
TextView itemName=convertView.findViewById(R.id.itemName);
itemName.setText(getItem(position).getName());
TextView itemPrice=convertView.findViewById(R.id.itemPrice);
itemPrice.setText(getItem(position).getPrice()+" ");
return convertView;
}
}