Java 通过主要活动';将数据添加到选项卡片段
我不熟悉android和java,但在我的第一个应用程序中,我正在做play store,这是你在play store上看到的第一件事,然后转到第二个活动,在那里看到整个列表。我已经构建了水平ArrayList,并且成功地构建了第二个活动的Java 通过主要活动';将数据添加到选项卡片段,java,android,android-fragments,tabs,Java,Android,Android Fragments,Tabs,我不熟悉android和java,但在我的第一个应用程序中,我正在做play store,这是你在play store上看到的第一件事,然后转到第二个活动,在那里看到整个列表。我已经构建了水平ArrayList,并且成功地构建了第二个活动的Cardview,我的ArrayList是静态的,我的意思是它没有使用任何服务器 我的问题是如何通过位于其中的适配器将MainActivity的数据发送到MainActivity2 以下是我的主要活动,我的数据位于此: public class MainAct
Cardview
,我的ArrayList是静态的,我的意思是它没有使用任何服务器
我的问题是如何通过位于其中的适配器将MainActivity
的数据发送到MainActivity2
以下是我的主要活动,我的数据位于此:
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);
}
}
MainActivity2承载了3个选项卡,其中一个我也需要接收,因为我声明了所有选项卡片段,每个选项卡片段的函数都像MainActivity的适配器(SectionDataAdapter),它将显示ArrayList的项。这意味着我还必须在所有片段中接收MainActivity的ArrayList
这是我的tab1片段:
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;
}
}
当然,我的RecycleServiceAdapter
显示了ArrayList的项
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);
}
}
}
公共类RecycleServiceAdapter扩展了RecyclerView.Adapter{
私有上下文;
私有ArrayList mData;
//构造器
公共RecycleServiceAdapter(上下文mContext、ArrayList mData){
this.mContext=mContext;
this.mData=mData;
}
@非空
@凌驾
public MyViewHolder0 onCreateViewHolder(@NonNull ViewGroup父级,int-viewType){
视图;
LayoutInflater mInflater=LayoutInflater.from(mContext);
view=mInflater.充气(R.layout.cardview\u item\u book,父项,false);
返回新的MyViewHolder0(视图);
}
@凌驾
BindViewHolder上的公共无效(@NonNull MyViewHolder0 holder,最终int位置){
holder.tv\u book\u title.setText(mData.get(position.getWord());
holder.img_book_缩略图.setImageResource(mData.get(position.getImage());
holder.cardView.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
意向意向=新意向(mContext,Book_Activity.class);
//将数据传递给book活动
//意图。putExtra(“关键字”,值);
intent.putExtra(“Word”,mData.get(position.getWord());
intent.putExtra(“发音”,mData.get(position.get发音());
intent.putExtra(“示例和解释”,mData.get(position.getExamples());
//intent.putExtra(“定义”,mData.get(position.getDefinition());
intent.putExtra(“WordImage”,mData.get(position.getImage());
//开始活动
mContext.startActivity(意图);
}
});
}
@凌驾
public int getItemCount(){
返回mData.size();
}
公共静态类MyViewHolder0扩展了RecyclerView.ViewHolder{
TextView电视、书籍、书名;
ImageView img_book_缩略图;
卡德维尤;
公共MyViewHolder0(查看项目视图){
超级(项目视图);
tv\u book\u title=itemView.findviewbyd(R.id.book\u title\u id);
img\u book\u缩略图=itemviewbyd(R.id.book\u img\u id);
cardView=itemView.findViewById(R.id.cardView\U id);
}
}
}
谁来帮帮我 您需要使用Intent将数据发送到另一个活动
@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扩展
public class Book implements Parcelable {
// book basics
private String title;
private String author;
}
用于帮助您将类包裹起来
检查本教程
我知道如何实现parcelable,但我有一个问题,那就是如何将数据的ArraySy传递给TabLayout Fragment!!
public class Book implements Parcelable {
// book basics
private String title;
private String author;
}