当前位置: 首页 > 知识库问答 >
问题:

通过适配器传递MainActivity的数据

公良骁
2023-03-14
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);


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

又如何接收?谁来帮帮我。

共有1个答案

田嘉澍
2023-03-14

您的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");
}
 类似资料:
  • 有什么简单的方法可以实现这一点吗? 这是我导航到路线的代码 这是我的路由模块 基本上,我希望执行与CreateAlbum组件是当前组件的子组件相同的操作,在这种情况下,我将使用@Input()

  • 问题内容: 我正在使用PhantomJS page.evaluate()进行抓取。我的问题是我传递到Webkit页面的代码是沙盒化的,因此无法访问我的主要幻象脚本的变量。这使得很难使抓取代码通用。 如何将参数推入页面? 问题答案: 我遇到了确切的问题。可以使用一些技巧,因为它也可以接受字符串。 有几种方法可以执行此操作,但是我使用了一个称为的包装器,该包装器接受其他参数以传递给必须在Webkit端

  • 当我点击按钮时: -迭代所有tr并将其所有输入的名称收集到数组(这已经完成了) - 我还从输入和触发名称的两个文本中获取数据 - 通过ajax将所有(一个数组和两个文本)发送到asp.net.cs(不起作用)

  • 问题内容: 我有两个标签,Label1和Label2。我想通过创建两个标签的UITTapRecognizer创建UITTapRecognizer,以使用传递参数的选择器来调用同一个函数,从而使打印出哪个标签的功能单一。下面是完成此操作的漫长方法,虽然比较麻烦,但可以正常工作。如果我知道如何将参数(Int)传递给选择器,那将是一个更干净的选择。 有没有办法修改选择器方法,以便我可以做类似的事情 问题

  • 我正在使用gradle credentials插件将用户和密码数据库传递给liquibase插件。另外,我使用spring data repositories和hibernate作为数据层,我希望传递给它与liquibase插件相同的配置。有一种通过gradle传递凭据的方法(我不想创建application.properties文件,因为凭据已经存储在凭据插件上)? 我正在使用以下代码向Liqu