从服务器

时间:2017-01-16 13:21:36

标签: java android android-recyclerview recycler-adapter

我的RecyclerView出了问题。我想在从服务器获取数据后更新卡(在RecyclerView内)。首先我认为获取或解析JSON数据或通过EventBus将数据发送到Fragment有问题,但是当我在Fragment的TextView中显示解析的对象时,一切都会显示出来。所以真正的问题是RecyclerView / Adapter。 我用一个空的ArrayList初始化了RecyclerView,但是何时以及如何更新RecyclerView的数据?我也在Stackoverflow上阅读了一些帖子,但我找不到合适的解决方案...

这是UserFragment

中的 onCreateView
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    // Inflate the layout for this fragment
    View mView = inflater.inflate(R.layout.fragment_users, container, false);
    mRecyclerView = (RecyclerView) mView.findViewById(R.id.rv_user);
    //set LayoutManager for View
    mLayoutManager = new LinearLayoutManager(getActivity());
    mRecyclerView.setLayoutManager(mLayoutManager);

    //set Adapter for View
    mAdapter = new UserAdapter(userList);
    mRecyclerView.setAdapter(mAdapter);
    Log.d(TAG, "onCreateView");
    return mView;
}

这是我的 UserAdapter

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

public static final String TAG = "UserAdapter";

private ArrayList<User> mUser;

public UserAdapter(ArrayList<User> user) {
    mUser = user;
}

public static class ViewHolder extends RecyclerView.ViewHolder {
    public CardView cv;
    public TextView user_name;
    public TextView user_lastName;

    public TextView user_city;
    public ViewHolder(View itemView) {
        super(itemView);
        cv = (CardView) itemView.findViewById(R.id.rv_user);
        user_name = (TextView) itemView.findViewById(R.id.tv_user_name);
        user_lastName = (TextView) itemView.findViewById(R.id.tv_user_lastname);
        user_city = (TextView) itemView.findViewById(R.id.tv_user_city);
        Log.d(TAG, "Viewholder");
    }

}

//initialize Viewholder
@Override
public UserAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
    View mView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_layout_person, viewGroup, false);
    Log.d(TAG, "onCreateViewHolder");
    return new ViewHolder(mView);
}

//Bind data to view
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    holder.user_name.setText(mUser.get(position).getUser().getFirstname());
    holder.user_lastName.setText(mUser.get(position).getUser().getLastname());
    holder.user_city.setText(mUser.get(position).getUser().getCity());
    Log.d(TAG, "onBindViewHolder");
}

public void setUserList(ArrayList<User> user){
    this.mUser = user;
    notifyItemRangeChanged(0, user.size());
    notifyDataSetChanged();
}

public void updateData(ArrayList<User> userList) {
    mUser.clear();
    mUser.addAll(userList);
    notifyDataSetChanged();
}

public void removeItem(int position) {
    mUser.remove(position);
    notifyItemRemoved(position);
}

@Override
public int getItemCount() {
    Log.d(TAG, "getItemCount");
    if (mUser == null) {
        return 0;
    } else {
        return mUser.size();
    }
}   }

这是MainActivity中的onResponse:

public void onResponse(Call call, Response response) throws IOException {
            if (response.isSuccessful()) {
                String responseStr = response.body().string();
                Gson gson = new Gson();
                Type userListType = new TypeToken<ArrayList<User>>() {
                }.getType();
                final ArrayList<User> userList = gson.fromJson(responseStr, userListType);
                CustomMessageEvent event = new CustomMessageEvent();
                event.setUserList(userList);
                EventBus.getDefault().post(event);
                System.out.print(responseStr);
                response.body().close();
            }
        }

更新

XML-File(CardView)

<android.support.v7.widget.CardView 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/rv_user"
                                android:layout_width="match_parent"
                                android:layout_height="120dp"
                                android:layout_margin="3dp"
                                android:elevation="11dp"
                                android:foregroundGravity="center_horizontal"
                                android:scrollbars="vertical"
                                android:theme="@style/AppThemeFSF"
                                app:cardBackgroundColor="@color/cardview_light_background"
                                app:cardCornerRadius="4dp"
                                app:cardElevation="4dp"
>

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

    <android.support.v7.widget.AppCompatImageView
        android:id="@+id/iv_profile_picture"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_gravity="center_vertical"
        android:layout_marginBottom="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_weight="1"
        android:src="@mipmap/ic_launcher"/>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:layout_weight="1"
            android:gravity="left|center"
            android:text="Name"/>

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_weight="1"
            android:gravity="left|center"
            android:text="Nachname"/>

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_weight="1"
            android:gravity="left|center"
            android:text="Stadt"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv_user_name"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:layout_weight="1"
            android:gravity="left|center"
            android:text="Name"
            android:textAppearance="?android:textAppearanceLarge"
            android:textSize="14sp"/>

        <TextView
            android:id="@+id/tv_user_lastname"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_weight="1"
            android:gravity="left|center"
            android:text="Nachname"
            android:textAppearance="?android:textAppearanceLarge"
            android:textSize="14sp"/>

        <TextView
            android:id="@+id/tv_user_city"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_weight="1"
            android:gravity="left|center"
            android:text="Stadt"
            android:textAppearance="?android:textAppearanceLarge"
            android:textSize="14sp"
            />

    </LinearLayout>

</LinearLayout>

Snippet from AS

Snippet from Logcat

先谢谢你们,祝你有个美好的一天!

3 个答案:

答案 0 :(得分:1)

问题是您没有订阅接收事件。所以,当你致电:

EventBus.getDefault().post(event);

没有人参加活动!而且你没有填写你的名单。所以你的适配器没有任何显示。

订阅此类活动:

@Subscribe
public void receiveEvent(CustomMessageEvent event){
    mAdapter.updateData(event.getUserList())
}

此方法应该在您的片段中。如果你把它放在你的适配器内,你将违反单一责任原则。您的适配器应该承担的唯一责任是将列表链接到RecyclerView。

快乐的编码!

答案 1 :(得分:0)

好的伙计们......我有解决方案!这让我有点疯狂......问题不在于适配器或ViewHolder或Fragment中的任何内容。 “真正的”问题是来自CardView-Elements内部TextView的文本大小。我删除了这个属性,现在每个数据集都可用! 非常感谢您的帮助和提示!太棒了!

HAPPY编码;)

答案 2 :(得分:-1)

尝试更改此

public void setUserList(ArrayList<User> user){
    this.mUser = user;
    notifyItemRangeChanged(0, user.size());
    notifyDataSetChanged();
}

到这个

 public void setUserList(ArrayList<User> user){
        mUser.add(user);
        notifyItemInserted(user.size());
        notifyDataSetChanged();
    }

然后在你的片段中,调用

  

mAdapter.setUserList(用户列表);