Killian
Killian

Reputation: 73

Multiple TextView in RecycleView. how to do that?

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.

Upvotes: 0

Views: 1498

Answers (1)

Benjamin
Benjamin

Reputation: 421

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

Upvotes: 1

Related Questions