gpt4 book ai didi

java - 如何从适配器访问 arrayList 到 Activity ?

转载 作者:太空狗 更新时间:2023-10-29 15:40:34 24 4
gpt4 key购买 nike

我在适配器中创建了一个方法。在这种方法中,我有一个数组列表。我想在 Activity 中访问此 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);
}
}

我想通过适配器的邀请方法访问邀请数组列表。

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

我想从适配器获取邀请数组列表到 sendInvite ImageView 的 onClickListener。

我该怎么做?谢谢..

最佳答案

在您的适配器中编写一个 get 方法,然后从您的 Activity 中调用它。

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

在您的 Activity 中,您可以获得这样的内容,yourAdapterObject.getArrayList();

关于java - 如何从适配器访问 arrayList 到 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39845955/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com