Tripathee Gaurav Tripathee Gaurav - 5 months ago 18
Android Question

How to attach array in onBindViewHolder

I have returned this format of data

{
"stations": [
{
"get_off": "sahayoginagar",
"get_on": "kanti",
"station_list": [
"values1",
"values2",
"values3",
"values4",
"values5",
"values6",

],
"v_type": "micro"
}
]


I can get the value of stations , but how to get the value of station_list

I have used this logic inside onBindViewHolder but it only outputs last value!

for(String hello : station.getSt_List()){
holder.station_name.setText(hello);
}
}


I am very much beginner in Android and also in Programming! Can somebody help me!

This is my adapter class:
public class StationsRecycleViewAdapter extends RecyclerView.Adapter<StationsRecycleViewAdapter.StationViewHolder>{

List<Station> example;
List<Stations> stationList;
Stations station;
List links;
Stations ss;
Station s;

public class StationViewHolder extends RecyclerView.ViewHolder {
public TextView station_name;

public StationViewHolder(View itemView) {
super(itemView);
station_name = (TextView) itemView.findViewById(R.id.station_name);
}
}

public StationsRecycleViewAdapter(List<Stations> stationList){
this.stationList = stationList;
}

@Override
public StationViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

View itemView;

itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_station, parent,false);
return new StationViewHolder(itemView);
}

@Override
public void onBindViewHolder(StationViewHolder holder, int position) {

System.out.println("1");
//s = example.get(position);

station = stationList.get(position);

System.out.println("Found ASrray" + station.getSt_List());
System.out.println("Found or Not" + stationList.size() + station.getGet_on() + station.getGet_off());
System.out.println("2");
String str="";
for(String hello : station.getSt_List()){

// added \n here

System.out.println("3");
str+=hello+",\n";
}

// EDIT
System.out.println("4");
holder.station_name.setText(str);

}

@Override
public int getItemCount() {
return stationList.size();
}
}


Please take a look a this pic.. I want this kind of view

Result i got

for creating circle i have done this in XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">


<ImageView
android:id="@+id/circle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/circle"
android:layout_centerVertical="true"

/>



<TextView
android:id="@+id/station_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Sahayoginagar"
android:textStyle="italic"
android:textSize="19dp"
android:paddingLeft="30dp"

/>

</RelativeLayout>

Answer

Do it like this:

@Override
public void onBindViewHolder(StationViewHolder holder, int position) {

    System.out.println("1");

    station = stationList.get(position);

    System.out.println("Found ASrray" + station.getSt_List());
    System.out.println("Found or Not" + stationList.size() + station.getGet_on() + station.getGet_off());

    //MADE CHANGES HERE 

    String str="";
    for(String hello : station.getSt_List()){

        // added \n here ADDED CODE HERE FOR BULLET

        str+= Html.fromHtml("\u2022")+" "+ hello+",\n";
    }

    // EDIT

    holder.station_name.setText(str);



}

Not need of having a for loop remove it. I think this will help..!!