安卓商城购物车案例
写的很垃圾也是初学呢,望多包容
大概长这样:
activity_main.xml布局文件
<?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"
app:layout_constraintTop_toTopOf="parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintTop_toTopOf="parent"
android:orientation="vertical">
<TextView
app:layout_constraintTop_toTopOf="parent"
android:layout_width="match_parent"
android:layout_height="40dp"
android:textSize="30sp"
android:text=" -------欢迎光临-------" />
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:layout_width="match_parent"
android:layout_height="70dp"
android:text="选好了"
android:textSize="30sp"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Ls_tmist_itemxl.xml板模板件
<?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="wrap_content"
>
<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
/>
<ImageView
android:id="@+id/image"
android:layout_width="110dp"
android:layout_height="110dp"
android:padding="5dp"
app:srcCompat="@drawable/dish_01" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="5dp"
android:textColor="#000fff"
android:textSize="30sp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/money"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:textColor="#D52F23"
android:textSize="30sp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="元/份"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Jvava码代码
package com.example.menutest;
import androidx.appcompat.app.AppCompatActivity;
import android.app.LauncherActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity {
ArrayList<HashMap<Sting,Object>>lisItems=null;
HashMap<String,Object>map=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView mylistview=(ListView)findViewById(R.id.listview);
ArrayList<HashMap<String, Object>> listItems = new ArrayList<HashMap<String,
Object>>();
int[]
userImg={R.drawable.dish_01,R.drawable.dish_02,R.drawable.dish_03,R.drawable.dish_04,R.d
rawable.dish_05,
R.drawable.dish_06,R.drawable.dish_07,R.drawable.dish_08};
String[] menuName=getResources().getStringArray(R.array.menuName);
String[] money=getResources().getStringArray(R.array.money);
for(int i=0;i<money.length;i++){
map=new HashMap<String,Object>();
map.put("img",userImg[i]);
map.put("name",menuName[i]);
map.put("money",money[i]);
listItems.add(map);
}
//创建并实例化SimpleAdapter
SimpleAdapter adapter=new SimpleAdapter(this,listItems,R.layout.list_item,new
String[]{"img","name","money"},
new int[]{R.id.image,R.id.name,R.id.money});
mylistview.setAdapter(adapter);
// mylistview.setOnItemClickListener();
}
}
emmmmm,这只是学校的一个小作业,记录一下,有更好的方法欢迎留言交流,也让我学习学习,哈哈哈哈