谁能给我一个例子hoe把保存的数据从活动传递到回收器适配器。我知道如何使用putExtra等将数据从回收器适配器传递到活动。
好心帮帮我。百万先谢!
活动A中的代码(Add address按钮可用并且存在回收器视图)
public class ProfileManageAdressFragment extends AppCompatActivity {
RecyclerView recyclerView;
ProfileManageAddressRecyclerAdapter adapter;
ArrayList<ProfileManageAddressGetterSetter> reviews;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_profile_manage_adress);
Button addAddress = findViewById(R.id.addNewAddress);
reviews = new ArrayList<>();
addAddress.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Clicked", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(ProfileManageAdressFragment.this, AddNewAddress.class);
startActivity(intent);
}
});
}
}
reviews.add(new ProfileManageAddressGetterSetter("Customer Name", "address line 1", "address line 2"));
recyclerView = findViewById(R.id.addressRecyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(ProfileManageAdressFragment.this));
adapter = new ProfileManageAddressRecyclerAdapter(this, reviews);
recyclerView.setAdapter(adapter);
public class ProfileManageAddressRecyclerAdapter extends RecyclerView.Adapter<ProfileManageAddressRecyclerAdapter.ViewHolder> {
private ArrayList<ProfileManageAddressGetterSetter> mDataset = new ArrayList<>();
private Context context;
public static class ViewHolder extends RecyclerView.ViewHolder {
private TextView customer_name, address_one, address_two;
private Button edit, remove;
public ViewHolder(View v) {
super(v);
customer_name = (TextView) v.findViewById(R.id.customerName);
address_one = (TextView) v.findViewById(R.id.addressLineOne);
address_two = v.findViewById(R.id.addressLineTwo);
}
}
public ProfileManageAddressRecyclerAdapter(View.OnClickListener profileManageAdressFragment, ArrayList<ProfileManageAddressGetterSetter> dataset) {
mDataset.clear();
mDataset.addAll(dataset);
}
@Override
public ProfileManageAddressRecyclerAdapter.ViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_manage_address, parent, false);
ProfileManageAddressRecyclerAdapter.ViewHolder vh = new ProfileManageAddressRecyclerAdapter.ViewHolder(view);
return vh;
}
@Override
public void onBindViewHolder(@NonNull ProfileManageAddressRecyclerAdapter.ViewHolder holder, int position) {
ProfileManageAddressGetterSetter profileManageAddressGetterSetter = mDataset.get(position);
holder.address_one.setText(profileManageAddressGetterSetter.getAddress_line_1());
holder.address_two.setText(profileManageAddressGetterSetter.getGetAddress_line_2());
holder.customer_name.setText(profileManageAddressGetterSetter.getContractor_name());
}
@Override
public int getItemCount() {
return mDataset.size();
}
}
public class AddNewAddress extends AppCompatActivity {
private EditText customer_name, address_one, address_two;
private TextView cancel;
private Button add_address;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_new_address);
customer_name = findViewById(R.id.customerName);
address_one = findViewById(R.id.addressOne);
address_two = findViewById(R.id.addressTwo);
add_address = findViewById(R.id.addAddress);
cancel = findViewById(R.id.completeCancel);
String cancel_text = "Cancel";
SpannableString spanableObject = new SpannableString(cancel_text);
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View widget) {
Toast.makeText(AddNewAddress.this, "Clicked", Toast.LENGTH_SHORT).show();
}
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setColor(Color.BLUE);
}
};
spanableObject.setSpan(clickableSpan, 0, 6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
cancel.setText(spanableObject);
cancel.setMovementMethod(LinkMovementMethod.getInstance());
final ProfileManageAdressFragment profileManageAdressFragment = new ProfileManageAdressFragment();
add_address.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(AddNewAddress.this, ProfileManageAdressFragment.class);
startActivity(intent);
}
});
}
private void setFragment(android.support.v4.app.Fragment fragment) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_frame, fragment).commit();
}
}
请检查我更新的回收器适配器。当我运行时,0将显示在文本区域中,如所附图像所示。我对Android系统是新手。请以身作则。
我最终通过使用ActivityResult实现了我的目标。现在我能够将数据从Activity传递到CardView。
解决方案:当在活动A中单击button时,我使用startResultActivity()启动活动。后来,当活动B i触发。最终用户输入数据,并使用putExtra()传递数据,一旦在活动B中单击save按钮,接下来在活动B和finish()中设置setResult()。
最后,我在活动A中定义onActivityResult()以获得结果。很好用!!
我的第二个:
特别是,当我使用这些行获取错误时: 我尝试了下面的代码: 并且获取常数大于0,并且显示Cart中的项数为1,但是每当我在代码获取问题中使用这一行时,从适配器到活动获取值是正确的方法吗? cartAdapter.java: logcat:
我有一个回收器适配器,我在其中为适配器中的项添加了。代码如下: 这里是片段代码,它处理从适配器到片段的值获取。 我正在尝试将和具有该的值从适配器传递到片段。但我的代码不行。我该怎么修好它?
我想把它从适配器传递到这个,这是我的适配器 这是我的适配器,它保存着RecycerView,我正在尝试获取考勤活动上查看的用户id RecycerView
因此,我尝试使用Recolyer视图和cursoradapter实现我的数据库,但我得到了错误。我不能很好地理解它们,所以我不知道是我没有正确地实现适配器,还是它是其他的东西。以下是java文件: mainactivity.java: 以下是回收器视图适配器: 最后是我得到的错误:
在此处输入图像描述 我有一份父母名单,每个人都有零个或多个孩子,我用回收视图给父母看。我想向家长展示每个回收人员视图项目的标题,下面是向所有孩子展示向孩子展示的最好方式是什么?是否适合在列表视图中显示孩子,或者为每个孩子添加动态视图,如果是的话,如何做到这一点?我已经附上了布局的图像,请检查我的意思,谢谢 在此处输入图像描述