Sid Sid - 2 months ago 9
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?

Adapter:

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


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

Activity:

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


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

How can I do this? Thank you..

Answer

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