【发布时间】:2021-12-12 20:42:08
【问题描述】:
我正在使用安卓工作室。代码 Java。我将 json 文件中的数据带到 listview。当我按下增加和减少按钮时,我想把它带到文本视图中。我只想增加或减少我点击的产品数量。我无法访问列表视图中的按钮和文本视图。我该怎么做?
try {
JSONObject obj=new JSONObject(LoadFromJsonAssets());
Resources resources = context.getResources();
JSONArray array =obj.getJSONArray("domestic");
HashMap<String,String> list;
ArrayList<HashMap<String,String>> arrayList=new ArrayList<>();
for (int i=0;i<array.length();i++){
JSONObject o=array.getJSONObject(i);
String productName=o.getString("productName");
String productPrice=o.getString("productPrice");
String productPic=o.getString("productPic");
final int resourceId = resources.getIdentifier(productPic, "drawable", context.getPackageName());
Drawable drawable = resources.getDrawable(resourceId);
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resourceId);
list= new HashMap<>();
list.put("productName",productName);
list.put("productPrice",productPrice);
list.put("productPic",Integer.toString(resourceId) );
arrayList.add(list);
}
final ListAdapter adapter= new SimpleAdapter(this,arrayList,R.layout.list_view_design,new String[]{"productName","productPrice","productPic"},new int[]{R.id.productName,R.id.productPrice,R.id.productPic});
listView.setAdapter(adapter);
列表视图设计:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:orientation="vertical"
android:layout_margin="10dp"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp">
<ImageView
android:layout_marginLeft="3dp"
android:id="@+id/productPic"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentStart="true"
android:layout_centerInParent="true">
</ImageView>
<LinearLayout
android:layout_marginLeft="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginRight="25dp"
android:layout_toLeftOf="@+id/lin2"
android:layout_toRightOf="@id/productPic"
android:orientation="vertical">
<TextView
android:id="@+id/productName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Product Name"
android:textSize="20dp"
android:textStyle="bold"></TextView>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/productPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Product price"></TextView>
<TextView
android:id="@+id/turkishlira"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_marginTop="5dp"
android:text="₺"></TextView>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/lin2"
android:orientation="horizontal"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:layout_marginRight="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/decreasing "
android:layout_marginRight="10dp"
android:src="@drawable/negative"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageView>
<TextView
android:layout_gravity="center"
android:id="@+id/counterrrr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="15dp"
></TextView>
<ImageView
android:id="@+id/Increasing"
android:layout_marginLeft="10dp"
android:src="@drawable/positive"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageView>
</LinearLayout>
</RelativeLayout>
【问题讨论】:
标签: java android list listview android-listview