在一个java.class中接收数据并将该数据传递给其他人

时间:2018-09-26 21:43:09

标签: java android

当我开始另一个活动时,我需要从我的'SectoinDataAdapter'中传递数据,必须在'RecyclerViewAdapter'中接收它们并使用它们,然后对该数据进行任何更改,然后将其传递给另一个java.class(适配器(如果需要的话))从“ RecyclerViewAdapter”到另一个名为“ BookActivity”和其他jaca.classes

我的“ SectoinDataAdapter”:

public class SectionDataAdapter extends RecyclerView.Adapter<SectionDataAdapter.SingleItemRowHolder>{

private final Context mContext;

private ArrayList<SingleItemModel> itemModels;


public SectionDataAdapter(ArrayList<SingleItemModel> itemModels, Context mContext) {
    this.itemModels = itemModels;

    this.mContext = mContext;
}


@Override
public SingleItemRowHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_single_card, null);
    SingleItemRowHolder singleItemRowHolder = new SingleItemRowHolder(v);
    return singleItemRowHolder;
}

@Override
public void onBindViewHolder(SingleItemRowHolder holder, int position) {




    SingleItemModel itemModel = itemModels.get(position);

    holder.tvTitle.setText(itemModel.getWord());
    holder.mitemImage.setImageResource(itemModel.getImage());


    Tab1Fragment tab1Fragment=new Tab1Fragment();
    Bundle bundle=new Bundle();
    bundle.putString("NAME",itemModel.getWord());
    bundle.putInt("JOB",itemModel.getImage());
    tab1Fragment.setArguments(bundle);

}


@Override
public int getItemCount() {
    return (null != itemModels ? itemModels.size() : 0);
}

public class SingleItemRowHolder extends RecyclerView.ViewHolder {


    protected TextView tvTitle;
    protected ImageView mitemImage;



    public SingleItemRowHolder(final View itemView) {
        super(itemView);
        final Intent intent = new Intent(mContext, ActivityDialogTheme.class);

        final Intent intent1 = new Intent(mContext, MainActivity2.class);

        final Activity activity = (Activity) mContext;
        this.mitemImage = itemView.findViewById(R.id.itemImage);
        this.tvTitle = itemView.findViewById(R.id.tvTitle);





        itemView.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                Toast.makeText(v.getContext(), tvTitle.getText(), LENGTH_SHORT).show();
                mContext.startActivity(intent1);

            }

        });
        itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {


                Toast.makeText(mContext,tvTitle.getText(), Toast.LENGTH_SHORT).show();
                //here i need to pass data to 'recyclerview' adapter
                mContext.startActivity(intent);
                activity.overridePendingTransition(R.anim.bottom_in, R.anim.fade_in_right);
                return true;
            }


        });

        }

    }
}

这是我的“ RecyclerViwerAdapter”:

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder0> {
private Context mContext ;
private ArrayList<SingleItemModel> mData ;

public RecyclerViewAdapter(Context mContext, ArrayList<SingleItemModel> mData) {
    this.mContext = mContext;
    this.mData = mData;
}

@Override
public MyViewHolder0 onCreateViewHolder(ViewGroup parent, int viewType) {
    View view ;
    LayoutInflater mInflater = LayoutInflater.from(mContext);
    view = mInflater.inflate(R.layout.cardview_item_book,parent,false);
    return new MyViewHolder0(view);
}
@Override
public void onBindViewHolder(MyViewHolder0 holder, final int position) {
    holder.tv_book_title.setText(mData.get(position).getWord());
    holder.img_book_thumbnail.setImageResource(mData.get(position).getImage());
        holder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext,Book_Activity.class);

                mContext.startActivity(intent);
            }
        });
    }
    @Override
    public int getItemCount() {
        return mData.size();
    }

    public static class MyViewHolder0 extends RecyclerView.ViewHolder {
        TextView tv_book_title;
        ImageView img_book_thumbnail;
        CardView cardView ;

        public MyViewHolder0(View itemView) {
            super(itemView);
            tv_book_title =  itemView.findViewById(R.id.book_title_id) ;
            img_book_thumbnail = itemView.findViewById(R.id.book_img_id);
            cardView =  itemView.findViewById(R.id.cardview_id);
        }
    }
}

我不认为需要BookActivity的代码 我确实知道如何传递和接收数据(使用捆绑方法,因为我也必须将数据传递给片段,这就是为什么必须使用捆绑方法的原因),我的问题是如何在一个java.class中接收数据,然后将该数据传递给另一个java.class。 ?

0 个答案:

没有答案