0

Hello I just started the Android and I do not understand how to put more TextView in a RecyclerView I have already seen this solution but I do not understand: How to create RecyclerView with multiple view type?

I tried to make a table with multiple dimensions but it did not work.

Adapter:

public class DeviceRecyclerView extends RecyclerView.Adapter<DeviceRecyclerView.ViewHolder> {

    private String[]data;
    public DeviceRecyclerView(String[]data){
        this.data = data;

    }
    @Override
    public DeviceRecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.device_list_row, parent , false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        String name = data[position];
        String rssi = data[position];
        String uuid = data[position];

        holder.DeviceNameTextView.setText(name);
        holder.DeviceUUIDTextViewValue.setText(rssi);
        holder.DeviceRSSIVTextViewValue.setText(uuid);


    }

    @Override
    public int getItemCount() {
        return data.length;
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView DeviceNameTextView;
        TextView DeviceUUIDTextViewValue;
        TextView DeviceRSSIVTextViewValue;

        public ViewHolder (View itemView){
            super(itemView);
            DeviceNameTextView = itemView.findViewById(R.id.DeviceNameTextView);
            DeviceUUIDTextViewValue = itemView.findViewById(R.id.DeviceUUIDTextViewValue);
            DeviceRSSIVTextViewValue = itemView.findViewById(R.id.DeviceRSSIVTextViewValue);
        }
    }

MainActivity:

RecyclerView deviceList = (RecyclerView)findViewById(R.id.recycler_view_NoPaired);
        deviceList.setLayoutManager(new LinearLayoutManager(this));
        String[]names = {"Samsung64656"};
        String[]rssi = {"ezez"};
        String[]uuid = {"08:90:e5:90"};
        deviceList.setAdapter(new DeviceRecyclerView(names));

My XML.

    <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/frameLayout"
    android:layout_width="match_parent"
    android:layout_height="180dp"
    android:gravity="center_vertical"
    tools:layout_editor_absoluteX="30dp"
    tools:layout_editor_absoluteY="81dp">
    <View
        android:id="@+id/ColoredRect"
        android:layout_width="10dp"
        android:layout_height="160dp"
        android:layout_marginBottom="16dp"

        android:layout_marginTop="16dp"
        android:background="#E27F26"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/DeviceNameTextView"
        android:layout_width="133dp"
        android:layout_height="24dp"
        android:layout_marginStart="20dp"
        android:layout_marginTop="16dp"
        android:text="@string/device_name_label"
        android:textColor="@color/TextColor"
        android:textSize="18sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/DeviceUUIDTv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="20dp"
        android:layout_marginTop="8dp"
        android:text="@string/deviceUUIDTextView"
        android:textColor="@color/TextColor"
        android:textSize="12sp"
        app:layout_constraintBottom_toTopOf="@+id/DeviceUUIDTextViewValue"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/DeviceNameTextView" />

    <TextView
        android:id="@+id/DeviceUUIDTextViewValue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="20dp"
        android:layout_marginTop="14sp"
        android:text=""
        android:textColor="@color/TextColor"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/DeviceUUIDTv" />

    <TextView
        android:id="@+id/rssiLabelTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="20dp"
        android:layout_marginTop="16dp"
        android:text="@string/deviceRSSITextView"
        android:textColor="@color/TextColor"
        android:textSize="12sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/DeviceUUIDTextViewValue" />

    <TextView
        android:id="@+id/DeviceRSSIVTextViewValue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="20dp"
        android:layout_marginTop="14sp"
        android:text=""
        android:textColor="@color/TextColor"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/rssiLabelTextView" />


</android.support.constraint.ConstraintLayout>

Please explain me clearly how to do it. I am French sorry for spelling errors.

Tanveer Munir
  • 1,956
  • 1
  • 12
  • 27
Killian
  • 73
  • 1
  • 9

1 Answers1

1

You just need to add some textView in you'r XML row (named device_list_row.xml), and init their in your adapter, like you have already did with 3 textview.

But's more easy with Object than use array of string, for data. for example in you'r case , make Device class object

