E/RecyclerView: No adapter attached; skipping layout when fetching Json Array - android

I have this strange issue. When i try to get data on my screen from database. It does not shows anything and goes my onFailure method in UsersHomeActivity class. My web service works fine when i try with Postman, i get all the data as Json Array. Also in Debug screen at LogCat i can see my response and request , they all correct. But when i click the bDisplayActivity, nothing shows and i see E/RecyclerView: No adapter attached; skipping layout.
My PHP WebService:
<?php
require_once ('viewConnection.php');
$status="status";
$message = "message";
$sql = "SELECT Activity_Name, Activity_StartDate, Activity_EndDate, Activity_Capacity, Activity_City,
Activity_Deadline_Abstract, Activity_Deadline_Notice, Activity_Deadline_CameraReady, Activity_Deadline_FullVersion FROM activity";
$res = mysqli_query($conn,$sql);
$result = array();
while($row = mysqli_fetch_array($res)){
array_push($result,
array('Activity_Name'=>$row[0], 'Activity_StartDate'=>$row[1],
'Activity_EndDate'=>$row[2], 'Activity_Capacity'=>$row[3], 'Activity_City'=>$row[4],
'Activity_Deadline_Abstract'=>$row[5], 'Activity_Deadline_Notice'=>$row[6],
'Activity_Deadline_CameraReady'=>$row[7], 'Activity_Deadline_FullVersion'=>$row[8],));
}
echo json_encode(array("Activity"=>$result,$status=>1,$message=>"Success"));
mysqli_close($conn);
?>
These are my codes:
my row list item layout :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="horizontal"
android:padding="5dp">
<TextView
android:id="#+id/tvActivity_Name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="#android:style/TextAppearance.Material.Body1" />
<TextView
android:id="#+id/tvActivity_StartDate"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tvActivity_EndDate"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tvActivity_Capacity"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tvActivity_City"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tvActivity_Deadline_Abstract"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tvActivity_Deadline_Notice"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tvActivity_Deadline_CameraReady"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tvActivity_Deadline_FullVersion"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
This my activity layout :
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.furkankaracan.activityplanner.UserHomeActivity">
<Button
android:id="#+id/button_display_data_activity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Aktiviteleri Yükle" />
<android.support.v7.widget.RecyclerView
android:layout_marginTop="20dp"
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="300dp" />
</LinearLayout>
And my adapter and my activity class :
public class ActivityAdapter extends
RecyclerView.Adapter<ActivityAdapter.ViewHolder> {
private List<ActivityModel> dataList;
private Context context;
public ActivityAdapter(List<ActivityModel> dataList, Context context){
this.dataList = dataList;
this.context = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
View view = LayoutInflater.from(context).inflate(R.layout.list_view, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position){
ActivityModel model = dataList.get(position);
DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
holder.tvActivity_Name.setText(model.getActivityName());
holder.tvActivity_StartDate.setText(df.format(model.getActivityStartDate()));
holder.tvActivity_EndDate.setText(df.format(model.getActivityEndDate()));
holder.tvActivity_Capacity.setText(model.getActivityCapacity());
holder.tvActivity_City.setText(model.getActivityCity());
holder.tvActivity_Deadline_Abstract.setText(df.format(model.getActivityDeadlineAbstract()));
holder.tvActivity_Deadline_Notice.setText(df.format(model.getActivityDeadlineNotice()));
holder.tvActivity_Deadline_CameraReady.setText(df.format(model.getActivityDeadlineCameraReady()));
holder.tvActivity_Deadline_FullVersion.setText(df.format(model.getActivityDeadlineFullVersion()));
}
#Override
public int getItemCount(){
return dataList.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
public TextView tvActivity_Name, tvActivity_StartDate, tvActivity_EndDate, tvActivity_Capacity, tvActivity_City,
tvActivity_Deadline_Abstract, tvActivity_Deadline_Notice, tvActivity_Deadline_CameraReady,
tvActivity_Deadline_FullVersion;
ViewHolder(View itemView){
super(itemView);
tvActivity_Name = (TextView) itemView.findViewById(R.id.tvActivity_Name);
tvActivity_StartDate = (TextView) itemView.findViewById(R.id.tvActivity_StartDate);
tvActivity_EndDate = (TextView) itemView.findViewById(R.id.tvActivity_EndDate);
tvActivity_Capacity = (TextView) itemView.findViewById(R.id.tvActivity_Capacity);
tvActivity_City = (TextView) itemView.findViewById(R.id.tvActivity_City);
tvActivity_Deadline_Abstract = (TextView) itemView.findViewById(R.id.tvActivity_Deadline_Abstract);
tvActivity_Deadline_Notice = (TextView) itemView.findViewById(R.id.tvActivity_Deadline_Notice);
tvActivity_Deadline_CameraReady = (TextView) itemView.findViewById(R.id.tvActivity_Deadline_CameraReady);
tvActivity_Deadline_FullVersion = (TextView) itemView.findViewById(R.id.tvActivity_Deadline_FullVersion);
}
}
}
public class UserHomeActivity extends AppCompatActivity {
private ActivityAdapter adapter;
private RecyclerView recyclerView;
private ProgressDialog progressDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_home);
Button bDisplayActivity = (Button) findViewById(R.id.button_display_data_activity);
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
progressDialog = new ProgressDialog(this);
bDisplayActivity.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
getAllActivity();
}
});
}
private void getAllActivity(){
progressDialog.setTitle("Displaying Data");
progressDialog.setMessage("Loading...");
GetActivityDataAPI service = RetrofitService.getClient().create(GetActivityDataAPI.class);
Call<ActivityListModel> listModelCall = service.getAllDataActivity();
listModelCall.enqueue(new Callback<ActivityListModel>() {
#Override
public void onResponse(Call<ActivityListModel> call, Response<ActivityListModel> response) {
ActivityListModel activityListModel = response.body();
if(activityListModel.getStatus() == 1){
List<ActivityModel> activityModels = activityListModel.getActivityList();
ActivityAdapter activityAdapter = new ActivityAdapter(activityModels, UserHomeActivity.this);
recyclerView.setLayoutManager(new LinearLayoutManager(UserHomeActivity.this));
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(activityAdapter);
progressDialog.dismiss();
}else {
Toast.makeText(UserHomeActivity.this, activityListModel.getMessage(), Toast.LENGTH_SHORT).show();
}
}
#Override
public void onFailure(Call<ActivityListModel> call, Throwable t) {
Toast.makeText(UserHomeActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show();
progressDialog.dismiss();
}
});
}
}
And My Model Classes:
ublic class ActivityModel {
#SerializedName("Activity_Name")
private String activityName;
#SerializedName("Activity_StartDate")
private Date activityStartDate;
#SerializedName("Activity_EndDate")
private Date activityEndDate;
#SerializedName("Activity_Capacity")
private Integer activityCapacity;
#SerializedName("Activity_City")
private String activityCity;
#SerializedName("Activity_Deadline_Abstract")
private Date activityDeadlineAbstract;
#SerializedName("Activity_Deadline_Notice")
private Date activityDeadlineNotice;
#SerializedName("Activity_Deadline_CameraReady")
private Date activityDeadlineCameraReady;
#SerializedName("Activity_Deadline_FullVersion")
private Date activityDeadlineFullVersion;
public ActivityModel(String activityName,Date activityStartDate, Date activityEndDate, Integer activityCapacity, String activityCity,
Date activityDeadlineAbstract, Date activityDeadlineNotice, Date activityDeadlineCameraReady, Date activityDeadlineFullVersion)
{
this.activityName = activityName;
this.activityStartDate = activityStartDate;
this.activityEndDate = activityEndDate;
this.activityCapacity = activityCapacity;
this.activityCity = activityCity;
this.activityDeadlineAbstract = activityDeadlineAbstract;
this.activityDeadlineNotice = activityDeadlineNotice;
this.activityDeadlineCameraReady = activityDeadlineCameraReady;
this.activityDeadlineFullVersion = activityDeadlineFullVersion;
}
public String getActivityName() {
return activityName;
}
public void setActivityName(String activityName) {
this.activityName = activityName;
}
public Date getActivityStartDate() {
return activityStartDate;
}
public void setActivityStartDate(Date activityStartDate) {
this.activityStartDate = activityStartDate;
}
public Date getActivityEndDate() {
return activityEndDate;
}
public void setActivityEndDate(Date activityEndDate) {
this.activityEndDate = activityEndDate;
}
public int getActivityCapacity() {
return activityCapacity;
}
public void setActivityCapacity(int activityCapacity) {
this.activityCapacity = activityCapacity;
}
public String getActivityCity() {
return activityCity;
}
public void setActivityCity(String activityCity) {
this.activityCity = activityCity;
}
public Date getActivityDeadlineAbstract() {
return activityDeadlineAbstract;
}
public void setActivityDeadlineAbstract(Date activityDeadlineAbstract) {
this.activityDeadlineAbstract = activityDeadlineAbstract;
}
public Date getActivityDeadlineNotice() {
return activityDeadlineNotice;
}
public void setActivityDeadlineNotice(Date activityDeadlineNotice) {
this.activityDeadlineNotice = activityDeadlineNotice;
}
public Date getActivityDeadlineCameraReady() {
return activityDeadlineCameraReady;
}
public void setActivityDeadlineCameraReady(Date activityDeadlineCameraReady) {
this.activityDeadlineCameraReady = activityDeadlineCameraReady;
}
public Date getActivityDeadlineFullVersion() {
return activityDeadlineFullVersion;
}
public void setActivityDeadlineFullVersion(Date activityDeadlineFullVersion) {
this.activityDeadlineFullVersion = activityDeadlineFullVersion;
}
}
public class ActivityListModel {
#SerializedName("Activity")
private ArrayList<ActivityModel> ActivityList ;
#SerializedName("status")
private Integer status;
#SerializedName("message")
private String message;
public ActivityListModel() {
}
public ArrayList<ActivityModel> getActivityList() {
return ActivityList;
}
public void setActivityList(ArrayList<ActivityModel> ActivityList) {
this.ActivityList = ActivityList;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
My LoggingInterceptors Class:
public class LoggingInterceptors implements Interceptor{
#Override
public Response intercept(Chain chain) throws IOException{
Request request = chain.request();
long t1 = System.nanoTime();
String requestLog = String.format("Sending request %s on %s%n%s",
request.url(), chain.connection(), request.headers());
if (request.method().compareToIgnoreCase("post") == 0){
requestLog = "\n" + requestLog + "\n" + bodyToString(request);
}
Log.d("TAG", "request" + "\n" + requestLog);
Response response = chain.proceed(request);
long t2 = System.nanoTime();
String responseLog = String.format("Received response for %s in %.1fms%n%s",
response.request().url(), (t2 - t1) / 1e6d, response.headers());
String bodyString = response.body().string();
Log.d("TAG", "response" + "\n" + responseLog + "\n" + bodyString);
return response.newBuilder()
.body(ResponseBody.create(response.body().contentType(), bodyString))
.build();
}
public static String bodyToString(final Request request){
try{
final Request copy = request.newBuilder().build();
final Buffer buffer = new Buffer();
copy.body().writeTo(buffer);
return buffer.readUtf8();
}catch (final IOException e){
return "hatalı bodyToString";
}
}
}

Try calling notifyDataSetChanged() in your adapter where you update your data source.

I think it would be a good idea to have a swapCursor method. This method will set the cursor and you can notify if the data has changed as follows:
public void swapCursor(Cursor cursor) {
if (mCursor != null) {
mCursor.close();
}
mCursor = cursor;
notifyDataSetChanged();
}
Then when you are loading the data in your Fragment/Activity you call it like this:
#Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data)
{
mAdapter.swapCursor(data);
}
#Override
public void onLoaderReset(Loader<Cursor> loader)
{
mAdapter.swapCursor(null);
}
Hope this helps :)

Related

I need to open a Pdf file when card view button clicked and each card has its own pdf against

Here is my Orderofday.java where i am getting data from URL and in String proceeding_file i get pdf link. i have shown the data present on every card but could not achieve to open a pdf link on card button clicked.
public class Orderofday extends AppCompatActivity {
private static final String TAG = Orderofday.class.getSimpleName();
private RecyclerView recyclerView;
private OrderList mAdapter;
ProgressDialog progess;
List<OrdersModel> myList = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_orderofday);
progess = new ProgressDialog(Orderofday.this);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mAdapter = new OrderList(myList, Orderofday.this);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);
getResult();
}
public void getResult() {
String url = "http://senate.gov.pk/ne/getdata/order_of_day_json.php";
//detect internet and show the data
if (isNetworkStatusAvialable(getApplicationContext())) {
progess.setMessage("Loading data....");
progess.show();
StringRequest strReq = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.d("response", response);
try {
JSONObject mainList = new JSONObject(response);
JSONArray result = mainList.getJSONArray("data");
if (result.length() > 0) {
for (int i = 0; i < result.length(); i++) {
mainList = result.getJSONObject(i);
OrdersModel model = new OrdersModel();
model.title_en = mainList.getString("title_en");
model.YearID = mainList.getString("YearID");
model.session_title_en = mainList.getString("session_title_en");
model.sitting_date = mainList.getString("sitting_date");
model.proceeding_file = mainList.getString("proceeding_file");
myList.add(model);
}
mAdapter.notifyDataSetChanged();
progess.dismiss();
} else {
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, "Error: " + error.getMessage());
progess.dismiss();
}
});
Log.d("params", strReq.getUrl() + " and " + strReq.getBodyContentType() + " abd " + strReq.getBodyContentType());
// Adding String request to request queue
AppSingleton.getInstance(getApplicationContext()).addToRequestQueue(strReq, TAG);
} else {
Toast.makeText(getApplicationContext(), "Please check your Internet Connection", Toast.LENGTH_SHORT).show();
}
}
//check internet connection
public static boolean isNetworkStatusAvialable(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityManager != null) {
NetworkInfo netInfos = connectivityManager.getActiveNetworkInfo();
if (netInfos != null) {
return netInfos.isConnected();
}
}
return false;
}
}
Here is my OrderList.java class where i am setting the text from OderModel.java
public class OrderList extends RecyclerView.Adapter<OrderList.MyViewHolder> {
List<OrdersModel> list;
Context c;
public OrderList(List<OrdersModel> list, Context c) {
this.list = list;
this.c = c;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.orders_list, parent, false);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
OrdersModel movie = list.get(position);
holder.name.setText(movie.gettitle_en());
holder.type.setText(movie.getYearID());
holder.session_title.setText((movie.getsession_title_en()));
holder.sitting_date.setText(movie.getsitting_date());
}
#Override
public int getItemCount() {
return list.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView name, type , session_title , sitting_date , proceeding_file;
public MyViewHolder(View view) {
super(view);
name = view.findViewById(R.id.name);
type = view.findViewById(R.id.type);
session_title = view.findViewById(R.id.session_title);
sitting_date = view.findViewById(R.id.sitting_date);
}
}
}
And here is my OrderModel.java where i made getters and setter
public class OrdersModel {
String title_en;
String YearID;
String session_title_en;
String sitting_date;
String proceeding_file;
public String gettitle_en() {
return title_en;
}
public void settitle_en(String title_en) {
this.title_en = title_en;
}
public String getYearID() {
return YearID;
}
public void setYearID(String YearID) {
this.YearID = YearID;
}
public String getsession_title_en() {
return session_title_en;
}
public void setsession_title_en(String session_title_en) {
this.session_title_en = session_title_en;
}
public String getsitting_date() {
return sitting_date;
}
public void setsitting_date(String session) {
this.sitting_date = sitting_date;
}
public String getproceeding_file() {
return proceeding_file;
}
public void setproceeding_file(String date) {
this.proceeding_file = proceeding_file;
}
String file;
}
Now i need to open a pdf against every card view clicked i got url of pdf in procedding_file (String) but couldn't get it. Kindly help me out to achieve this and here is xml.
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_margin="#dimen/_5sdp"
android:layout_height="wrap_content"
android:padding="10dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/name"
android:textStyle="bold"
android:textSize="20sp"
android:layout_margin="5dp"
android:text="Title_ID"
android:textColor="#android:color/black" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/type"
android:layout_below="#id/name"
android:layout_margin="5dp"
android:textSize="15sp"
android:text="YearID"
android:textColor="#android:color/black" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/session_title"
android:layout_below="#id/type"
android:layout_margin="5dp"
android:textSize="15sp"
android:text="Session Title"
android:textColor="#android:color/black" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/sitting_date"
android:layout_below="#id/session_title"
android:layout_margin="5dp"
android:textSize="15sp"
android:text="Date "
android:textColor="#android:color/black" />
<Button
android:id="#+id/view_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/sitting_date"
android:layout_alignParentRight="true"
android:text="Click to View Pdf"
android:textColor="#000000"
android:textSize="#dimen/_10sdp">
</Button>
<com.github.barteksc.pdfviewer.PDFView
android:id="#+id/pdfView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
Above answer is correct. However a better solution and the rightest one is to create an interface for the clicklistenet and implement it in tour activity
Initialize viewButton in ViewHolder.
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView name, type , session_title , sitting_date , proceeding_file;
public Button viewButton;
public MyViewHolder(View view) {
super(view);
name = view.findViewById(R.id.name);
type = view.findViewById(R.id.type);
session_title = view.findViewById(R.id.session_title);
sitting_date = view.findViewById(R.id.sitting_date);
viewButton = view.findViewById(R.id.view_button);
}
}
Then set onClickListener on viewButton and openPdf
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
OrdersModel movie = list.get(position);
holder.name.setText(movie.gettitle_en());
holder.type.setText(movie.getYearID());
holder.session_title.setText((movie.getsession_title_en()));
holder.sitting_date.setText(movie.getsitting_date());
holder.viewButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openPdf(movie.getproceeding_file())
}
});
}
Suggestion: You should move your PdfViewer in another activity and redirect user to that activity when click viewButton.
initialise view_pdf button in MyViewHolder class. like others.
parse proceeding file like others ~ movie.getproceeding_file()
under the onBindViewHolder put below snippet of code:
holder.proceeding_file.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String pdf_url = movie.getproceeding_file();
// do not forget to concate preceding url of the pdf
Webview webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(pdf_url);
}
});

