我的Recyclerview没有显示任何内容

时间:2018-06-27 07:34:46

标签: android android-recyclerview

我想将项目从mysql数据库显示到我的recyclerview,但我不知道为什么它不显示任何内容

Recyclerview

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_likely_problems"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/zerodimen"
android:paddingLeft="@dimen/zerodimen"
android:paddingRight="@dimen/zerodimen"
android:paddingTop="@dimen/zerodimen"
android:background="@color/colorBackground"
android:orientation="vertical"
tools:context="com.example.system2.tranxav.LIkelyProblemsActivity">
<android.support.v7.widget.Toolbar
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/toolbar"
    android:background="@color/background" />

<android.support.v4.widget.SwipeRefreshLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/likelyProblemSwipeToRefresh">     
</android.support.v4.widget.SwipeRefreshLayout>
<android.support.v7.widget.RecyclerView
    android:layout_below="@id/toolbar"
    android:id="@+id/likelyProblemRecyclerView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

</LinearLayout>

Recyclerview内容

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

<android.support.v7.widget.CardView
    android:layout_margin="8dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:text="welcome"
        android:padding="8dp"
        android:id="@+id/likelyProblem"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</android.support.v7.widget.CardView>
 </LinearLayout>

RecyclerAdapter

public class LikelyProblemAdapter extends   RecyclerView.Adapter<LikelyProblemAdapter.LikelyProblemViewHolder> {
private List<LikelyProblems> mlikeyProblems;
Context context;
RecyclerViewClickListener mlistener;

public interface  RecyclerViewClickListener{
    void onClick(View view, int position);
}

public LikelyProblemAdapter(List<LikelyProblems> mlikeyProblems, Context context) {
    this.mlikeyProblems = mlikeyProblems;
    this.context = context;
}

@NonNull
@Override
public LikelyProblemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(context);
    View  view = inflater.inflate(R.layout.activity_likely_problem_recyclerview_item, null, false);
    LikelyProblemViewHolder viewHolder = new LikelyProblemViewHolder(view);
    return viewHolder;

}

@Override
public void onBindViewHolder(@NonNull LikelyProblemViewHolder holder, int position) {
    LikelyProblems likelyProblems = mlikeyProblems.get(position);
    holder.likelyProblem.setText(likelyProblems.getProblems());
}

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

class LikelyProblemViewHolder extends RecyclerView.ViewHolder {
    TextView likelyProblem;
    Context mCxt;

    public LikelyProblemViewHolder(View itemView) {
        super(itemView);

        this.mCxt = mCxt;
        likelyProblem = itemView.findViewById(R.id.likelyProblem);
    }
  }
 }

MainActivity

private List<LikelyProblems> mlikelyProblems;
private String LIKELY_PROBLEM_URL = MYURL.url + "vehicle-problems";
  @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_first_problem);

    mRecyclerView = (RecyclerView) findViewById(R.id.likelyProblemRecyclerView); // Instantiate Recyclerview
    mlikelyProblems = new ArrayList<>(); // Create a new array list

    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
    mRecyclerView.setLayoutManager(linearLayoutManager);
    mRecyclerView.setHasFixedSize(true);


    likelyProblemAdapter = new LikelyProblemAdapter(mlikelyProblems, this);
    likelyProblemAdapter.notifyDataSetChanged();

    RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
    StringRequest stringRequest = new StringRequest(Request.Method.GET, LIKELY_PROBLEM_URL, new com.android.volley.Response.Listener<String>() {

        @Override
        public void onResponse(String response) {
            Log.i("info", response);
            try {
                JSONArray jsonArray = new JSONArray(response);
                for (int i=0; i<=jsonArray.length()-1; i++){
                    JSONObject likelyProblemJson = jsonArray.getJSONObject(i);
                    likelyProblem = likelyProblemJson.getString("problems");
                    LikelyProblems likelyProblems = new LikelyProblems(likelyProblem);
                    mlikelyProblems.add(likelyProblems);
                    mRecyclerView.setAdapter(likelyProblemAdapter);
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    }, new com.android.volley.Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();
        }
    });

    int socketTimeout = 30000;
    RetryPolicy retryPolicy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
    stringRequest.setRetryPolicy(retryPolicy);
    requestQueue.add(stringRequest);
 }
}

我在控制台上收到json响应,但在recyclerview上没有显示,我不知道是什么原因造成的。