Device.java
public class Device {
        String name;
        String rssi;
        String uuid;
        public String getName() {
                return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getRssi() {
            return rssi;
        }

        public void setRssi(String rssi) {
            this.rssi = rssi;
        }

        public String getUuid() {
            return uuid;
        }

        public void setUuid(String uuid) {
            this.uuid = uuid;
        }


}

And adapter code ->

public class DeviceRecyclerView extends RecyclerView.Adapter<DeviceRecyclerView.ViewHolder> {

private ArrayList<Device> deviceList;
public DeviceRecyclerView(Arraylist<Device> deviceList){
    this.deviceList= deviceList;

}
@Override
public DeviceRecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    View view = inflater.inflate(R.layout.device_list_row, parent , false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    Device device = deviceList.get(position);

    holder.deviceNameTextView.setText(device.getName());
    holder.deviceUUIDTextViewValue.setText(device.getUuid());
    holder.deviceRSSIVTextViewValue.setText(device.getRssi());



}

@Override
public int getItemCount() {
    return deviceList.length;
}

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
    TextView deviceNameTextView, deviceUUIDTextViewValue, deviceRSSIVTextViewValue;

    public ViewHolder(View itemView)  {

        super(itemView);
        itemView.setOnClickListener(this);
        deviceNameTextView = itemView.findViewById(R.id.DeviceNameTextView);
        deviceUUIDTextViewValue = itemView.findViewById(R.id.DeviceUUIDTextViewValue);
        deviceRSSIVTextViewValue = itemView.findViewById(R.id.DeviceRSSIVTextViewValue);
    }

    @Override
    public void onClick(View v) {
        Log.i("DEBUG","Item RecyclerView Cliqué");
    }
}

With that, you init your Adapter with list of Device Object, who can have any variable has you want without need to pass more array or data to Adapter:-)

And for make a new Device, and use it in RV

Device device = new Device();
device.setName("Device Name 01");
device.setUuid("uuid value");
device.SetRssi("rssi value");

ArrayList<Device> deviceList = new ArrayList<>();
deviceList.add(device);

//Init adapter and fill it
DeviceRecyclerView deviceRecyclerView = new DeviceRecyclerView(deviceList);


LinearLayoutManager llm = new LinearLayoutManager(getContext());
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(llm);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(deviceRecyclerView);
Killian
  • 73
  • 1
  • 9
Benjamin
  • 401
  • 1
  • 3
  • 16
  • when do I make the relationship with my XML device_list_row.xml? – Killian Jan 23 '19 at 15:58
  • and i dont understand what is this "DeviceRecyclerView deviceRecyclerView = new DeviceRecyclerView(deviceList);" – Killian Jan 23 '19 at 16:36
  • The relationship with view are in the onCreateViewHolder, as you can see you init the view and return it, and you get it back in view holder class where you bind data to text view – Benjamin Jan 23 '19 at 16:38
  • I do not understand, my RecycleView is not showing. – Killian Jan 23 '19 at 16:46
  • I forgot to edit the return of getItemCount, see the updated amazed if you don’t change that , try to use debug to see if your array are correctly filed? If getItemCount are not correct the rv won’t show data because the number of row are based on this count – Benjamin Jan 23 '19 at 16:48
  • return deviceList.length; Error: Cannot resolve symbol 'lengh'. i tried with size(); but it dosn't work – Killian Jan 23 '19 at 16:58
  • Ooops excuse moi, that’s .size() correct, try to run in debug and put breakpoint after fill adapter and see if he s correctly filed – Benjamin Jan 23 '19 at 16:59
  • Where is the fill adapter? Sorry to ask as many questions. And to fill the RecyclerView, you do not have to use a "setAdapter();"? – Killian Jan 24 '19 at 08:32
  • You fill the adapter when you init it with deviceList List of Device. See my updated answer for the missing code about recyclerView :-) – Benjamin Jan 24 '19 at 08:44
  • LinearLayoutManager llm = new LinearLayoutManager(getContex());; Error: Cannot resolve symbol 'getContext'. I have this error now Maybe i can use LinearLayoutManager(this); ??? – Killian Jan 24 '19 at 08:56
  • for this error you just need to pass context as param in LinearLayoutManager. I'm happy if that's help you :-) you can put my answer as correct Answer if that help you :p – Benjamin Jan 24 '19 at 09:06
  • now I wish I could click on this list, would you have an idea to orient me? – Killian Jan 24 '19 at 13:44
  • See the update answer, it should be something like that, just put OnClickListener on layout of any row – Benjamin Jan 24 '19 at 13:48
  • I tested another way, and it worked. see the update :) thank you for responding so quickly – Killian Jan 24 '19 at 14:26
  • You'r update are better than mine for clickListener haha, pas de soucis avec plaisir ! :-) – Benjamin Jan 24 '19 at 14:29
  • Thank you so much. See you soon on a new problem! :p – Killian Jan 25 '19 at 08:53