如何通过单击回收器视图外的按钮将数据传递给回收器视图中的另一个活动



所以,我一直在这个食品配送应用程序上工作。我想要从回收者视图到另一个活动的数据,但是在点击不在回收者视图中的按钮时,你可以看到该按钮不在回收者视图

package com.example.al_aham;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.room.Room;
import org.jetbrains.annotations.NotNull;
import java.util.List;
public class myadapter  extends RecyclerView.Adapter<myadapter.myviewholder>
{
List<Product> products;
Button checkout;
TextView rateview;
public myadapter(List<Product> products, TextView rateview) {
this.products = products;
this.rateview= rateview;
}
@NonNull
@NotNull
@Override
public myviewholder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.singlerowdesign,parent,false);
return new myviewholder(view);
}
@Override
public void onBindViewHolder(@NonNull @NotNull myviewholder holder, int position) {

holder.recid.setText(String.valueOf(products.get(position).getPid()));
holder.recpname.setText(products.get(position).getPname());
holder.recpprice.setText(String.valueOf(products.get(position).getPrice()));
holder.recqnt.setText(String.valueOf(products.get(position).getQnt()));


holder.delbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AppDatabase db = Room.databaseBuilder(holder.recid.getContext(),
AppDatabase.class, "cart_db").allowMainThreadQueries().build();
ProductDao productDao = db.ProductDao();
productDao.deleteById(products.get(position).getPid());
products.remove(position);
notifyItemRemoved(position);
updateprice();
}
});
holder.incr.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int qnt=products.get(position).getQnt();
qnt++;
products.get(position).setQnt(qnt);
notifyDataSetChanged();
updateprice();
}
});
holder.decr.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int qnt=products.get(position).getQnt();
qnt--;
products.get(position).setQnt(qnt);
notifyDataSetChanged();
updateprice();
}
});

}
@Override
public int getItemCount() {
return products.size();
}
class myviewholder extends RecyclerView.ViewHolder
{
TextView recid,recpname,recqnt, recpprice;
ImageButton delbtn;
ImageView incr,decr;
public myviewholder(@NonNull @NotNull View itemView) {
super(itemView);
recid=itemView.findViewById(R.id.recid);
recpname=itemView.findViewById(R.id.recpname);
recpprice=itemView.findViewById(R.id.recpprice);
recqnt=itemView.findViewById(R.id.recqnt);
delbtn=itemView.findViewById(R.id.delbtn);
incr=itemView.findViewById(R.id.incbtn);
decr=itemView.findViewById(R.id.decbtn);
}
}
public void updateprice()
{
int sum=0,i;
for(i=0;i< products.size();i++)
sum=sum+(products.get(i).getPrice()*products.get(i).getQnt());
rateview.setText("Total Amount : INR "+sum);
}
}

package com.example.al_aham;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.room.Room;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class cartdata extends AppCompatActivity {
List<Product> products;
RecyclerView recview;
Button checkout;
TextView rateview;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cartdata);
getSupportActionBar().hide();
Button checkout = findViewById(R.id.checkout);
products = new ArrayList<>();
rateview=findViewById(R.id.rateview);
getroomdata();
checkout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

Intent intent = new Intent(cartdata.this, Checkout.class);
intent.putExtra("pname", products.get(1).getPname());
startActivity(intent);
}
});

}
public void getroomdata()
{
AppDatabase db = Room.databaseBuilder(getApplicationContext(),
AppDatabase.class, "cart_db").allowMainThreadQueries().build();
ProductDao productDao = db.ProductDao();
recview=findViewById(R.id.recview);
recview.setLayoutManager(new LinearLayoutManager(this));

List<Product> products=productDao.getallproduct();
myadapter adapter=new myadapter(products, rateview);
recview.setAdapter(adapter);
int sum=0,i;
for(i=0;i< products.size();i++)
sum=sum+(products.get(i).getPrice()*products.get(i).getQnt());
rateview.setText("Total Amount : INR "+sum);

}

}

输入图片描述

我想用它来传递数据给另一个活动通过点击那个按钮

试试下面的代码

Intent intent = new Intent(cartdata.this, Checkout.class);
intent.putExtra("list", products);
startActivity(intent);

在Checkout活动中像这样获取数据ArrayList myList = getIntent ().getSerializableExtra("list");

相关内容

  • 没有找到相关文章

最新更新