将数据从recyclerView传递到另一个recycleView

时间:2019-04-30 04:24:56

标签: android android-intent android-recyclerview

im试图传递来自recyclerView的数据,该数据在另一个活动中作为搜索和购物车与另一个recyclerView连接到数据库,以便能够将其作为订单添加回数据库中,我不确定我是否做对了一切此主题的在线资源非常有限。我没有收到任何错误,也没有收到数据。

  

我知道这个问题,但我不知道如何解决。

     

如何在recyclerView布局中投射Textview。我吓到充气机了,但说不能重新使用方法


RecyclerView适配器用于来自数据库的数据


公共类RecyclerAdapter扩展了RecyclerView.Adapter {

private static final String TAG = "RecyclerAdapter";

private List<ModelClass> modelClassList;

private Context mContext;

public RecyclerAdapter(Context context, List<ModelClass> modelClassList) {
    this.modelClassList = modelClassList;
    mContext = context;


}


@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_item, parent, false);
    MyViewHolder holder = new MyViewHolder(v); //ini
    // sialisasi ViewHolder




    return holder;


}

@NonNull
@Override
public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, final int i) {

    final ModelClass data =modelClassList.get(i);

    myViewHolder.mactivity.setText(data.getActivity());
    myViewHolder.morigin.setText(data.getOrigin());
    myViewHolder.mdestination.setText(data.getDestination());
    myViewHolder.mdescription.setText(data.getDescription());
    myViewHolder.mdate_from.setText(data.getDate_from());
    myViewHolder.mdate_to.setText(data.getDate_to());
    myViewHolder.mprice.setText(data.getPrice());

    myViewHolder.btnadd.setOnClickListener((new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d(TAG, "onClick: Clicked on"+ data.getActivity());

            Intent intent = new Intent(mContext,CART.class);
            intent.putExtra("activity", data.getActivity());
            intent.putExtra("origin",data.getOrigin());
            intent.putExtra("destination",data.getDestination());
            intent.putExtra("description",data.getDescription());
            intent.putExtra("date_from",data.getDate_from());
            intent.putExtra("date_to",data.getDate_to());
            intent.putExtra("price",data.getPrice());

            mContext.startActivity(intent);



        }
    }));








}



@Override
public int getItemCount() {
    return modelClassList.size();
}

public static class  MyViewHolder extends RecyclerView.ViewHolder {


    TextView mactivity;
    TextView morigin;
    TextView mdestination;
    TextView mdescription;
    TextView mdate_from;
    TextView mdate_to;
    TextView mprice;
    ImageView img_activity;
    Button btnadd;

    public MyViewHolder(@NonNull View itemView) {
        super(itemView);

        mactivity = itemView.findViewById(R.id.tvactivity);
        morigin = itemView.findViewById(R.id.tvorigin);
        mdestination = itemView.findViewById(R.id.tvdestination);
        mdescription = itemView.findViewById(R.id.tvdescription);
        mdate_from = itemView.findViewById(R.id.tvdate_from);
        mdate_to = itemView.findViewById(R.id.tvdate_to);
        mprice = itemView.findViewById(R.id.tvprice);
        img_activity = itemView.findViewById(R.id.img_activity);
        btnadd = itemView.findViewById(R.id.btnadd);












            }
}

}


CARTADAPTER


  public class CartAdapter extends RecyclerView.Adapter<CartAdapter.MyViewHolder> {

private List<ModelClass2> modelClassList;

private Context mContext;

public CartAdapter(Context context, List<ModelClass2> modelClassList) {
    this.modelClassList = modelClassList;
    mContext = context;


}


@NonNull
@Override
public CartAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout, parent, false);
    MyViewHolder holder = new MyViewHolder(v); //ini
    // sialisasi ViewHolder


    return holder;


}

@NonNull
@Override
public void onBindViewHolder(@NonNull CartAdapter.MyViewHolder myViewHolder, final int i) {

    final ModelClass2 data = modelClassList.get(i);

    myViewHolder.mactivity.getText();
    myViewHolder.morigin.setText(data.getOrigin());
    myViewHolder.mdestination.setText(data.getDestination());
    myViewHolder.mdescription.setText(data.getDescription());
    myViewHolder.mdate_from.setText(data.getDate_from());
    myViewHolder.mdate_to.setText(data.getDate_to());
    myViewHolder.mprice.setText(data.getPrice());







}


@Override
public int getItemCount() {
    return modelClassList.size();
}

public static class MyViewHolder extends RecyclerView.ViewHolder {


    TextView mactivity;
    TextView morigin;
    TextView mdestination;
    TextView mdescription;
    TextView mdate_from;
    TextView mdate_to;
    TextView mprice;
    ImageView img_activity;


    public MyViewHolder(@NonNull View itemView) {
        super(itemView);

        mactivity = itemView.findViewById(R.id.tvactivity);
        morigin = itemView.findViewById(R.id.tvorigin);
        mdestination = itemView.findViewById(R.id.tvdestination);
        mdescription = itemView.findViewById(R.id.tvdescription);
        mdate_from = itemView.findViewById(R.id.tvdate_from);
        mdate_to = itemView.findViewById(R.id.tvdate_to);
        mprice = itemView.findViewById(R.id.tvprice);
        img_activity = itemView.findViewById(R.id.img_activity);



    }
}

}


CARTCLASS


  public class CART extends AppCompatActivity {

private static final String TAG ="GalleryActivity" ;
private RecyclerView recyclerView;
private Button btnMainactivity;
Class ModelClass2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cart);

    btnMainactivity = findViewById(R.id.btnMainactivity);

    btnMainactivity.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent abreCad = new Intent(CART.this, MainActivity.class);
            startActivity(abreCad);




        }
    });


    recyclerView = findViewById(R.id.recycler_view2);
    LinearLayoutManager layoutManager = new LinearLayoutManager(this);
    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);

    recyclerView.setLayoutManager(layoutManager);

    List<ModelClass2> modelClassList = new ArrayList<>();



   getIncomingIntent();


    CartAdapter adapter = new CartAdapter(CART.this, modelClassList);
    recyclerView.setAdapter(adapter);
    adapter.notifyDataSetChanged();
}

private void getIncomingIntent(){
    Log.d(TAG, "getIncomingIntent: Checking for incoming intents");

    if(getIntent().hasExtra("activity")){
        Log.d(TAG, "getIncomingIntent: found intent extras");

        String activity = getIntent().getStringExtra("activity");

        setList(activity);


    }

}

private void setList(String activity){

    TextView mactivity = findViewById(R.id.tvactivity);
    mactivity.setText(activity);


}

}


在CartClass的最后3行中出现错误=


>

TextView mactivity = findViewById(R.id.tvactivity);
        mactivity.setText(activity);

错误


  

FATAL EXCEPTION: main
    Process: com.example.test, PID: 26271
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.CART.CART}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
        at com.example.test.CART.CART.setList(CART.java:84)
        at com.example.test.CART.CART.getIncomingIntent(CART.java:74)
        at com.example.test.CART.CART.onCreate(CART.java:58)
        at android.app.Activity.performCreate(Activity.java:7136)
        at android.app.Activity.performCreate(Activity.java:7127)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:193) 
        at android.app.ActivityThread.main(ActivityThread.java:6669) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 

0 个答案:

没有答案