Android Retrofit2: How can I create expandable recycler view for categories and subcategories?

I am trying to archive this functionality for my grocery app https://imgur.com/Ugj4BIO
But I am failed to do so!
I am showing you what I am able to archive. if you have any Better Solution let me know.
Here is my Json code from which I am fetching my data from web server right now its localhost
{"cats":
[{"sub_cats":
[{
"sub_id":"1",
"sub_title":"Charger",
"sub_img":"http:\/\/localhost\/adminPohanch\/public\/img\/img23.jpg"
},
{
"sub_id":"2",
"sub_title":"Dummy",
"sub_img":"https:\/\/encrypted-tbn0.gstatic.com\/images?q=tbn:ANd9GcTrRRV2S-HAzHVIfS0EtgfIeWnej05TRN1PqWQCLW44h3NqM9UOtQ"
}
],
"id":"52",
"title":"Electric Equipments",
"img":""}],
"message":"Categories Loaded","success":1}
Here is the retofit's API abstract class's method
#GET("cats.php")
Call<Categories> getCategories();
My Categories Model Class is this
public class Categories {
#SerializedName("id")
#Expose
private String id;
#SerializedName("title")
#Expose
private String title;
#SerializedName("img")
#Expose
private String img;
#SerializedName("sub_id")
#Expose
private String sub_id;
#SerializedName("sub_title")
#Expose
private String sub_title;
#SerializedName("sub_img")
#Expose
private String sub_img;
#SerializedName("cats")
#Expose
private List<Categories> cats = null;
#SerializedName("sub_cats")
#Expose
private List<Categories> sub_cats = null;
#SerializedName("message")
#Expose
private String message;
#SerializedName("success")
#Expose
private Integer success;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getImg() {
return img;
}
public void setImg(String img) {
this.img = img;
}
public String getSub_id() {
return sub_id;
}
public void setSub_id(String sub_id) {
this.sub_id = sub_id;
}
public String getSub_title() {
return sub_title;
}
public void setSub_title(String sub_title) {
this.sub_title = sub_title;
}
public String getSub_img() {
return sub_img;
}
public void setSub_img(String sub_img) {
this.sub_img = sub_img;
}
public List<Categories> getCats() {
return cats;
}
public void setCats(List<Categories> cats) {
this.cats = cats;
}
public List<Categories> getSub_cats() {
return sub_cats;
}
public void setSub_cats(List<Categories> sub_cats) {
this.sub_cats = sub_cats;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Integer getSuccess() {
return success;
}
public void setSuccess(Integer success) {
this.success = success;
}
}
Here is the code for my Activity
from which I am getting result/data and initializing to my Adapter
Call<Categories> callCats = RetrofitBuilder.getInstance().getApi().getCategories();
callCats.enqueue(new Callback<Categories>() {
#Override
public void onResponse(Call<Categories> call, Response<Categories> response) {
Categories cats = response.body();
Log.d("cats", "onResponse: " + cats);
if (cats != null){
List<Categories> catsList = cats.getCats();
Log.d("Categories", "onResponse: " + catsList.size());
Log.d("Categories", "onResponse: " + cats.getCats().toString());
for(int i = 0; i < catsList.size(); i++){
Categories categoriesData = new Categories();
String id = catsList.get(i).getId();
String title = catsList.get(i).getTitle();
String img = catsList.get(i).getImg();
categoriesData.setId(id);
categoriesData.setTitle(title);
categoriesData.setImg(img);
getCatItems.add(categoriesData);
List<Categories> sub = catsList.get(i).getSub_cats();
Toast.makeText(BuyerDashboard.this, ""+sub.size(), Toast.LENGTH_SHORT).show();
for (int j = 0; j < sub.size(); j++){
Categories categories = new Categories();
categories.setId(sub.get(i).getSub_id());
categories.setSub_title(sub.get(i).getSub_title());
categories.setSub_img(sub.get(i).getSub_img());
getSubItems.add(categories);
}
}
ExpandableRecyclerViewAdapter expandableCategoryRecyclerViewAdapter =
new ExpandableRecyclerViewAdapter(getApplicationContext(), getCatItems, getSubItems);
expanderRecyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
expanderRecyclerView.setAdapter(expandableCategoryRecyclerViewAdapter);
}
}
#Override
public void onFailure(Call<Categories> call, Throwable t) {
Toast.makeText(BuyerDashboard.this, t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
**ExpandableRecyclerViewAdapter **
public class ExpandableRecyclerViewAdapter extends RecyclerView.Adapter<ExpandableRecyclerViewAdapter.ViewHolder> {
List<Categories> nameList ;
List<Integer> counter = new ArrayList<Integer>();
List<Categories> itemNameList;
Context context;
public ExpandableRecyclerViewAdapter(Context context,
List<Categories> nameList,
List<Categories> itemNameList ) {
this.nameList = nameList;
this.itemNameList = itemNameList;
this.context = context;
Log.d("namelist", nameList.toString());
for (int i = 0; i < nameList.size(); i++) {
counter.add(0);
}
}
#NonNull
#Override
public ExpandableRecyclerViewAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.cat_items, viewGroup, false);
ExpandableRecyclerViewAdapter.ViewHolder vh = new ExpandableRecyclerViewAdapter.ViewHolder(v);
return vh;
}
#Override
public void onBindViewHolder(#NonNull final ExpandableRecyclerViewAdapter.ViewHolder holder, final int position) {
Categories categories = nameList.get(position);
holder.name.setText(categories.getTitle());
InnerRecyclerViewAdapter itemInnerRecyclerView = new InnerRecyclerViewAdapter(itemNameList, context);
holder.cardRecyclerView.setLayoutManager(new GridLayoutManager(context, 2));
holder.cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (counter.get(position) % 2 == 0) {
holder.cardRecyclerView.setVisibility(View.VISIBLE);
} else {
holder.cardRecyclerView.setVisibility(View.GONE);
}
counter.set(position, counter.get(position) + 1);
}
});
holder.cardRecyclerView.setAdapter(itemInnerRecyclerView);
}
#Override
public int getItemCount() {
return nameList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView name;
ImageButton dropBtn;
RecyclerView cardRecyclerView;
CardView cardView;
public ViewHolder(final View itemView) {
super(itemView);
name = itemView.findViewById(R.id.categoryTitle);
dropBtn = itemView.findViewById(R.id.categoryExpandBtn);
cardRecyclerView = itemView.findViewById(R.id.innerRecyclerView);
cardView = itemView.findViewById(R.id.cardView);
}
}
}
InnerRecyclerViewAdapter For Childs/(sub-categories)
public class InnerRecyclerViewAdapter extends RecyclerView.Adapter<InnerRecyclerViewAdapter.ViewHolder> {
public List<Categories> nameList;
Context context;
public InnerRecyclerViewAdapter(List<Categories> nameList, Context context) {
this.nameList = nameList;
this.context = context;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int i) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.sub_cat_items, parent, false);
InnerRecyclerViewAdapter.ViewHolder vh = new InnerRecyclerViewAdapter.ViewHolder(v);
return vh;
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, final int position) {
Categories subCategories = nameList.get(position);
holder.name.setText(subCategories.getTitle());
holder.name.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(context, ProductsActivity.class);
Categories subCategories = nameList.get(position);
i.putExtra("sub_cat_id", subCategories.getId());
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
});
}
#Override
public int getItemCount() {
return nameList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView name;
public ViewHolder(View itemView) {
super(itemView);
name = itemView.findViewById(R.id.itemTextView);
}
}
}
Right now I am not displaying anything for Images I am focusing on Title of categories(parents) and sub-categories (child)
Here is my sub_cat_items file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:id="#+id/cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:id="#+id/linearLayoutHolder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp">
<ImageView
android:id="#+id/sub_cat_img"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:background="#drawable/pohanch" />
<TextView
android:id="#+id/itemTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:text="Fresh Fruits"
android:textAlignment="center"
android:textSize="14sp" />
</LinearLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
And the last here is my cat_items File
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true">
<android.support.v7.widget.CardView
android:id="#+id/cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/linearLayoutHolder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="20dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:orientation="vertical">
<TextView
android:id="#+id/categoryTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fruits & Vegetables"
android:textColor="#color/basiccolor"
android:textSize="16sp" />
<ImageButton
android:id="#+id/categoryExpandBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:background="#FFF"
android:src="#drawable/ic_arrow_downward_black_24dp" />
</RelativeLayout>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/innerRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/linearLayoutHolder"
android:background="#f6f6f6"
android:visibility="gone">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
I am successful in displaying Categories (parents) to the output but having issues with subcategories(children)
Here is my output
https://imgur.com/Feq6XBP
Checkout this example code for customized expandableview.
https://github.com/developernadeem/ShopGrocery
RecyclerView is for list not for ExpandableListView maybe you want to check this tutorial: https://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/
Or maybe want to check this library: https://github.com/thoughtbot/expandable-recycler-view
Of course you can use RecyclerView but you have to change the layout doing your own calculations, and making the listener to make the expandable and not expandable
Retrofit returns object
Configure classes this way
public class Category{
List<Cats> cats;
private String message;
private int success;
}
Public class Cats{
List<SubCats> sub_cats;
private String id;
private String title;
private String img;
}
public class SubCats{
private String sub_id;
private String sub_title;
private String sub_img;
}
Now
#GET("cats.php")
Call<Category> getCategories();
Yo need to configure JSON as well, Remove red marked in the image. It seems unnecessary.
Reconfigure the PHP as well

