Java 如何在“回收者”视图中获取所有项目的总数
我在活动中有recycler视图和spinner,这取决于spinner值。post请求已发送到服务器,之后填充了recycler视图,其中每行都有一些成本。我想增加每行的成本。 截图如下: 在屏幕截图中,我们可以看到数量为2,成本为40,像这一行一样,其他行也有一些价值。因此,我想将每一行的所有成本相加,并将其显示在左下角的区域中,其中写入了Java 如何在“回收者”视图中获取所有项目的总数,java,android,Java,Android,我在活动中有recycler视图和spinner,这取决于spinner值。post请求已发送到服务器,之后填充了recycler视图,其中每行都有一些成本。我想增加每行的成本。 截图如下: 在屏幕截图中,我们可以看到数量为2,成本为40,像这一行一样,其他行也有一些价值。因此,我想将每一行的所有成本相加,并将其显示在左下角的区域中,其中写入了ToTAL 这是我的密码: 活动\u选择\u pack.xml <?xml version="1.0" encoding="utf-8"?>
ToTAL
这是我的密码:
活动\u选择\u pack.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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=".SelectPack">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/progress"
android:layout_centerInParent="true"/>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="100dp"
app:cardUseCompatPadding="true"
app:cardCornerRadius="3dp"
android:layout_margin="16dp"
android:id="@+id/marketCard"
app:cardBackgroundColor="@color/colorPrimary">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#fff"
android:textSize="15sp"
android:id="@+id/textMarket"
android:text="Select Market Name"/>
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="2dp"
android:layout_below="@+id/textMarket"
android:id="@+id/marketSpinner"
android:layout_marginTop="25dp"
android:background="@drawable/spinner_back"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:id="@+id/products"
android:layout_below="@+id/marketCard"
android:visibility="invisible"
android:layout_above="@+id/totalLayout"
android:layout_marginBottom="3dp"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="@color/colorPrimary"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:id="@+id/totalLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="40dp"
android:padding="10dp"
android:background="@drawable/spinner_back"
android:text="Total:00.00"
android:layout_centerVertical="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="40dp"
android:padding="8dp"
android:background="@drawable/login_but"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:text="Generate bill"
android:textSize="12sp"
android:textColor="#fff"/>
</RelativeLayout>
</RelativeLayout>
<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="3dp"
app:cardUseCompatPadding="true"
app:cardBackgroundColor="#fff">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Crown"
android:layout_margin="10dp"
android:id="@+id/marketName"
android:textSize="18sp"
android:textColor="@color/colorPrimary"/>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:id="@+id/view1"
android:layout_below="@+id/marketName"
android:background="#adadad"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Product No."/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="300"
android:id="@+id/productNo"
android:textColor="@color/colorPrimary"
android:layout_alignParentRight="true"/>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_marginTop="10dp"
android:layout_below="@+id/productNo"
android:background="#adadad"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Page"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="300"
android:id="@+id/page"
android:textColor="@color/colorPrimary"
android:layout_alignParentRight="true"/>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_marginTop="10dp"
android:layout_below="@+id/page"
android:background="#adadad"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MRP"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="300"
android:id="@+id/mrp"
android:textColor="@color/colorPrimary"
android:layout_alignParentRight="true"/>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_marginTop="10dp"
android:layout_below="@+id/mrp"
android:background="#adadad"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Inner pack"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="300"
android:id="@+id/innerPack"
android:textColor="@color/colorPrimary"
android:layout_alignParentRight="true"/>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_marginTop="10dp"
android:layout_below="@+id/innerPack"
android:background="#adadad"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Outer pack"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="300"
android:id="@+id/outerPack"
android:textColor="@color/colorPrimary"
android:layout_alignParentRight="true"/>
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_marginTop="10dp"
android:layout_below="@+id/innerPack"
android:background="#adadad"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp">
<Spinner
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:id="@+id/qtySpinner"
android:background="@drawable/qty_spinner"
android:layout_weight="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TOTAL"
android:id="@+id/total"
android:layout_weight="1"/>
<Button
android:layout_width="50dp"
android:layout_height="30dp"
android:text="ORDER"
android:id="@+id/order"
android:textColor="#fff"
android:background="@drawable/login_but"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
选择pack\u layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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=".SelectPack">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/progress"
android:layout_centerInParent="true"/>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="100dp"
app:cardUseCompatPadding="true"
app:cardCornerRadius="3dp"
android:layout_margin="16dp"
android:id="@+id/marketCard"
app:cardBackgroundColor="@color/colorPrimary">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#fff"
android:textSize="15sp"
android:id="@+id/textMarket"
android:text="Select Market Name"/>
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="2dp"
android:layout_below="@+id/textMarket"
android:id="@+id/marketSpinner"
android:layout_marginTop="25dp"
android:background="@drawable/spinner_back"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:id="@+id/products"
android:layout_below="@+id/marketCard"
android:visibility="invisible"
android:layout_above="@+id/totalLayout"
android:layout_marginBottom="3dp"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="@color/colorPrimary"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:id="@+id/totalLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="40dp"
android:padding="10dp"
android:background="@drawable/spinner_back"
android:text="Total:00.00"
android:layout_centerVertical="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="40dp"
android:padding="8dp"
android:background="@drawable/login_but"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:text="Generate bill"
android:textSize="12sp"
android:textColor="#fff"/>
</RelativeLayout>
</RelativeLayout>
<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="3dp"
app:cardUseCompatPadding="true"
app:cardBackgroundColor="#fff">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Crown"
android:layout_margin="10dp"
android:id="@+id/marketName"
android:textSize="18sp"
android:textColor="@color/colorPrimary"/>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:id="@+id/view1"
android:layout_below="@+id/marketName"
android:background="#adadad"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Product No."/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="300"
android:id="@+id/productNo"
android:textColor="@color/colorPrimary"
android:layout_alignParentRight="true"/>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_marginTop="10dp"
android:layout_below="@+id/productNo"
android:background="#adadad"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Page"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="300"
android:id="@+id/page"
android:textColor="@color/colorPrimary"
android:layout_alignParentRight="true"/>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_marginTop="10dp"
android:layout_below="@+id/page"
android:background="#adadad"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MRP"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="300"
android:id="@+id/mrp"
android:textColor="@color/colorPrimary"
android:layout_alignParentRight="true"/>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_marginTop="10dp"
android:layout_below="@+id/mrp"
android:background="#adadad"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Inner pack"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="300"
android:id="@+id/innerPack"
android:textColor="@color/colorPrimary"
android:layout_alignParentRight="true"/>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_marginTop="10dp"
android:layout_below="@+id/innerPack"
android:background="#adadad"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Outer pack"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="300"
android:id="@+id/outerPack"
android:textColor="@color/colorPrimary"
android:layout_alignParentRight="true"/>
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_marginTop="10dp"
android:layout_below="@+id/innerPack"
android:background="#adadad"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp">
<Spinner
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:id="@+id/qtySpinner"
android:background="@drawable/qty_spinner"
android:layout_weight="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TOTAL"
android:id="@+id/total"
android:layout_weight="1"/>
<Button
android:layout_width="50dp"
android:layout_height="30dp"
android:text="ORDER"
android:id="@+id/order"
android:textColor="#fff"
android:background="@drawable/login_but"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
选择pack.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_pack);
fAuth = FirebaseAuth.getInstance();
ActionBar ab = getSupportActionBar();
assert ab!= null;
ab.setTitle("Select Pack");
ab.setDisplayHomeAsUpEnabled(true);
marketSpinner = findViewById(R.id.marketSpinner);
progress = findViewById(R.id.progress);
products = findViewById(R.id.products);
products.setHasFixedSize(true);
products.setLayoutManager(new LinearLayoutManager(this));
productList = new ArrayList<>();
List<String> categories = new ArrayList<String>();
categories.add("Select market");
categories.add("Crown");
categories.add("Long Book A4");
categories.add("Long Book");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
marketSpinner.setAdapter(dataAdapter);
marketSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String item = adapterView.getItemAtPosition(i).toString();
if(item.equals("Select market")){
progress.setVisibility(View.INVISIBLE);
}
else{
getData(item);
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
private void getData(String item){
progress.setVisibility(View.VISIBLE);
products.setVisibility(View.INVISIBLE);
productList.clear();
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(20, TimeUnit.SECONDS)
.readTimeout(20,TimeUnit.SECONDS)
.writeTimeout(20,TimeUnit.SECONDS)
.build();
RequestBody formBody = new FormBody.Builder()
.add("name",item)
.build();
Request request = new Request.Builder().post(formBody).url(URL).build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onResponse(@NotNull Call call, @NotNull final Response response) throws IOException {
runOnUiThread(new Runnable() {
@Override
public void run() {
try {
JSONArray jsonArray = new JSONArray(response.body().string());
if(jsonArray.length() > 0){
products.setVisibility(View.VISIBLE);
progress.setVisibility(View.INVISIBLE);
}
for(int i=0;i<jsonArray.length();i++){
progress.setVisibility(View.INVISIBLE);
JSONObject object = jsonArray.getJSONObject(i);
String str1 = object.getString("market");
String str2 = object.getString("product_no");
String str3 = object.getString("page");
String str4 = object.getString("mrp");
String str5 = object.getString("inner_pack");
String str6 = object.getString("outer_pack");
Log.d("prod",str2);
ProductsModel model = new ProductsModel(str1,str2,str3,str4,str5,str6);
productList.add(model);
}
ProductAdapter adapter = new ProductAdapter(getApplicationContext(),productList);
products.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
@Override
public void onFailure(@NotNull Call call, @NotNull final IOException e) {
runOnUiThread(new Runnable() {
@Override
public void run() {
progress.setVisibility(View.INVISIBLE);
products.setVisibility(View.INVISIBLE);
Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_SHORT).show();
}
});
}
});
}
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u select\u pack);
fAuth=FirebaseAuth.getInstance();
ActionBar ab=getSupportActionBar();
断言ab!=null;
ab.setTitle(“选择包”);
ab.setDisplayHomeAsUpEnabled(真);
marketSpinner=findViewById(R.id.marketSpinner);
进度=findViewById(R.id.progress);
产品=findViewById(R.id.products);
产品。setHasFixedSize(真);
产品。setLayoutManager(新的LinearLayoutManager(本));
productList=新的ArrayList();
列表类别=新建ArrayList();
类别。添加(“选择市场”);
类别。添加(“官方”);
类别。添加(“长卷A4”);
类别。添加(“长卷”);
ArrayAdapter dataAdapter=新的ArrayAdapter(这是android.R.layout.simple\u微调器\u项,类别);
dataAdapter.setDropDownViewResource(android.R.layout.simple\u微调器\u下拉项);
marketSpinner.setAdapter(dataAdapter);
marketSpinner.setOnItemSelectedListener(新的AdapterView.OnItemSelectedListener(){
@凌驾
已选择公共视图(AdapterView AdapterView、View视图、int i、long l){
String item=adapterView.getItemAtPosition(i).toString();
如果(项目等于(“选择市场”)){
progress.setVisibility(View.INVISIBLE);
}
否则{
获取数据(项目);
}
}
@凌驾
未选择公共无效(AdapterView AdapterView){
}
});
}
私有void getData(字符串项){
progress.setVisibility(View.VISIBLE);
产品.设置可见性(视图.不可见);
productList.clear();
OkHttpClient客户端=新建OkHttpClient.Builder()
.connectTimeout(20,时间单位为秒)
.readTimeout(20,时间单位为秒)
.writeTimeout(20,时间单位。秒)
.build();
RequestBody formBody=new formBody.Builder()
.添加(“名称”,项目)
.build();
Request Request=newrequest.Builder().post(formBody.url(url.build());
client.newCall(request).enqueue(new Callback()){
@凌驾
public void onResponse(@NotNull调用,@NotNull最终响应)引发IOException{
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
试一试{
JSONArray JSONArray=newjsonarray(response.body().string());
if(jsonArray.length()>0){
产品.setVisibility(视图.VISIBLE);
progress.setVisibility(View.INVISIBLE);
}
for(int i=0;i adapterView){
}
});
}
@凌驾
public int getItemCount(){
返回productList.size();
}
公共类ViewHolder扩展了RecyclerView.ViewHolder{
TextView marketName、productNo、page、mrp、innerPack、outerPack、total;
纺纱机;
按钮顺序;
公共视图持有者(@NonNull View itemView){
超级(项目视图);
order=itemView.findviewbyd(R.id.order);
qtySpinner=itemviewbyd(R.id.qtySpinner);
marketName=itemView.findViewById(R.id.marketName);
productNo=itemView.findviewbyd(R.id.productNo);
page=itemView.findviewbyd(R.id.page);
mrp=itemView.findviewbyd(R.id.mrp);
innerPack=itemView.findViewById(R.id.innerPack);
outerPack=itemView.findviewbyd(R.id.outerPack);
总计=itemView.findViewById(R.id.total);
}
}
}
有人请让我知道我如何获得和添加每行的总成本,并显示在左下角区域。任何帮助将不胜感激
谢谢您可以使用数组并在其中附加每个单元格的值,但不清除它,然后在适配器中创建一个函数,该函数随此数组返回,在这里您可以添加它的项。我如何将此实现为代码请您使用代码更新您的答案。