通过适配器传递MainActivity的数据

时间:2018-10-22 19:09:12

标签: java android

我是Android和Java的新手,但在我的第一个应用程序中,我有点在做Play Store,这是您在Play Store上看到的第一件事,然后转到第二个活动,并在那里查看整个列表。我已经建立了水平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));
    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;
            }
        });
        }
    }
}

如何接收? 有人帮我。

5 个答案:

答案 0 :(得分:2)

您的SectionDataModel需要实现Parceble接口。如下所示,您可以实施-

public class Person implements Parcelable{
String name;
int age;
String sex;

public Person(String name, int age, String sex) {
    this.name = name;
    this.age = age;
    this.sex = sex;
} 
 public static final Creator<Person> CREATOR = new Creator<Person>() {
    @Override
    public Person createFromParcel(Parcel in) {
        return new BeanClass(in);
    }

    @Override
    public Person[] newArray(int size) {
        return new Person[size];
    }
};
@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(name);
    dest.writeInt(age);
    dest.writeString(sex);
}
}

将数据从适配器类传递到片段或活动-

@Override
    public void onClick(View v){
        Toast.makeText(v.getContext(), tvTitle.getText(), LENGTH_SHORT).show();
        //passing data to Tab1Fragment
        Bundle bundle = new Bundle();
        bundle.putParcelableArrayList("SectionDataModels", sectionDataModelList);
        intent1.putExtras(bundle);
        mContext.startActivity(intent1);
    }

在活动中接收SectionDataModelList-

ArrayList<SectionDataModel> listFromActivity =new ArrayList<>();

listFromActivity=this.getIntent().getExtras().getParcelableArrayList("SectionDataModels");

    if (listFromActivity1 != null) {

        Log.d("listis",""+listFromActivity1.toString());
    }

此外,您可以尝试通过这种方式接收意图数据-如果需要

Bundle bundle = getActivity().getIntent().getExtras();
model = bundle.getParcelable("SectionDataModels");

OR

Bundle bundle = this.getArguments();
if (bundle != null) {
    model = bundle.getParcelable("SectionDataModels");
}

答案 1 :(得分:0)

您可以将arraylist保留在应用程序类中,而不是将其保留在第一个活动中(因为在整个应用程序中都是相同的),然后只需使用以下命令将被单击项的位置从activty1传递给activity2 intent1.putExtra("position",position)

在活动2中,使用

获取职位
int position = getIntent().getIntExtra("position")

现在只需使用此位置从应用程序类数组列表中获取模型类对象即可。

答案 2 :(得分:0)

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

答案 3 :(得分:0)

这完全取决于列表的大小。

您还可以使用和数组列表创建一个可包裹的类,并将其发送到第二个活动。

[1] Android Class Parcelable with ArrayList

[2] Android: How to pass Parcelable object to intent and use getParcelable method of bundle?

答案 4 :(得分:0)

为了简化操作,您可以将其转换为json字符串格式,然后再次将其转换回您设置的模型对象列表,并以意图将其作为捆绑字符串传递,您可以使用GSON库可以轻松解析json格式和对象模型。