我是Android和Java的新手,但在我的第一个应用程序中,我有点在做Play Store,这是您在Play Store上看到的第一件事,然后转到第二个活动,并在那里查看整个列表。我已经建立了水平ArrayList,并且也成功建立了第二个活动的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);
}
}
我的SectionDataAdapter
只能从其中发送数据到Second MainActivity,因为如果我从MainActivity
本身进行操作,它会返回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。
这是我的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的物品
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);
}
}
}
有人帮助我..!
答案 0 :(得分:0)
您需要使用 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;
}
使用http://www.jsonschema2pojo.org/帮助您使类可包裹。
查看本教程