RecyclerView doesn't display any item view at load

I am retrieving JSON data and parcing it by Retrofit. I want to display it on RecyclerView but can't make it display the views. Where am I doing it wrong? Here MainActivity, Model and ListFlowerAdapter are given
public class MainActivity extends AppCompatActivity {
private ListFlowerAdapter mListFlowerAdapter;
private RecyclerView mRecyclerView;
private RetrofitClient mRetrofitClient;
LinearLayoutManager linearLayoutManager;
//private List<FlowerModel> myFlowers;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
configViews();
mRetrofitClient = new RetrofitClient();
Call<List<FlowerModel>> listCall = mRetrofitClient.getFlowerService().getAllFlowers();
listCall.enqueue(new Callback<List<FlowerModel>>() {
#Override
public void onResponse(Call<List<FlowerModel>> call, Response<List<FlowerModel>> response) {
if (response.isSuccessful()) {
List<FlowerModel> flowerList = response.body();
for (int i = 0; i < flowerList.size(); i++) {
FlowerModel flowerModel = flowerList.get(i);
mListFlowerAdapter.addFlower(flowerModel);
}
} else {
int sc = response.code();
switch (sc) {
}
}
}
#Override
public void onFailure(Call<List<FlowerModel>> call, Throwable t) {
}
});
}
private void configViews() {
mRecyclerView = findViewById(R.id.recycler_view);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setRecycledViewPool(new RecyclerView.RecycledViewPool());
linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(linearLayoutManager);
mListFlowerAdapter = new ListFlowerAdapter();
mRecyclerView.setAdapter(mListFlowerAdapter);
//mRecyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext(),LinearLayoutManager.VERTICAL,false));
//mListFlowerAdapter = new ListFlowerAdapter();
// mRecyclerView.setAdapter(mListFlowerAdapter);
}
}
ListFlowerAdapter class
public class ListFlowerAdapter extends RecyclerView.Adapter<ListFlowerAdapter.MyViewHolder> {
private static final String TAG = ListFlowerAdapter.class.getSimpleName();
private Context context;
private List<FlowerModel> myFlowers;
public ListFlowerAdapter() {
myFlowers = new ArrayList<>();
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View row = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_item, null, false);
return new MyViewHolder(row);
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, int position) {
FlowerModel currFlower = myFlowers.get(position);
holder.mName.setText(currFlower.getName());
holder.mPrice.setText(Double.toString(currFlower.getPrice()));
Picasso.with(holder.itemView.getContext()).load("http://services.hanselandpetal.com/photos/" + currFlower.getPhoto()).into(holder.mPhoto);
}
#Override
public int getItemCount() {
return myFlowers.size();
}
public void addFlower(FlowerModel flowerModel) {
myFlowers.add(flowerModel);
notifyDataSetChanged();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
private ImageView mPhoto;
private TextView mName, mPrice;
public MyViewHolder(View itemView) {
super(itemView);
mPhoto = itemView.findViewById(R.id.flower_photo);
mName = itemView.findViewById(R.id.flower_name);
mPrice = itemView.findViewById(R.id.flower_price);
}
}
}
FlowerModel class
public class FlowerModel implements Serializable {
#Expose
private String category;
#Expose
private String price;
#Expose
private String instructions;
#Expose
private String photo;
#Expose
private String name;
#Expose
private int productId;
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public double getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getInstructions() {
return instructions;
}
public void setInstructions(String instructions) {
this.instructions = instructions;
}
public String getPhoto() {
return photo;
}
public void setPhoto(String photo) {
this.photo = photo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getProductId() {
return productId;
}
public void setProductId(int productId) {
this.productId = productId;
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:orientation="horizontal">
<ImageView
android:id="#+id/flower_photo"
android:layout_width="150dp"
android:layout_height="150dp"
android:background="#mipmap/ic_launcher"
android:scaleType="centerCrop" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/flower_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Large"
android:text="Large Text" />
<TextView
android:id="#+id/flower_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:text="Medium Text" />
</LinearLayout>
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
tools:context=".MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Stop Downloading" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none"
android:visibility="visible" />
Could you help me with the issue, please? Thank you very much.
Don't notify your adapter recursively,
Your code :
for (int i = 0; i < flowerList.size(); i++) {
FlowerModel flowerModel = flowerList.get(i);
mListFlowerAdapter.addFlower(flowerModel); // Called from here
}
Code from adapter
public void addFlower(FlowerModel flowerModel) {
myFlowers.add(flowerModel);
notifyDataSetChanged(); // Notifying everytime
}
Instead, you should add all data to adapter and notify only once : (Updated code)
for (int i = 0; i < flowerList.size(); i++) {
FlowerModel flowerModel = flowerList.get(i);
mListFlowerAdapter.addFlower(flowerModel);
}
mListFlowerAdapter.notifyDataSetChanged(); // Notify like this
Code from adapter
public void addFlower(FlowerModel flowerModel) {
myFlowers.add(flowerModel);
//notifyDataSetChanged(); Remove from here
}
Pass activity context to the adapter and use that context for populating item on list view.
private Context mContext;
public ListFlowerAdapter(Context context) {
myFlowers = new ArrayList<>();
this.mContext = context;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View row = LayoutInflater.from(mContext).inflate(R.layout.row_item, parent, false);
return new MyViewHolder(row);
}
From activity send the activity context for initializing the adapter like this
mListFlowerAdapter = new ListFlowerAdapter(this);
And for adding a new item
public void addFlowers(List<FlowerModel> flowerModels) {
for (int i = 0; i < flowerModels.size(); i++) {
FlowerModel flowerModel = flowerModels.get(i);
myFlowers.add(flowerModel);
}
notifyDataSetChanged();
}
And successful response just send the list for updating the adapter
List<FlowerModel> flowerList = response.body();
mListFlowerAdapter.addFlowers(flowerList);
Hope this will help.
Try to remove this line:
mRecyclerView.setHasFixedSize(true);
And add this in the XML file:
android:fillViewport="true"
Without that line, your RecyclerView content initial height is 0 (empty list), and with the fixed size it can't grow although you add items to the adapter.

Parsing Json data with imageview using Volley and Picasso and displaying in Recycler and Card View

My data shows in logcat but displays nothing while opening app. You can further view over here. But I found a problem "No adapter attached : skipping layout" Any idea with this problem?
This is my MainActivity xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:card_view="http://schemas.android.com/tools">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
card_view:cardUseCompatPadding="true"
card_view:cardElevation="5dp"
card_view:cardCornerRadius="5dp">
</android.support.v7.widget.RecyclerView>
<ProgressBar
android:id="#+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:indeterminateTint="#color/colorPrimary" />
</RelativeLayout>
CardView xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:card_view="http://schemas.android.com/apk/res-auto">
<android.support.v7.widget.CardView
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:elevation="3dp"
card_view:cardUseCompatPadding="true"
card_view:cardElevation="5dp"
card_view:cardCornerRadius="5dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton android:id="#+id/imagebtn"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:scaleType="centerCrop" />
<TextView android:id="#+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:layout_below="#+id/imagebtn"
android:padding="5dp"
android:textColor="#000"
android:gravity="center"
android:text="Hospital Name"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
MainActivity java class file:
public class MainActivity extends AppCompatActivity implements
ExampleAdapter.OnItemClickListener {
public static final String EXTRA_CREATOR = "orga_organame";
public static final String EXTRA_URL =
"http://xelwel.com.np/hamrosewaapp/uploads/images/logo/civil.png";
private RecyclerView mRecyclerview;
private ExampleAdapter mExampleAdapter;
private List<UserInfo> mExampleList;
private RequestQueue mRequestQueue;
ImageButton imageButton;
TextView textView;
ProgressBar progressbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressbar = findViewById(R.id.progressBar);
progressbar.setVisibility(View.VISIBLE);
imageButton = findViewById(R.id.imagebtn);
textView = findViewById(R.id.description);
mRecyclerview = findViewById(R.id.recycler_view);
mRecyclerview.setHasFixedSize(true);
mRecyclerview.setLayoutManager(new LinearLayoutManager(this));
mExampleList = new ArrayList<>();
mRequestQueue = Volley.newRequestQueue(this);
jsonParse();
}
public void jsonParse() {
final String url =
("http://xelwel.com.np/hamrosewaapp/api/get_organization_list");
final StringRequest request = new StringRequest(Request.Method.POST,
url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.i("response", url + "response:" + response);
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray = jsonObject.getJSONArray("org_list");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject obj = jsonArray.getJSONObject(i);
String hsname = obj.getString("orga_organame");
String image = obj.getString("logo_path" + "civil.png");
mExampleList.add(new UserInfo(hsname, image));
}
mExampleAdapter = new ExampleAdapter(MainActivity.this,
mExampleList);
mRecyclerview.setAdapter(mExampleAdapter);
mExampleAdapter.setOnItemClickListener(MainActivity.this);
progressbar.setVisibility(View.GONE);
} catch (JSONException e) {
Toast.makeText(getApplicationContext(),"Error Parsing Data",
Toast.LENGTH_LONG);
e.printStackTrace();
}catch (Exception e) {
Toast.makeText(getApplicationContext(),"Something went
wrong", Toast.LENGTH_LONG);
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
}) {
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("api_key", "123456789");
return params;
}
};
mRequestQueue.add(request);
}
#Override
public void onItemClick(int position) {
Intent detailIntent = new Intent(this, DetailActivity.class);
UserInfo clickedItem = mExampleList.get(position);
detailIntent.putExtra(EXTRA_URL, clickedItem.getImage_link());
detailIntent.putExtra(EXTRA_CREATOR, clickedItem.getName());
startActivity(detailIntent);
}
}
DetailActivity java class file:
Intent intent = getIntent();
String imageUrl = intent.getStringExtra(EXTRA_URL);
String name = intent.getStringExtra(EXTRA_CREATOR);
ImageView imageView = findViewById(R.id.image_view_detail);
TextView textView = findViewById(R.id.text_view);
Picasso.with(this).load(imageUrl).fit().centerInside().into(imageView);
textView.setText(name);
ExampleAdapter java class file:
public class ExampleAdapter extends
RecyclerView.Adapter<ExampleAdapter.ViewHolder> {
private Context mcontext;
private List<UserInfo> mExampleList;
private OnItemClickListener mListener;
public interface OnItemClickListener{
void onItemClick(int position);
}
public void setOnItemClickListener(OnItemClickListener listener){
mListener = listener;
}
public ExampleAdapter(Context context, List<UserInfo> list) {
mcontext = context;
mExampleList = list;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView =
LayoutInflater.from(mcontext).inflate(R.layout.cardview,parent,false);
return new ViewHolder(itemView);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
UserInfo info = mExampleList.get(position);
String imageUrl = info.getImage_link();
String hsname = info.getName();
holder.textView.setText(hsname);
Picasso.with(mcontext).load(imageUrl).fit().centerInside().
into(holder.imageView);
}
#Override
public int getItemCount() {
return mExampleList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView textView;
public ImageView imageView;
public ViewHolder(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.description);
imageView = (ImageView) itemView.findViewById(R.id.image);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(mListener!= null){
int position = getAdapterPosition();
if (position!= RecyclerView.NO_POSITION){
mListener.onItemClick(position);
}
}
}
});
}
}
}
UserInfo java class file:
public class UserInfo {
public String id, name, image_link;
public UserInfo(String id, String name) {
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return image_link;
}
public void setName(String image_link) {
this.image_link = image_link;
}
public String getImage_link() {
return image_link;
}
public void setImage_link(String image_link) {
this.image_link = image_link;
}
}
These are my total code related with these problem. Please view the codes and response with some specific solution...
Try this
You made mistake in UserInfo class Constructor.
You added
mExampleList.add(new UserInfo(hsname, image));
But here set id and name
public UserInfo(String id, String name) {
this.id = id;
this.name = name;
}
change your Constructor
public UserInfo(String name, String image_link) {
this.image_link = image_link;
this.name = name;
}
You didn't set any value in image_link variable,But accessing in adapter class
UserInfo info = mExampleList.get(position);
String imageUrl = info.getImage_link();
String hsname = info.getName();
Try this
try {
JSONObject jsonObject = new JSONObject(response);
String logoURL = jsonObject.getString("logo_path")
JSONArray jsonArray = jsonObject.getJSONArray("org_list");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject obj = jsonArray.getJSONObject(i);
String hsname = obj.getString("orga_organame");
String imageurl = obj.getString("orga_image");
String image = logoURL +"/"+ imageurl ;
mExampleList.add(new UserInfo(hsname, image));
}
}

