猿问

通过适配器传递 MainActivity 的数据

我是 android 和 java 的新手,但在我的第一个应用程序中,我正在做 Play 商店,你在 Play 商店看到的第一件事,然后转到第二个活动并在那里看到整个列表。我已经构建了水平 ArrayList 并且我也成功构建GridView了第二个活动,我的 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));

}

  }

而我SectionDataAdapter只有从它我可以将数据发送到第二个 MainActivity 因为如果我从MainActivity它自己做它返回 Null


蓝山帝景
浏览 129回答 3
3回答

白猪掌柜的

您的 SectionDataModel 需要实现 Parceble 接口。如下所示,您可以实施 -public class Person implements Parcelable{String name;int age;String sex;public Person(String name, int age, String sex) {&nbsp; &nbsp; this.name = name;&nbsp; &nbsp; this.age = age;&nbsp; &nbsp; this.sex = sex;}&nbsp;&nbsp;public static final Creator<Person> CREATOR = new Creator<Person>() {&nbsp; &nbsp; @Override&nbsp; &nbsp; public Person createFromParcel(Parcel in) {&nbsp; &nbsp; &nbsp; &nbsp; return new BeanClass(in);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public Person[] newArray(int size) {&nbsp; &nbsp; &nbsp; &nbsp; return new Person[size];&nbsp; &nbsp; }};@Overridepublic int describeContents() {&nbsp; &nbsp; return 0;}@Overridepublic void writeToParcel(Parcel dest, int flags) {&nbsp; &nbsp; dest.writeString(name);&nbsp; &nbsp; dest.writeInt(age);&nbsp; &nbsp; dest.writeString(sex);}}将数据从您的适配器类传递到片段或活动 -@Override&nbsp; &nbsp; public void onClick(View v){&nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(v.getContext(), tvTitle.getText(), LENGTH_SHORT).show();&nbsp; &nbsp; &nbsp; &nbsp; //passing data to Tab1Fragment&nbsp; &nbsp; &nbsp; &nbsp; Bundle bundle = new Bundle();&nbsp; &nbsp; &nbsp; &nbsp; bundle.putParcelableArrayList("SectionDataModels", sectionDataModelList);&nbsp; &nbsp; &nbsp; &nbsp; intent1.putExtras(bundle);&nbsp; &nbsp; &nbsp; &nbsp; mContext.startActivity(intent1);&nbsp; &nbsp; }在活动中接收 SectionDataModelList -ArrayList<SectionDataModel> listFromActivity =new ArrayList<>();listFromActivity=this.getIntent().getExtras().getParcelableArrayList("SectionDataModels");&nbsp; &nbsp; if (listFromActivity1 != null) {&nbsp; &nbsp; &nbsp; &nbsp; Log.d("listis",""+listFromActivity1.toString());&nbsp; &nbsp; }此外,您可以尝试通过这种方式接收意图数据 - 如果您需要Bundle bundle = getActivity().getIntent().getExtras();model = bundle.getParcelable("SectionDataModels");ORBundle bundle = this.getArguments();if (bundle != null) {&nbsp; &nbsp; model = bundle.getParcelable("SectionDataModels");}

慕沐林林

您可以将 ArrayList 设为公共静态,然后将其导入到第二个活动中

30秒到达战场

您可以将 arraylist 保留在您的应用程序类中而不是第一个活动中(因为它在整个应用程序中都是相同的),然后只需使用&nbsp;intent1.putExtra("position",position).在活动 2 中使用int&nbsp;position&nbsp;=&nbsp;getIntent().getIntExtra("position")现在只需使用此位置从应用程序类数组列表中获取模型类对象。
随时随地看视频慕课网APP

相关分类

Java
我要回答