如何在onBindViewHolder中附加数组

时间:2016-06-17 06:37:59

标签: android android-recyclerview

我已经退回了这种数据格式

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

            ],
            "v_type": "micro"
        }
    ]

我可以获得电台的价值,但是如何获得电台列表的值

我在onBindViewHolder中使用了这个逻辑,但它只输出了最后一个值!

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

我非常熟悉Android和编程!有人可以帮帮我!

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

创建圈子我已经用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>

3 个答案:

答案 0 :(得分:1)

这样做:

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



}

不需要使用for循环删除它。我想这会有所帮助.. !!

答案 1 :(得分:0)

你几乎就在那里。查看示例屏幕截图,您需要在屏幕顶部显示文本视图以显示电台常规信息。在此之下,您需要显示电台名称的RecyclerView。

解决方案将如下所示。

像这样修改你的适配器类。它不需要知道关于站对象本身的任何信息。您只需传递一个站名列表。

public class StationsRecycleViewAdapter extends RecyclerView.Adapter<StationsRecycleViewAdapter.StationViewHolder>{

    List<String> stationList;

    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<String> 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) {
        holder.station_name.setText(stationList.get(position));
    }

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

在您的呼叫活动/片段中,您将像这样实例化您的适配器并设置常规电台信息。

Stations station = stations.get(index); // get the station you want to display
textViewGeneralInfo.setText(String.format("Get up at %s and get off at %s", station.getGet_on() + station.getGet_off());
StationRecycleViewAdapter adapter = new StationRecycleViewAdapter(station.getSt_List());

recyclerView.setAdapter(适配器);

您的布局将如下所示。

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical">

    <TextView
        android:id="@+id/textViewGeneralInfo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

答案 2 :(得分:0)

为JSON创建一个POJO或MODEL类。 Go to this site.

解析List Model类中的数据集将它们传递给Model Constructor,然后在Model中完成解析和保存数据。在OnBindHolder中传递模型。从活动或片段传递的列表模型中获取数据。