My Recyclerview is not showing anything

前端 未结 5 1892
自闭症患者
自闭症患者 2021-01-23 13:35

I want to display Items from the mysql database to my recyclerview but I dont know why it is not displaying anything

Recyclerview



        
相关标签:
5条回答
  • 2021-01-23 14:12

    Try this code .. make one method for set adapter..

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

    this method define after getting all data like this way..

     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();
            }
    
    0 讨论(0)
  • 2021-01-23 14:14

    Issue no 1 :- set adapter outside the loop

    Issue no 2 :- you forgot to notify your likelyProblemAdapter after adding new data in your mlikelyProblems

    Issue no 3 :- layout

    Try this

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

    Change your layout like this

    <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>
    
    0 讨论(0)
  • 2021-01-23 14:25

    Hope so its work for you

    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();
    
    0 讨论(0)
  • 2021-01-23 14:28
    1. Change xml (SwipeRefreshLayout currently hide your recycleview):

    2. set adapter outside the loop: mRecyclerView.setAdapter(likelyProblemAdapter);

    0 讨论(0)
  • Move setAdapter outside of for loop. And then call 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();
            }
    
        }
    

    Remove likelyProblemAdapter.notifyDataSetChanged(); from onCreate.

    Also update your Layout move Recycler View inside SwipeRefreshLayout.

    Like

    <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>
    
    0 讨论(0)
提交回复
热议问题