在Recyclerview中显示字符串[]

时间:2018-04-04 09:25:10

标签: java android android-recyclerview

我正在尝试将String []数组“favWalks”添加到RecyclerView,但我没有添加每个元素,只是覆盖并显示最后一个元素。

我从调用Getter的Object获取数组然后遍历它将每个元素设置为TextView但是我只是覆盖了,有没有办法附加到RecyclerView?

我从Retrofit电话中得到以下回复。

{
    "_id": {
        "$oid": "5abeb626c2ef1603f406028e"
    },
    "email": "butlerpaul695@gmail.com",
    "completedWalks": [
        "Grand Canal Way"
    ],
    "favWalks": [
        "Grand Canal Way",
        "Sl? Na Finne - Sli Dhun na nGall",
        "Wicklow Way"
    ]
}

我的UserModel包含getter:

@SerializedName("email")
private String email;
@SerializedName("completedWalks")
private String[] completedWalks;
@SerializedName("favWalks")
private String[] favWalks;

public String getEmail() {
    return email;
}
public String[] getCompletedWalks() {
    return completedWalks;
}

public String[] getFavWalks() {
    return favWalks;
}

我的XML:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/user_chosen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="72dp"
android:orientation="horizontal"
android:padding="16dp">
<LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:orientation="vertical">

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:paddingRight="16dp"
        android:textColor="@color/input_login"
        android:textSize="16sp"
        android:contentDescription="Trail Name:"
        android:textStyle="bold" />
</LinearLayout>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="35dp"
    android:orientation="horizontal">

    <ImageButton
        android:id="@+id/deleteWalk"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:background="@drawable/white_border"
        android:src="@drawable/ic_delete"/>
</LinearLayout>

获取响应并调用适配器:

List<User> userList = response.body();
recyclerView.setAdapter(new UserWalksAdapter(userList,R.layout.list_item_fav,getApplicationContext()));

我的适配器:

public class UserWalksAdapter extends RecyclerView.Adapter<UserWalksAdapter.UserWalkHolder>
{
    private List<User> users;
    private int rowLayout;
    private Context context;

    public static class UserWalkHolder extends RecyclerView.ViewHolder
        implements  View.OnClickListener, View.OnLongClickListener {
        LinearLayout usersLayout;
        TextView trailTitle;
        ImageButton delete;
        private TrailAdapter.ItemClickListener mListener;

        public UserWalkHolder(final View v)
        {
            super(v);
            usersLayout = (LinearLayout) v.findViewById(R.id.user_chosen);
            trailTitle = (TextView) v.findViewById(R.id.title);
            delete = (ImageButton) v.findViewById(R.id.deleteWalk);
            v.setClickable(true);
            v.setFocusableInTouchMode(true);
            v.setOnClickListener(this);
            v.setOnLongClickListener(this);
        }

        public void setClickListener(TrailAdapter.ItemClickListener listener) {
            this.mListener = listener;
        }

        @Override public void onClick(View view) {
            mListener.onClickItem(getLayoutPosition());
        }

        @Override public boolean onLongClick(View view) {
            mListener.onLongClickItem(getLayoutPosition());
            return true;
        }
    }

    public interface ItemClickListener {
        void onClickItem(int pos);
        void onLongClickItem(int pos);
    }

    public UserWalksAdapter (List<User> users, int rowLayout, Context context)
    {
        this.users = users;
        this.rowLayout = rowLayout;
        this.context = context;
    }

    @Override
    public UserWalksAdapter.UserWalkHolder onCreateViewHolder ( ViewGroup parent, int viewType)
    {
        View view = LayoutInflater.from(parent.getContext()).inflate(rowLayout, parent, false);
        return new UserWalkHolder(view);
    }

    @Override
    public void onBindViewHolder(UserWalkHolder holder, int position)
    {
        //get array from User object
        String[] favWalks = users.get(position).getFavWalks();

        for (String s : favWalks)
        {
            Log.d("userWalksAdapter","TrailName:" + s);
            holder.trailTitle.setText(s);
        }
    }

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

谢谢。

如果它是一个解决方法,我可以查询我的数据库只返回数组:

{
    "favWalks": [
        "Grand Canal Way",
        "Sl? Na Finne - Sli Dhun na nGall",
        "Wicklow Way"
    ]
}

1 个答案:

答案 0 :(得分:1)

string array而不是List<User>传递给适配器试试这个

List<User> userList = response.body();
String[] fav=userList.getFavWalks();
recyclerView.setAdapter(new UserWalksAdapter(fav,R.layout.list_item_fav,getApplicationContext()));

并像这样更改适配器构造函数

public UserWalksAdapter (String[] fav, int rowLayout, Context context)
{