anup anup - 8 months ago 76
Java Question

How to hide part of a string in a recyclerview?

I have a recyclerview where I am displaying a string which contains a id,name and contact number as a single string.I would like to hide the id in the string.I am using the following code to add the string to the array list.I am displaying the concatenated string in a textview like (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]);

}
}


This is the adapter class that I am using:-

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.

Answer Source

try this

String[] separated = list.get(position).split("-");
separated[0]; // this will contain your ID eg 1
separated[1]; // this will contain your remaining string eg XYZ 0123456789

viewHolder.guest_name.setText(separated[1]);