Sid Sid - 1 year ago 69
Java Question

How to access an arrayList from an adapter to an activity?

I have created one method in an adapter. In this method I have an arraylist. I want to access this arrayList in an activity. How can I do this?


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) {
name = (TextView) view.findViewById(;
checkBox = (CheckBox) view.findViewById(;


public InviteAdapter(Context context, ArrayList<Contact> contactArrayList) {
this.contactArrayList = contactArrayList;
this.mContext = context;


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

public void onBindViewHolder(MyViewHolder holder, int position) {
Contact contact = contactArrayList.get(position);;


public int getItemCount() {
return contactArrayList.size();

public void toggleContactsSelection( boolean isSelected ) {
for( Contact contact : contactArrayList ) {

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



I want to access invitation array list from invite method of an adapter.


public class InviteContactsActivity extends AppCompatActivity {

private RecyclerView mRecyclerView;
private InviteAdapter mAdapter;
private ArrayList<Contact> mContactList;
private ContactTableHelper mContactDb;
private ArrayList<Invitation> invitationArrayList;

protected void onCreate(Bundle savedInstanceState) {

final ImageView selectAll = (ImageView)findViewById(;

selectAll.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {

PopupMenu popup = new PopupMenu(InviteContactsActivity.this,selectAll);

popup.getMenuInflater().inflate(, popup.getMenu());

popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {

mAdapter.toggleContactsSelection(true); //Selects all contacts
return true;

ImageView sendInvites = (ImageView)findViewById(;

sendInvites.setOnClickListener(new View.OnClickListener() {
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(;

RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(InviteContactsActivity.this);
mRecyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
mRecyclerView.setItemAnimator(new DefaultItemAnimator());

mAdapter = new InviteAdapter(InviteContactsActivity.this, mContactList);

I want to get invitation arraylist from an adapter to onClickListener of sendInvite image view.

How can I do this? Thank you..

Answer Source

Write a get method inside your adapter then call it from your activity.

    public ArrayList<Object> getArrayList(){
    return yourArrayList;

Inside your activity you can get this like, yourAdapterObject.getArrayList();

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download