我在适配器中创建了一个方法。在这种方法中,我有一个arraylist。我想在一个活动中访问这个arrayList。我怎么能这样做?
适配器:
public class InviteAdapter extends RecyclerView.Adapter<InviteAdapter.MyViewHolder> {
private ArrayList<Contact> contactArrayList;
private Context mContext;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView name;
private CheckBox checkBox;
public MyViewHolder(View view) {
super(view);
name = (TextView) view.findViewById(R.id.textContactName);
checkBox = (CheckBox) view.findViewById(R.id.checkBox);
}
}
public InviteAdapter(Context context, ArrayList<Contact> contactArrayList) {
this.contactArrayList = contactArrayList;
this.mContext = context;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.invite_contact_item, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Contact contact = contactArrayList.get(position);
holder.name.setText(contact.getmFullName());
holder.checkBox.setChecked(contact.getSelected());
}
@Override
public int getItemCount() {
return contactArrayList.size();
}
public void toggleContactsSelection( boolean isSelected ) {
for( Contact contact : contactArrayList ) {
contact.setSelected(isSelected);
invite(contact);
}
notifyDataSetChanged(); // OR you can use notifyItemRangeChanged - which ever suits your needs
}
public void invite(Contact contact)
{
ArrayList<Invitation> invitationArrayList = new ArrayList<>();
SharedPreferences sharedpreferences = mContext.getSharedPreferences("UserId", Context.MODE_PRIVATE);
String mUserId = sharedpreferences.getString("userId","");
DateFormat df = new SimpleDateFormat("EEE, d MMM yyyy, HH:mm", Locale.ENGLISH);
String date = df.format(Calendar.getInstance().getTime());
Invitation invitation = new Invitation();
invitation.setSender_id(mUserId);
invitation.setDate(date);
invitation.setInvitee_no(contact.getmMobileNo());
invitation.setStatus("0");
invitation.setUser_name(contact.getmUserName());
invitationArrayList.add(invitation);
}
}
我想从适配器的邀请方法访问邀请数组列表。
的活动:
public class InviteContactsActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private InviteAdapter mAdapter;
private ArrayList<Contact> mContactList;
private ContactTableHelper mContactDb;
private ArrayList<Invitation> invitationArrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_invite_contacts);
final ImageView selectAll = (ImageView)findViewById(R.id.imageMenu);
selectAll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
PopupMenu popup = new PopupMenu(InviteContactsActivity.this,selectAll);
popup.getMenuInflater().inflate(R.menu.pop_up_menu, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
mAdapter.toggleContactsSelection(true); //Selects all contacts
return true;
}
});
popup.show();
}
});
ImageView sendInvites = (ImageView)findViewById(R.id.imageSelect);
sendInvites.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Gson gson=new Gson();
String toServer = gson.toJson(
Collections.singletonMap("invitations", invitationArrayList)
);
new SendMultipleInvitesAsyncTask(InviteContactsActivity.this).execute(toServer);
}
});
mContactDb = new ContactTableHelper(InviteContactsActivity.this);
mContactList = mContactDb.getAllContacts();
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mRecyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(InviteContactsActivity.this);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mAdapter = new InviteAdapter(InviteContactsActivity.this, mContactList);
mRecyclerView.setAdapter(mAdapter);
}
}
我想从适配器获取邀请arraylist到sendInvite图像视图的onClickListener。
我该怎么做?谢谢..
答案 0 :(得分:8)
在适配器中编写一个get方法,然后从您的活动中调用它。
public ArrayList<Object> getArrayList(){
return yourArrayList;
}
在您的活动中,您可以使用yourAdapterObject.getArrayList();
答案 1 :(得分:1)
您还可以使用interface将数据从adpater类发送到活动。 它在下面的链接中有很好的描述。
答案 2 :(得分:0)
创建ivitearraylist作为公众,并通过adpater访问活动。
ArrayList<Invitation> invitationArrayList = new ArrayList<>();
public void invite(Contact contact)
{
SharedPreferences sharedpreferences = mContext.getSharedPreferences("UserId", Context.MODE_PRIVATE);
String mUserId = sharedpreferences.getString("userId","");
DateFormat df = new SimpleDateFormat("EEE, d MMM yyyy, HH:mm", Locale.ENGLISH);
String date = df.format(Calendar.getInstance().getTime());
Invitation invitation = new Invitation();
invitation.setSender_id(mUserId);
invitation.setDate(date);
invitation.setInvitee_no(contact.getmMobileNo());
invitation.setStatus("0");
invitation.setUser_name(contact.getmUserName());
invitationArrayList.add(invitation);
}
活动代码: 访问invitationArrayList作为adapter.invitationArrayList。
ImageView sendInvites = (ImageView)findViewById(R.id.imageSelect);
sendInvites.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Gson gson=new Gson();
String toServer = gson.toJson(
Collections.singletonMap("invitations", mAdpater.invitationArrayList)
);
new SendMultipleInvitesAsyncTask(InviteContactsActivity.this).execute(toServer);
}
});
它可能对您有帮助:))
快乐编码:)