5 个答案:

答案 0 :(得分:2)

  

问题1:-在循环外设置适配器

     

问题2:-,您在向likelyProblemAdapter添加新数据后忘记通知mlikelyProblems

     

问题3:-布局

尝试一下

  for (int i=0; i<=jsonArray.length()-1; i++){
                        JSONObject likelyProblemJson = jsonArray.getJSONObject(i);
                        likelyProblem = likelyProblemJson.getString("problems");
                        LikelyProblems likelyProblems = new LikelyProblems(likelyProblem);
                        mlikelyProblems.add(likelyProblems);

                    }

     mRecyclerView.setAdapter(likelyProblemAdapter);\
     likelyProblemAdapter.notifyDataSetChanged();

像这样更改布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_likely_problems"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/zerodimen"
android:paddingLeft="@dimen/zerodimen"
android:paddingRight="@dimen/zerodimen"
android:paddingTop="@dimen/zerodimen"
android:background="@color/colorBackground"
android:orientation="vertical"
tools:context="com.example.system2.tranxav.LIkelyProblemsActivity">
<android.support.v7.widget.Toolbar
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/toolbar"
    android:background="@color/background" />

<android.support.v4.widget.SwipeRefreshLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/likelyProblemSwipeToRefresh">   

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

</android.support.v4.widget.SwipeRefreshLayout>

</LinearLayout>

答案 1 :(得分:1)

setAdapter移动到for循环之外。然后致电notifyDataSetChanged

@Override
    public void onResponse(String response) {
        Log.i("info", response);
        try {
            JSONArray jsonArray = new JSONArray(response);
            for (int i=0; i<=jsonArray.length()-1; i++){
                JSONObject likelyProblemJson = jsonArray.getJSONObject(i);
                likelyProblem = likelyProblemJson.getString("problems");
                LikelyProblems likelyProblems = new LikelyProblems(likelyProblem);
                mlikelyProblems.add(likelyProblems);
            }

            mRecyclerView.setAdapter(likelyProblemAdapter);
            likelyProblemAdapter.notifyDataSetChanged();

        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

likelyProblemAdapter.notifyDataSetChanged();中删除onCreate

还要在Recycler View内更新您的 Layout 移动SwipeRefreshLayout

喜欢

<android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/likelyProblemSwipeToRefresh"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

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

 </android.support.v4.widget.SwipeRefreshLayout>

答案 2 :(得分:1)

  1. 更改xml(SwipeRefreshLayout当前隐藏您的回收视图):

  2. 在循环外设置适配器:mRecyclerView.setAdapter(likelyProblemAdapter);

答案 3 :(得分:1)

希望它对您有用

try{
        JSONArray jsonArray = new JSONArray(response);
        for (int i = 0; i <= jsonArray.length() - 1; i++) {
            JSONObject likelyProblemJson = jsonArray.getJSONObject(i);
            likelyProblem = likelyProblemJson.getString("problems");
            LikelyProblems likelyProblems = new LikelyProblems(likelyProblem);
            mlikelyProblems.add(likelyProblems);
        }
    } catch(
    JSONException e)

    {
        e.printStackTrace();
    } 
    mRecyclerView.setAdapter(likelyProblemAdapter);
    likelyProblemAdapter.notifyDataSetChanged();

答案 4 :(得分:0)

尝试此代码.. 设置适配器的一种方法。

    private void setAdapter(){
    if (likelyProblemAdapter==null){
        if (!mlikelyProblems.isEmpty()) {
            likelyProblemAdapter = new LikelyProblemAdapter(mlikelyProblems, this);
            mRecyclerView.setAdapter(likelyProblemAdapter);
        }
    }
    else{
        likelyProblemAdapter.notifyDataSetChanged();

    }
}

以这种方式获取所有数据后定义此方法。

 try {
            JSONArray jsonArray = new JSONArray(response);
            for (int i=0; i<=jsonArray.length()-1; i++){
                JSONObject likelyProblemJson = jsonArray.getJSONObject(i);
                likelyProblem = likelyProblemJson.getString("problems");
                LikelyProblems likelyProblems = new LikelyProblems(likelyProblem);
                mlikelyProblems.add(likelyProblems);
                mRecyclerView.setAdapter(likelyProblemAdapter);
            }
          setAdapter();

        } catch (JSONException e) {
            e.printStackTrace();
        }