将MainActivity的数据传递到Tab片段 [英] pass MainActivity's data to Tab Fragment

查看:44
本文介绍了将MainActivity的数据传递到Tab片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Android和Java的新手,但在我的第一个应用程序中,我有点在做Play Store,这是您在Play Store上看到的第一件事,然后转到第二个活动,并在那里查看整个列表.我已经建立了水平ArrayList,并且也成功建立了第二个活动的 Cardview ,我的ArrayList是静态的,这意味着它没有使用任何服务器.

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.

我的问题是如何通过位于其中的适配器将 MainActivity 的数据发送到 MainActivity2 .

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);


}
  }

还有我的 SectionDataAdapter ,它只能通过它将数据发送到Second MainActivity,因为如果我从 MainActivity 本身进行操作,它会返回Null:

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正在托管3个选项卡,在其中一个中我也需要接收它,因为我声明了所有选项卡Fragment,并且每个选项卡的Fragment的功能都类似于MainActivity的适配器(SectionDataAdapter),它将显示ArrayLists的项目.这意味着我还必须在所有片段中都收到MainActivity的ArrayList.

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.

这是我的Tab1Fragment:

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;
}
  }

当然还有我的 RecyclerViewAdapter ,它显示了ArrayLaists的物品

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);
        }
    }



}

有人帮助我..!

推荐答案

您需要使用 Intent 才能将数据发送到另一个活动

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);
  }

数据需要从Parcelable扩展

data need to extends from Parcelable

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

使用 http://www.jsonschema2pojo.org/帮助您使类可拆分.

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

查看本教程

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

这篇关于将MainActivity的数据传递到Tab片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