RecyclerView with SearchView will not populate upon initialization

I am currently using searchView to filter results on a RecyclerView. The problem is when the fragment is initialized, the list won't appear not until I click on the search icon on the toolbar and type some texts on it. I've tried modifying the codes. Take a look specifically at the adapter and holder classes, if you'll try to exchange my current codes to these:
public MembershipActivationAdapter(Context context, List<Member> members){
this.context = context;
this.mMembers = members;
}
and
#Override
public MembershipActivationViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
final View itemView = LayoutInflater.from(context).inflate(R.layout.member_activation_content, viewGroup, false);
}
,it will show the list upon calling the fragment however if I'll type something on the SearchView, it seems to function only as a one way filter. It will filter names but whenever I'll try to remove a character or a text on the searchview, it won't show the list back. Please help me with this. These are my codes:
Adapter & ViewHolder class:
public class MembershipActivationAdapter extends RecyclerView.Adapter<MembershipActivationAdapter.MembershipActivationViewHolder> {
private final List<Member> mMembers;
private final LayoutInflater inflater;
//private final Context context;
public MembershipActivationAdapter(Context context, List<Member> members){
inflater = LayoutInflater.from(context);
mMembers = new ArrayList<>(members);
//this.context = context;
//this.mMembers = members;
}
#Override
public int getItemCount() {
return mMembers.size();
}
#Override
public void onBindViewHolder(MembershipActivationViewHolder holder, final int position) {
final Member m = mMembers.get(position);
holder.name.setText(m.getName());
holder.email.setText(m.getEmail());
if(m.getStatus().equals("Active")){
holder.status.setText(Html.fromHtml("<font color='#00CD00'>" + m.getStatus() + "</font>"));
}else{
holder.status.setText(Html.fromHtml("<font color='#ff0000'>" + m.getStatus() + "</font>"));
}
}
#Override
public MembershipActivationViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
final View itemView = inflater.inflate(R.layout.member_activation_content, viewGroup, false);
return new MembershipActivationViewHolder(itemView);
//final View itemView = LayoutInflater.from(context).inflate(R.layout.member_activation_content, viewGroup, false);
}
public static class MembershipActivationViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public final TextView name;
public final TextView email;
public final TextView status;
public MembershipActivationViewHolder(View v) {
super(v);
v.setOnClickListener(this);
name = (TextView) v.findViewById(R.id.member_name);
email = (TextView) v.findViewById(R.id.member_email);
status = (TextView) v.findViewById(R.id.member_status);
}
}
public List<Member> getMemberList() {
return mMembers;
}
public void animateTo(List<Member> members) {
applyAndAnimateRemovals(members);
applyAndAnimateAdditions(members);
applyAndAnimateMovedItems(members);
}
private void applyAndAnimateRemovals(List<Member> newMembers) {
for (int i = mMembers.size() - 1; i >= 0; i--) {
final Member member = mMembers.get(i);
if (!newMembers.contains(member)) {
removeItem(i);
}
}
}
private void applyAndAnimateAdditions(List<Member> newMembers) {
for (int i = 0, count = newMembers.size(); i < count; i++) {
final Member member = newMembers.get(i);
if (!mMembers.contains(member)) {
addItem(i, member);
}
}
}
private void applyAndAnimateMovedItems(List<Member> newMembers) {
for (int toPosition = newMembers.size() - 1; toPosition >= 0; toPosition--) {
final Member member = newMembers.get(toPosition);
final int fromPosition = mMembers.indexOf(member);
if (fromPosition >= 0 && fromPosition != toPosition) {
moveItem(fromPosition, toPosition);
}
}
}
public Member removeItem(int position) {
final Member member = mMembers.remove(position);
notifyItemRemoved(position);
return member;
}
public void addItem(int position, Member member) {
mMembers.add(position, member);
notifyItemInserted(position);
}
public void moveItem(int fromPosition, int toPosition) {
final Member member = mMembers.remove(fromPosition);
mMembers.add(toPosition, member);
notifyItemMoved(fromPosition, toPosition);
}
}
Fragment Class:
public class MembershipActivationFragment extends Fragment implements SearchView.OnQueryTextListener{
private static String url = "http://www.xxxxx.org/portal/webservices/get_members.php";
private List<Member> memberList;
private RecyclerView recyclerView;
private MembershipActivationAdapter adapter;
private String pid;
private ProgressDialog progressDialog;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.member_activation, container, false);
recyclerView = (RecyclerView) rootView.findViewById(R.id.members_recycler_view);
setHasOptionsMenu(true);
memberList = new ArrayList<>();
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
progressDialog = new ProgressDialog(getActivity());
progressDialog.setMessage("Loading...");
progressDialog.show();
populate();
return rootView;
}
public void populate(){
JsonArrayRequest request = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray jsonArray) {
hidePDialog();
String name, email, status;
Member member;
for(int i = 0; i < jsonArray.length(); i++) {
try {
JSONObject object = jsonArray.getJSONObject(i);
name = object.getString("firstname") + " " + object.getString("lastname");
email = object.getString("email");
if(Integer.parseInt(object.getString("activated"))==1){
status = "Active";
}else{
status = "Not Active";
}
member = new Member(name, email, status);
memberList.add(member);
} catch (Exception e) {
}
}
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener(){
#Override
public void onErrorResponse(VolleyError volleyError) {
volleyError.printStackTrace();
}
});
AppController.getInstance().addToRequestQueue(request);
adapter = new MembershipActivationAdapter(getActivity(), memberList);
recyclerView.setAdapter(adapter);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.search_menu, menu);
final MenuItem item = menu.findItem(R.id.action_search);
final SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
searchView.setOnQueryTextListener(this);
}
#Override
public boolean onQueryTextChange(String query) {
final List<Member> filteredMemberList = filter(memberList, query);
adapter.animateTo(filteredMemberList);
recyclerView.scrollToPosition(0);
return true;
}
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
private List<Member> filter(List<Member> members, String query) {
query = query.toLowerCase();
final List<Member> filteredModelList = new ArrayList<>();
for (Member member : members) {
final String text = member.getName().toLowerCase();
if (text.contains(query)) {
filteredModelList.add(member);
}
}
return filteredModelList;
}
private void hidePDialog() {
if (progressDialog != null) {
progressDialog.dismiss();
progressDialog = null;
}
}
}
RecyclerView container xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/members_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
Content class:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="5dip">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name here"
android:textStyle="bold"
android:id="#+id/member_name"
android:layout_centerVertical="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="email#gmail.com"
android:id="#+id/member_email"
android:layout_below="#+id/member_name"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="email#gmail.com"
android:id="#+id/member_status"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"/>
</RelativeLayout>
</LinearLayout>
Model Class:
public class Member {
private String name;
private String email;
private String status;
public Member(String name, String email, String status) {
this.name = name;
this.email = email;
this.status = status;
}
public Member(String name){
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
Atlast! I was able to find the resolution for this. Kudos to the developer himself, Xaver Kapeller!
public void populate(){
JsonArrayRequest request = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray jsonArray) {
// hidePDialog();
String email, status;
// Member member;
for(int i = 0; i < jsonArray.length(); i++) {
try {
JSONObject object = jsonArray.getJSONObject(i);
final String name = object.getString("firstname") + " " + object.getString("lastname");
email = object.getString("email");
if(Integer.parseInt(object.getString("activated"))==1){
status = "Active";
}else{
status = "Not Active";
}
mModels.add(new ExampleModel(name));
// member = new Member(name, email, status);
} catch (Exception e) {
}
}
mAdapter = new ExampleAdapter(getActivity(), mModels);
mRecyclerView.setAdapter(mAdapter);
}
}, new Response.ErrorListener(){
#Override
public void onErrorResponse(VolleyError volleyError) {
volleyError.printStackTrace();
}
});
AppController.getInstance().addToRequestQueue(request);
}
Just tranfer these two lines of code inside onResponse() and everything will work perfectly fine:
mAdapter = new ExampleAdapter(getActivity(), mModels);
mRecyclerView.setAdapter(mAdapter);

Categories

Resources