I want to display Items from the mysql database to my recyclerview but I dont know why it is not displaying anything
Recyclerview
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();
}
Issue no 1 :- set adapter outside the loop
Issue no 2 :- you forgot to notify your
likelyProblemAdapter
after adding new data in yourmlikelyProblems
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>
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();
Change xml (SwipeRefreshLayout currently hide your recycleview):
set adapter outside the loop: mRecyclerView.setAdapter(likelyProblemAdapter);
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>