pass MainActivity's data to Tab Fragment

你离开我真会死。 提交于 2019-12-11 15:45:49

问题


I'm new to android and java but in my very first app I'm kinda doing play store, the first thing you see on play store and then go to the second activity and see that whole list there. I've built the horizontal ArrayList and i succeeded to build Cardview of second activity as well, my ArrayList is static i mean it's not using any server.

My problem is how can i send MainActivity's data via the adapter which is situated in it to MainActivity2.

Here is my Main Activity which my data is situated there:

public class MainActivity extends AppCompatActivity {

private ArrayList<SectionDataModel> allSampleData;


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

    allSampleData = new ArrayList<>();


    RecyclerView recyclerView = findViewById(R.id.my_recycler_view1);
    recyclerView.setHasFixedSize(true);
    RecyclerViewDataAdapter adapter = new RecyclerViewDataAdapter(allSampleData, this);
    recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
    recyclerView.setAdapter(adapter);


    EssentialData();



}

public void EssentialData() {
    SectionDataModel Unit1 = new SectionDataModel();
    Unit1.setHeaderTitle("Unit 1");


    ArrayList<SingleItemModel> singleItemModels = new ArrayList<>();

    singleItemModels.add(new SingleItemModel("Word ", "Pronunciation", "Example", R.drawable.alferet));
    singleItemModels.add(new SingleItemModel("The Apple", "Apple Store Is Open", "Description book", R.drawable.alferet));
    singleItemModels.add(new SingleItemModel("The Apple", "Apple Store Is Open", "Description book", R.drawable.soft));
    singleItemModels.add(new SingleItemModel("The Apple", "Apple Store Is Open", "Description book", R.drawable.alferet));
    singleItemModels.add(new SingleItemModel("The Apple", "Apple Store Is Open", "Description book", R.drawable.alferet));
    singleItemModels.add(new SingleItemModel("The Apple", "Apple Store Is Open", "Description book", R.drawable.alferet));
    singleItemModels.add(new SingleItemModel("The Apple", "Apple Store Is Open", "Description book", R.drawable.alferet));
    singleItemModels.add(new SingleItemModel("The Apple", "Apple Store Is Open", "Description book", R.drawable.alferet));
    singleItemModels.add(new SingleItemModel("The Apple", "Apple Store Is Open", "Description book", R.drawable.alferet));
    singleItemModels.add(new SingleItemModel("The Apple", "Apple Store Is Open", "Description book", R.drawable.alferet));
    singleItemModels.add(new SingleItemModel("The Apple", "Apple Store Is Open", "Description book", R.drawable.alferet));
    singleItemModels.add(new SingleItemModel("The Apple", "Apple Store Is Open", "Description book", R.drawable.alferet));
    singleItemModels.add(new SingleItemModel("The Apple", "Apple Store Is Open", "Description book", R.drawable.alferet));


    Unit1.setAllItemInSection(singleItemModels);
    allSampleData.add(Unit1);


}
  }

And my SectionDataAdapter which only from it i can send Data to Second MainActivity because if i do it from MainActivity itself it returs Null :

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

private final Context mContext;

private ArrayList<SingleItemModel> itemModels;


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


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




@Override
public void onBindViewHolder(@NonNull SingleItemRowHolder holder, int position) {
    SingleItemModel itemModel = itemModels.get(position);
    holder.tvTitle.setText(itemModel.getWord());
    holder.mitemImage.setImageResource(itemModel.getImage());
}


@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);
        //Intent to start next activity
        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();
                //passing data to Tab1Fragment

                mContext.startActivity(intent1);
            }
        });
        itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                Toast.makeText(mContext,tvTitle.getText(), Toast.LENGTH_SHORT).show();
                mContext.startActivity(intent);

                //appearing animation
                activity.overridePendingTransition(R.anim.bottom_in, R.anim.fade_in_right);
                return true;
            }
        });
        }
    }
}

MainActivity2 is hosting 3 Tabs, in one of them i need to receive it too, because i declared all tabs Fragment and every tab's Fragment's function is like MainActivity 's adapter (SectionDataAdapter) that it is going to show ArrayLists' Items. Which means i have to receive the ArrayList of MainActivity in all Fragments as well.

here is my Tab1Fragment:

public class Tab1Fragment extends Fragment {
public Tab1Fragment() {
    // Required empty public constructor
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.tab1_fragment, container, false);
    return view;
}
  }

and of course my RecyclerViewAdapter that shows ArrayLaists' Items

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


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



@NonNull
@Override
public MyViewHolder0 onCreateViewHolder(@NonNull 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(@NonNull 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);
                // passing data to the book activity

                //intent.putExtra("key_Word",value);
                intent.putExtra("Word",mData.get(position).getWord());
                intent.putExtra("Pronunciation",mData.get(position).getPronunciation());
                intent.putExtra("Examples&Explanation",mData.get(position).getExamples());
                //intent.putExtra("Definition",mData.get(position).getDefinition());
                intent.putExtra("WordImage",mData.get(position).getImage());
                // start the activity
                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);
        }
    }



}

somebody help me..!


回答1:


you need use Intent in order to send data to another activity

@Override
  public void onClick(View v) {

    Book book = new Book(mBkTitle.getText().toString(),
            mBkAuthor.getText().toString());

    Intent intent = new Intent(MainActivity.this, BookActivity.class);
    intent.putExtra("Book", book);
    startActivity(intent);
  }

data need to extends from Parcelable

public class Book implements Parcelable {
  // book basics
  private String title;
  private String author;
}

use http://www.jsonschema2pojo.org/ to help you make class parcelable.

Check this tutorial

https://code.tutsplus.com/tutorials/how-to-pass-data-between-activities-with-android-parcelable--cms-29559



来源:https://stackoverflow.com/questions/52954833/pass-mainactivitys-data-to-tab-fragment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!