我在適配器中創建了一種方法。在這個方法中,我有一個數組列表。我想在一個活動中訪問這個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);
}
}
我想從一個適配器來sendInvite的onClickListener圖像視圖獲取邀請數組列表。
我該怎麼做?謝謝..
使的ArrayList公共裏面適配器和簡單地通過適配器對象訪問數組列表,同時執行的onclick – Aditi
'在這個方法中我有一個arraylist'。那arraylist是在你的適配器類中。就像方法一樣。 – greenapps