我有一个 recyclerview,其中显示一个字符串,其中包含 id、姓名和联系电话作为单个字符串。我想隐藏字符串中的 id。我使用以下代码将字符串添加到数组列表。我在 TextView 中显示连接的字符串,例如 (1-XYZ 0123456789)
private void showGuestDetails(String response) {
Guest_info guest_info = new Guest_info(response);
guest_info.ParseGuestInfo();
String guestid[] = guest_info.guest_id;
for (int i = 0; i < guestid.length; i++) {
list.add(guest_info.guest_id[i] + "-" + guest_info.guest_name[i] + " " + guest_info.guest_mobile[i]);
}
}
这是我正在使用的适配器类:-
public class GuestAdapter extends RecyclerView.Adapter<GuestAdapter.GuestViewHolder> {
private List<String> list_item ;
public Context mcontext;
private BookingActivity bookingActivity;
public GuestAdapter(List<String> list, Context context, BookingActivity bookingActivity) {
list_item = list;
mcontext = context;
this.bookingActivity =bookingActivity;
}
// Called when RecyclerView needs a new RecyclerView.ViewHolder of the given type to represent an item.
@Override
public GuestAdapter.GuestViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a layout
View view = LayoutInflater.from(parent.getContext()).inflate(
R.layout.list_item, null);
GuestViewHolder myViewHolder = new GuestViewHolder(view);
return myViewHolder;
}
// Called by RecyclerView to display the data at the specified position.
@Override
public void onBindViewHolder(final GuestViewHolder viewHolder, final int position ) {
viewHolder.guest_name.setText(list_item.get(position));
viewHolder.guest_name.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bookingActivity.setGuestText(list_item.get(position));
}
});
}
//Returns the total number of items in the data set hold by the adapter.
@Override
public int getItemCount() {
return list_item.size();
}
// initializes textview in this class
public static class GuestViewHolder extends RecyclerView.ViewHolder {
public TextView guest_name;
public GuestViewHolder(View itemLayoutView) {
super(itemLayoutView);
guest_name = (TextView) itemLayoutView.findViewById(R.id.guestName);
}
}
}
Is there a way by which I will be able to display everything after the hyphen and hide everything before the hyphen including the hyphen. Any help or suggestion is appreciated. Thank you.
最佳答案
你的情况很好,但是在字符串列表的地方,只需传递(Guest_info类)的完整对象列表, 并像这样更新你的适配器
public class GuestAdapter extends RecyclerView.Adapter<GuestAdapter.GuestViewHolder> {
//private List<String> list_item ;
private List<Guest_info> list_item ;
public Context mcontext;
private BookingActivity bookingActivity;
public GuestAdapter(List<Guest_info> list, Context context, BookingActivity bookingActivity) {
list_item = list;
mcontext = context;
this.bookingActivity =bookingActivity;
}
// Called when RecyclerView needs a new RecyclerView.ViewHolder of the given type to represent an item.
@Override
public GuestAdapter.GuestViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a layout
View view = LayoutInflater.from(parent.getContext()).inflate(
R.layout.list_item, null);
GuestViewHolder myViewHolder = new GuestViewHolder(view);
return myViewHolder;
}
// Called by RecyclerView to display the data at the specified position.
@Override
public void onBindViewHolder(final GuestViewHolder viewHolder, final int position ) {
Guest_info bean = list_item.get(position);
viewHolder.guest_name.setText(bean.getGuest_name());
viewHolder.guest_name.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bookingActivity.setGuestText(bean.getGuest_name());
}
});
}
//Returns the total number of items in the data set hold by the adapter.
@Override
public int getItemCount() {
return list_item.size();
}
// initializes textview in this class
public static class GuestViewHolder extends RecyclerView.ViewHolder {
public TextView guest_name;
public GuestViewHolder(View itemLayoutView) {
super(itemLayoutView);
guest_name = (TextView) itemLayoutView.findViewById(R.id.guestName);
}
}
}
这样你就可以显示任何你想要的数据,你不需要对字符串进行操作,并且可以获得完整的数据。
关于java - 如何在回收 View 中隐藏部分字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40798719/