I've get some Json data from server by okhttp ,and I want to show them in a
recycleview inside of a fragment using a custom adapter and receiving the following error.
when I run this code,the logcat tell me the followeing msg:
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
I print the list in the logcat ,it shows null.
but I can get some json data from the IE.Do anyone help me,please?
public class customer extends AppCompatActivity {
private static final String TAG = "MyDebug______ " ;
private RecyclerView cutomerRecyclerView;
private customerAdapter mAdapter;
private ArrayList<custModal> list;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_customer);
Toolbar toolbar = (Toolbar)findViewById(R.id.custom_toolbar);
setSupportActionBar(toolbar);
new getHttpData().execute();
Log.d(TAG, "the list is :" +list);
mAdapter = new customerAdapter(this,list);
cutomerRecyclerView = (RecyclerView)findViewById(R.id.customer_list);
cutomerRecyclerView.setLayoutManager(new LinearLayoutManager(this));
cutomerRecyclerView.setAdapter(mAdapter);
cutomerRecyclerView.addItemDecoration(new RecyclerView.ItemDecoration(){
#Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state){
super.onDraw(c,parent,state);
c.drawColor(R.color.colorDevider);
}
#Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
super.onDrawOver(c, parent, state);
}
#Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
outRect.offset(0,1);
}
});
}
public class customerAdapter extends RecyclerView.Adapter<customerAdapter.customerViewHolder>{
private Context mcontext;
private ArrayList<custModal> mlist;
// private LayoutInflater inflater;
class customerViewHolder extends RecyclerView.ViewHolder{
private TextView name,mem,cust_id;
public customerViewHolder(View view){
super(view);
cust_id = (TextView )view.findViewById(R.id.cust_id);
name = (TextView)view.findViewById(R.id.cust_name);
mem = (TextView)view.findViewById(R.id.cust_mem);
}
}
public customerAdapter(Context context,ArrayList<custModal> list){
this.mcontext = context;
this.mlist = list;
}
#Override
public customerViewHolder onCreateViewHolder(ViewGroup parent,int viewType){
customerViewHolder holder = new customerViewHolder(LayoutInflater.from(
customer.this).inflate(R.layout.customer_item,parent,false));
return holder;
}
#Override
public void onBindViewHolder(customerViewHolder holder,int position){
holder.cust_id.setText(mlist.get(position).getId());
holder.name.setText(mlist.get(position).getName());
holder.mem.setText(mlist.get(position).getMobiphone());
}
#Override
public int getItemCount(){ return mlist.size();}
}
class custModal {
private String id;
private String name;
private String gender;
private String mobiphone;
private String creat_time;
public void setId(String id) {
this.id = id;
}
public String getId() {
return id;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getGender() {
return gender;
}
public void setMobiphone(String mobiphone) {
this.mobiphone = mobiphone;
}
public String getMobiphone() {
return mobiphone;
}
public void setCreat_time(String creat_time) {
this.creat_time = creat_time;
}
public String getCreat_time() {
return creat_time;
}
}
public class getHttpData extends AsyncTask<String,Integer,ArrayList<custModal> >{
//private ArrayList<custModal> custlist;
#Override
// protected void onPreExecute(){}
#Override
protected ArrayList<custModal> doInBackground(String... params){
String url = "http://aaaa.bbbbb.cn/index.php/mobi/customer/app_getlist";
try{
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.build();
Response response = client.newCall(request).execute();
String responseData = response.body().string();
Log.d(TAG, "doInBackground: "+ responseData);
if(responseData != null){
//return getCustFromJson(responseData);
Gson gson = new Gson();
ArrayList<custModal> custlist = gson.fromJson(responseData,
new TypeToken<List<custModal>>() {}.getType()
);
return custlist;
}else{
return null;
}
}catch (IOException I){
I.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(ArrayList<custModal> custlist){
if(!custlist.isEmpty()){
super.onPostExecute(custlist);
list = custlist;
mAdapter.notifyDataSetChanged();
}
}
}
}
make sure initialize adapter, array list of adapter and attaching adapter to recycler in onCreate
You have to then add the new yourAsyncTask().execute where you need to to http call.
In your onBackground add items to the array list and in postExecute simply call notify data change method
Your list is in a null state when you pass it via adapter constructor therefore any operations on the list return null.
You could add a callback to your okhttp request and construct the adapter on the response? One way or another though you need to update the list before you can perform any operations on it.
I put the following code into the Method onPostExecute,it worked.But I think it is not a right solution.like this:
protected void onPostExecute(ArrayList<custModal> custlist){
Log.d(TAG, "onPostExecute(Result result) called");
Log.d(TAG, "onPostExecute: "+custlist);
mAdapter = new customerAdapter(customer.this,custlist);
cutomerRecyclerView = (RecyclerView)findViewById(R.id.customer_list);
cutomerRecyclerView.setLayoutManager(new LinearLayoutManager(customer.this));
cutomerRecyclerView.setAdapter(mAdapter);
}
Related
I using retrofit to display data but data is not getting displayed.i don't know where am I getting wrong.in logcat im getting response as:
2019-12-26 20:34:39.847 28305-28305/com.example.androidtutorial E/helloash: [com.example.androidtutorial.GetAheadFolder.JavaDatum#36ebcd9]
I need help..thanks
GetAHeadModelsnext:
public class GetAHeadModelsnext{
#SerializedName("id")
#Expose
private String id;
#SerializedName("title")
#Expose
private String title;
#SerializedName("java_data")
#Expose
private List<JavaDatum> java_data ;
#SerializedName("xml_data")
#Expose
private List<XmlDatum> xmlData;
public GetAHeadModelsnext(String id, String title, List<JavaDatum> java_data, List<XmlDatum> xmlData) {
this.id = id;
this.title = title;
this.java_data = java_data;
this.xmlData = xmlData;
}
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 List<JavaDatum> getJavaData() {
return java_data;
}
public void setJavaData(List<JavaDatum> javaData) {
this.java_data = javaData;
}
public List<XmlDatum> getXmlData() {
return xmlData;
}
public void setXmlData(List<XmlDatum> xmlData) {
this.xmlData = xmlData;
}
submodel(ListJavaDatum):
public class JavaDatum {
#SerializedName("file_name")
#Expose
private String fileName;
#SerializedName("code")
#Expose
private String code;
public JavaDatum(String fileName, String code) {
this.fileName = fileName;
this.code = code;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}}
interface:
public interface DescriptService {
#GET("/v1/android_tutorials/single_advance?")
Call<GetAHeadModelsnext> getaheadjava(#Query("advance_id") String id);}
activity:
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
/*Create handle for the RetrofitInstance interface*/
progressDialog = new ProgressDialog(getContext());
progressDialog.setMessage("Loading....");
progressDialog.show();
Intent intent = getActivity().getIntent();
String id = intent.getStringExtra("idGHnext");
Log.e("ashwiniiii", String.valueOf(id));
GetAheadApiService service = GetAheadApiClient.getRetrofitInstance().create(GetAheadApiService.class);
Call<GetAHeadModelsnext> call = service.getaheadjava(id);
call.enqueue(new Callback<GetAHeadModelsnext>() {
#Override
public void onResponse(Call<GetAHeadModelsnext> call, Response<GetAHeadModelsnext> response) {
progressDialog.dismiss();
List<JavaDatum> retro=response.body().getJavaData();
generateDataList(retro);
Log.e("helloash", String.valueOf(response.body().getJavaData()));
}
#Override
public void onFailure(Call<GetAHeadModelsnext> call, Throwable t) {
progressDialog.dismiss();
Toast.makeText(getContext(), "Something went wrong...Please try later!", Toast.LENGTH_SHORT).show();
}
});
}
private void generateDataList(List<JavaDatum> photoList) {
recyclerView = getView().findViewById(R.id.nextGHrecycle);
LinearLayoutManager manager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(manager);
recyclerView.setHasFixedSize(true);
adapter = new JavaGetAheadAdapter(getContext(),photoList);
recyclerView.setAdapter(adapter);
}}
ADapter:
public class JavaGetAheadAdapter extends RecyclerView.Adapter<JavaGetAheadAdapter.CustomViewHolder> {
List<JavaDatum> GHmdel;
Context context;
public JavaGetAheadAdapter(Context context,List<JavaDatum> employees) {
this.GHmdel = employees;
this.context = context;
}
#Override
public CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.getaheadnext_item, parent, false);
return new CustomViewHolder(itemView);
}
#Override
public void onBindViewHolder(CustomViewHolder holder, int position) {
holder.employeeName.setText(GHmdel.get(position).getFileName());//
holder.textView.setText(GHmdel.get(position).getCode());
Log.d("adapter",GHmdel.get(position).getFileName());
Log.d("adapter2",GHmdel.get(position).getCode());
}
#Override
public int getItemCount() {
return GHmdel.size();
}
public class CustomViewHolder extends RecyclerView.ViewHolder {
public TextView employeeName;
TextView textView;
public CustomViewHolder(View view) {
super(view);
employeeName = (TextView) view.findViewById(R.id.detailsgetaheadtitle);
textView = view.findViewById(R.id.detailsgetahead);}}}
my json :
{
"id": "1",
"title": "ViewPager and TabHost",
"java_data": [
{
"file_name": "ListDisplay.java",
"code": "package com.example.ListDisplay;\r\n\r\nimport android.os.Bundle;\r\nimport android.app.Activity;\r\nimport android.view.Menu;\r\nimport android.widget.ArrayAdapter;\r\nimport android.widget.ListView;\r\n\r\npublic class ListDisplay extends Activity {\r\n // Array of strings...\r\n String[] mobileArray = {\"Android\",\"IPhone\",\"WindowsMobile\",\"Blackberry\",\r\n \"WebOS\",\"Ubuntu\",\"Windows7\",\"Max OS X\"};\r\n \r\n #Override\r\n protected void onCreate(Bundle savedInstanceState) {\r\n super.onCreate(savedInstanceState);\r\n setContentView(R.layout.activity_main);\r\n \r\n ArrayAdapter adapter = new ArrayAdapter<String>(this, \r\n R.layout.activity_listview, mobileArray);\r\n \r\n ListView listView = (ListView) findViewById(R.id.mobile_list);\r\n listView.setAdapter(adapter);\r\n }\r\n}"
}
],
"xml_data": [
{
"file_name": "activity_main.xml",
"code": "<LinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\r\n xmlns:tools=\"http://schemas.android.com/tools\"\r\n android:layout_width=\"match_parent\"\r\n android:layout_height=\"match_parent\"\r\n android:orientation=\"vertical\"\r\n tools:context=\".ListActivity\" >\r\n\r\n <ListView\r\n android:id=\"#+id/mobile_list\"\r\n android:layout_width=\"match_parent\"\r\n android:layout_height=\"wrap_content\" >\r\n </ListView>\r\n \r\n</LinearLayout>\r\nimport android.os.Bundle;\r\nimport android.app.Activity;\r\nimport android.view.Menu;\r\nimport android.widget.ArrayAdapter;\r\nimport android.widget.ListView;\r\n\r\npublic class ListDisplay extends Activity {\r\n // Array of strings...\r\n String[] mobileArray = {\"Android\",\"IPhone\",\"WindowsMobile\",\"Blackberry\",\r\n \"WebOS\",\"Ubuntu\",\"Windows7\",\"Max OS X\"};\r\n \r\n #Override\r\n protected void onCreate(Bundle savedInstanceState) {\r\n super.onCreate(savedInstanceState);\r\n setContentView(R.layout.activity_main);\r\n \r\n ArrayAdapter adapter = new ArrayAdapter<String>(this, \r\n R.layout.activity_listview, mobileArray);\r\n \r\n ListView listView = (ListView) findViewById(R.id.mobile_list);\r\n listView.setAdapter(adapter);\r\n }\r\n}"
},
{
"file_name": "strings.xml",
"code": "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<resources>\r\n <string name=\"app_name\">ListDisplay</string>\r\n <string name=\"action_settings\">Settings</string>\r\n</resources>"
},
{
"file_name": "activity_listview.xml",
"code": "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<!-- Single List Item Design -->\r\n\r\n<TextView xmlns:android=\"http://schemas.android.com/apk/res/android\"\r\n android:id=\"#+id/label\"\r\n android:layout_width=\"fill_parent\"\r\n android:layout_height=\"fill_parent\"\r\n android:padding=\"10dip\"\r\n android:textSize=\"16dip\"\r\n android:textStyle=\"bold\" >\r\n</TextView>"
}
]
}
Might be i got the problem.
Problem is here #GET("/v1/android_tutorials/single_advance?") you have? in the end of your url and you also have #Query("advance_id") field that mean your request contain two ?. you request currently look like this.
/v1/android_tutorials/single_advance??advance_id="xxxx"
Remember when you add #Query("advance_id") to your request this by default add ? to your your. So remove? from your url end. Your url should look like this
#GET("/v1/android_tutorials/single_advance")
Try to remove / before v1 in your interface GET method and add that into your base url. In short in any method of retrofit interface don't start with /.
Assuming data is there try making below changes
public class JavaGetAheadAdapter extends RecyclerView.Adapter<JavaGetAheadAdapter.CustomViewHolder> {
List<JavaDatum> GHmdel = new ArrayList(); //new change
Context context;
//removed the list from constructor
public JavaGetAheadAdapter(Context context) {
this.GHmdel = employees;
}
//new method for updating the data items
public void updateData(List<JavaDatum> employees){
this.GHmdel.addAll(employees);
notifyDataSetChanged();
}
#Override
public CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.getaheadnext_item, parent, false);
return new CustomViewHolder(itemView);
}
#Override
public void onBindViewHolder(CustomViewHolder holder, int position) {
holder.employeeName.setText(GHmdel.get(position).getFileName());//
holder.textView.setText(GHmdel.get(position).getCode());
Log.d("adapter",GHmdel.get(position).getFileName());
Log.d("adapter2",GHmdel.get(position).getCode());
}
#Override
public int getItemCount() {
return GHmdel.size();
}
public class CustomViewHolder extends RecyclerView.ViewHolder {
public TextView employeeName;
TextView textView;
public CustomViewHolder(View view) {
super(view);
employeeName = (TextView) view.findViewById(R.id.detailsgetaheadtitle);
textView = view.findViewById(R.id.detailsgetahead);}}}
At the calling side just make the mentioned below changes.
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
/*Create handle for the RetrofitInstance interface*/
progressDialog = new ProgressDialog(getContext());
progressDialog.setMessage("Loading....");
progressDialog.show();
Intent intent = getActivity().getIntent();
String id = intent.getStringExtra("idGHnext");
Log.e("ashwiniiii", String.valueOf(id));
// new changes
generateDataList(view);
GetAheadApiService service = GetAheadApiClient.getRetrofitInstance().create(GetAheadApiService.class);
Call<GetAHeadModelsnext> call = service.getaheadjava(id);
call.enqueue(new Callback<GetAHeadModelsnext>() {
#Override
public void onResponse(Call<GetAHeadModelsnext> call, Response<GetAHeadModelsnext> response) {
progressDialog.dismiss();
List<JavaDatum> retro=response.body().getJavaData();
//new changes
adapter.updateData(retro);
Log.e("helloash", String.valueOf(response.body().getJavaData()));
}
#Override
public void onFailure(Call<GetAHeadModelsnext> call, Throwable t) {
progressDialog.dismiss();
Toast.makeText(getContext(), "Something went wrong...Please try later!", Toast.LENGTH_SHORT).show();
}
});
}
//new changes goes here
private void generateDataList(View view) {
recyclerView = view.findViewById(R.id.nextGHrecycle);
LinearLayoutManager manager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(manager);
recyclerView.setHasFixedSize(true);
adapter = new JavaGetAheadAdapter(getContext());
recyclerView.setAdapter(adapter);
}}
Try and comment below if its working or not still.
friends...
In my project, I am creating a list of images from the server and showing in Recyclerview.
now I want to remove a particular item from the list when I click on the verify button.
I tried to hide holder.itemview but its see again when I scroll.
I just want to remove or hide that item which is verified once.
here is my code :
1) Main activity
public class MainActivity extends AppCompatActivity {
private static final String url = "www.mysite.com/api/api-id-list.php?action=imgs";
private ProgressDialog mDetailProgress;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_main_activity);
LoadRecyclerView();
}
private void LoadRecyclerView() {
mDetailProgress = new ProgressDialog(this);
mDetailProgress.setTitle("Loading images");
mDetailProgress.setMessage("Please Wait...");
mDetailProgress.setCanceledOnTouchOutside(false);
mDetailProgress.show();
final RecyclerView imgList = (RecyclerView) findViewById(R.id.imgList);
imgList.setLayoutManager(new LinearLayoutManager(this));
StringRequest request = new StringRequest(url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.create();
Image[] images = gson.fromJson(response, Image[].class);
imgList.setAdapter(new ImageAdapter(MainActivity.this, images));
mDetailProgress.dismiss();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getBaseContext(), "Something went wrong..!", Toast.LENGTH_LONG);
}
});
RequestQueue queue = Volley.newRequestQueue(this);
queue.add(request);
}}
2) ImageAdapter
public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ImageViewHolder> {
private ProgressDialog mDetailProgress;
private Context context;
private Image[] data;
Button btn_ban, btn_verify;
private View view;
public ImageAdapter (Context context, Image[] data) {
this.context = context;
this.data = data;
}
#NonNull
#Override
public ImageViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int i) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
view = inflater.inflate(R.layout.single_item, parent, false);
return new ImageViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull final ImageViewHolder holder, final int position) {
final Image imagelist = data[position];
holder.userCode.setText(imagelist.getCode());
Glide.with(holder.userImage.getContext()).load(imagelist.getIdPhoto()).into(holder.userImage);
btn_verify.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
holder.itemView.setVisibility(View.GONE);
}
});
}
#Override
public int getItemCount() { return data.length; }
public class ImageViewHolder extends RecyclerView.ViewHolder {
TextView userCode;
ImageView userImage;
public ImageViewHolder(#NonNull View itemView) {
super(itemView);
userImage = (ImageView) itemView.findViewById(R.id.card_iv_img);
userCode = (TextView) itemView.findViewById(R.id.card_tv_code);
btn_ban = (Button) itemView.findViewById(R.id.btn_ban);
btn_verify = (Button) itemView.findViewById(R.id.btn_verify);
}
}}
3) Image.java
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Image {
#SerializedName("per_ID")
#Expose
private String perID;
#SerializedName("code")
#Expose
private String code;
#SerializedName("first_nm")
#Expose
private String firstNm;
#SerializedName("last_nm")
#Expose
private String lastNm;
#SerializedName("photo_ID")
#Expose
private String photoID;
#SerializedName("id_photo")
#Expose
private String idPhoto;
public String getPerID() {
return perID;
}
public void setPerID(String perID) {
this.perID = perID;
}
public Image withPerID(String perID) {
this.perID = perID;
return this;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public Image withCode(String code) {
this.code = code;
return this;
}
public String getFirstNm() {
return firstNm;
}
public void setFirstNm(String firstNm) {
this.firstNm = firstNm;
}
public Image withFirstNm(String firstNm) {
this.firstNm = firstNm;
return this;
}
public String getLastNm() {
return lastNm;
}
public void setLastNm(String lastNm) {
this.lastNm = lastNm;
}
public Image withLastNm(String lastNm) {
this.lastNm = lastNm;
return this;
}
public String getPhotoID() {
return photoID;
}
public void setPhotoID(String photoID) {
this.photoID = photoID;
}
public Image withPhotoID(String photoID) {
this.photoID = photoID;
return this;
}
public String getIdPhoto() {
return idPhoto;
}
public void setIdPhoto(String idPhoto) {
this.idPhoto = idPhoto;
}
public Image withIdPhoto(String idPhoto) {
this.idPhoto = idPhoto;
return this;
}}
First make data not array, but List, so it's easy to remove item;
Secondly set listener for verify button inside ViewHolder and inside OnClick remove item and notify adapter.
btn_verify.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int position = getAdapterPosition();
data.remove(position);
notifyItemRemoved(position);
}
});
Note: Setting OnClickListener inside ViewHolder makes sure that only correct item is removed by using getAdapterPosition() to get correct position.
Position provided by onBindViewHolder might be invalid after new items are inserted.
I am using recyclerView and Adapter to fetch the data in profileActivity
here is my
public class studentDetailsRecyclerActivity extends AppCompatActivity {
//recyclerview to set the details for UI in the student profile activity
private RecyclerView mRecyclerView;
private storeDetailsAdapter mStoreDetailsAdapter;
private List<storeStudentDetails> studentDetailsList;
private FirebaseFirestore dbReference;
private ProgressBar mProgressBar;
private String TAG = studentDetailsRecyclerActivity.class.getSimpleName();
#Override
protected void onCreate(Bundle savedInstanceState) {
dbReference = FirebaseFirestore.getInstance();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycler_details);
mProgressBar = findViewById(R.id.progressbar);
mRecyclerView = (RecyclerView)findViewById(R.id.recyclerView_products);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
studentDetailsList = new ArrayList<>();
mStoreDetailsAdapter = new storeDetailsAdapter(this,studentDetailsList);
mRecyclerView.setAdapter(mStoreDetailsAdapter);
//to get the "details" this is our collection from firestore so we must fetch them
//by calling the addOnSuccessListener
dbReference.collection("details").get()
.addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
#Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) { //we must have to hide the progress bar when the data gets loaded
//here queryDocumentsSnapshot will hold all the "details" which is your collection in firestore
if(!queryDocumentSnapshots.isEmpty()){
//we must have to create empty list so that to store all
//details from DocumentsSnapshots
List<DocumentSnapshot> list = queryDocumentSnapshots.getDocuments();
//enhanced for loop because we have to give every index documentSnapShot
for(DocumentSnapshot d: list){
storeStudentDetails sd = d.toObject(storeStudentDetails.class);
studentDetailsList.add(sd);
Log.d(TAG, "onSuccess: " + sd.toString());
}
//to refresh and sync we must have to use notifyDataSetChanged
mStoreDetailsAdapter.notifyDataSetChanged();
}
}
}) .addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Toast.makeText(getApplicationContext(), "Error getting data!!!", Toast.LENGTH_LONG).show();
}
});
}
}
and here is my storeDetailsAdapter
import java.util.List;
public class storeDetailsAdapter extends RecyclerView.Adapter<storeDetailsAdapter.StudentViewHolder>{
private Context context;
private List<storeStudentDetails> studentDetailsList;
public storeDetailsAdapter(Context context, List<storeStudentDetails> studentDetailsList) {
this.context = context;
this.studentDetailsList = studentDetailsList;
}
#NonNull
#Override
public StudentViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return new StudentViewHolder(
LayoutInflater.from(context).inflate(R.layout.profile_activity, parent, false)
);
}
#Override
public void onBindViewHolder(#NonNull StudentViewHolder holder, int position) {
storeStudentDetails mStoreDetails = studentDetailsList.get(position);
holder.studName.setText(mStoreDetails.getStudentName());
holder.rollNum.setText(mStoreDetails.getRollNo());
holder.bookName.setText( mStoreDetails.getBook());
holder.fine.setText("Fine:" + mStoreDetails.getFine());
holder.dept.setText(mStoreDetails.getDept());
}
#Override
public int getItemCount() {
return studentDetailsList.size();
}
class StudentViewHolder extends RecyclerView.ViewHolder {
TextView studName,rollNum,bookName,dept,fine;
public StudentViewHolder(View itemView) {
super(itemView);
studName=itemView.findViewById(R.id.studentName_prof);
rollNum = itemView.findViewById(R.id.rollNumber_prof);
bookName = itemView.findViewById(R.id.bookName_prof);
fine = itemView.findViewById(R.id.fineAmt_prof);
dept = itemView.findViewById(R.id.department_prof);
}
}
}
and here is my StoreStudentDetails class:
public class storeStudentDetails implements Serializable {
private String studentName;
private String rollNo;
private String book;
private Double fine;
private String dept;
#Exclude private String id;
public storeStudentDetails() {
}
public storeStudentDetails(String studentName, String rollNo,String book, double fine ,String dept) {
this.studentName = studentName;
this.rollNo = rollNo;
this.book = book;
this.fine = fine;
this.dept = dept;
}
public void setId(String id) {
this.id = id;
}
public String getStudentName() {
return studentName;
}
public String getRollNo() {
return rollNo;
}
public String getBook() {
return book;
}
public Double getFine() {
return fine;
}
public String getDept() {
return dept;
}
public String getId() {
return id;
}
}
To solve this, please move the following lines of code:
mStoreDetailsAdapter = new storeDetailsAdapter(this,studentDetailsList);
mRecyclerView.setAdapter(mStoreDetailsAdapter);
Right before the following line of code:
mStoreDetailsAdapter.notifyDataSetChanged();
And this is because onSuccess() method has an asynchronous behavior and by the time you are setting the adapter outside the callback your list is empty.
As you can see, the easiest solution for this problem is to move those lines of code inside the callback. but if you want to use the value of your studentDetailsList outside the onSuccess() method, I recommend you see the last part of my anwser from this post in which I have explained how it can be done using a custom callback. You can also take a look at this video for a better understanding.
I am trying to make a call to this api and am having difficulty as the response.body() is returning null.
http://demo.museum.vebrary.vn/api/stuff/getall
I want to get stuff name of list and show to my recyclerview.
My model:
public class SOAnswersResponse {
#SerializedName("StuffModels")
#Expose
private List<StuffModel> stuffModels = null;
public List<StuffModel> getStuffModels() {
return stuffModels;
}
public void setStuffModels(List<StuffModel> stuffModels) {
this.stuffModels = stuffModels;
}
and
public class StuffModel {
#SerializedName("STUFFID")
#Expose
private Integer sTUFFID;
#SerializedName("STUFFCODE")
#Expose
private String sTUFFCODE;
#SerializedName("STUFFNAME")
#Expose
private String sTUFFNAME;
#SerializedName("STUFFNOTE")
#Expose
private String sTUFFNOTE;
#SerializedName("STUFFORDER")
#Expose
private Integer sTUFFORDER;
#SerializedName("CUSTOMERID")
#Expose
private String cUSTOMERID;
#SerializedName("EXHIBITS")
#Expose
private List<Object> eXHIBITS = null;
Json response
{
"StuffModels":[
{
"STUFFID":2,
"STUFFCODE":"Gi",
"STUFFNAME":"Giấy",
"STUFFNOTE":"",
"STUFFORDER":2,
"CUSTOMERID":"CAMAU",
"EXHIBITS":[
]
},
ApiUtils Class
public class ApiUtils {
private ApiUtils() {
}
public static final String BASE_URL = "http://demo.museum.vebrary.vn/api/";
public static SOService getSOService() {
return RetrofitClient.getClient(BASE_URL).create(SOService.class);
}
}
Service interface
public interface SOService {
#GET("/stuff/getall")
Call<SOAnswersResponse> getAnswers();
}
RetrofitClient Class
public class RetrofitClient {
private static Retrofit retrofit = null;
public static Retrofit getClient(String baseUrl) {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
My RecyclerView adapter
public class CategogyNameRecyclerViewAdapter extends RecyclerView.Adapter<CategogyNameRecyclerViewAdapter.ViewHolder> {
private List<StuffModel> mItems;
private Context mContext;
private PostItemListener mItemListener;
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public TextView titleTv;
PostItemListener mItemListener;
public ViewHolder(View itemView, PostItemListener postItemListener) {
super(itemView);
titleTv = itemView.findViewById(R.id.tvListMenuCategogy);
this.mItemListener = postItemListener;
itemView.setOnClickListener(this);
}
#Override
public void onClick(View view) {
StuffModel item = getItem(getAdapterPosition());
this.mItemListener.onPostClick(item.getSTUFFID());
notifyDataSetChanged();
}
}
public CategogyNameRecyclerViewAdapter(Context context, List<StuffModel> posts, PostItemListener itemListener) {
mItems = posts;
mContext = context;
mItemListener = itemListener;
}
#Override
public CategogyNameRecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View postView = inflater.inflate(R.layout.item_list_text, parent, false);
ViewHolder viewHolder = new ViewHolder(postView, this.mItemListener);
return viewHolder;
}
#Override
public void onBindViewHolder(CategogyNameRecyclerViewAdapter.ViewHolder holder, int position) {
StuffModel item = mItems.get(position);
TextView textView = holder.titleTv;
textView.setText(item.getSTUFFNAME());
}
#Override
public int getItemCount() {
return mItems.size();
}
public void updateAnswers(List<StuffModel> items) {
mItems = items;
notifyDataSetChanged();
}
private StuffModel getItem(int adapterPosition) {
return mItems.get(adapterPosition);
}
public interface PostItemListener {
void onPostClick(long id);
}
}
And my main activity
public class Testttt extends AppCompatActivity {
private CategogyNameRecyclerViewAdapter mAdapter;
private RecyclerView mRecyclerView;
private SOService mService;
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView(R.layout.test );
mService = ApiUtils.getSOService();
mRecyclerView = (RecyclerView) findViewById(R.id.rcvCategogyNameMenuTest);
mAdapter = new CategogyNameRecyclerViewAdapter(this, new ArrayList<StuffModel>(0), new CategogyNameRecyclerViewAdapter.PostItemListener() {
#Override
public void onPostClick(long id) {
Toast.makeText(Testttt.this, "Post id is" + id, Toast.LENGTH_SHORT).show();
}
});
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(layoutManager);
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.setHasFixedSize(true);
RecyclerView.ItemDecoration itemDecoration = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST);
mRecyclerView.addItemDecoration(itemDecoration);
loadAnswers();
}
public void loadAnswers() {
mService.getAnswers().enqueue(new Callback<SOAnswersResponse>() {
#Override
public void onResponse(Call<SOAnswersResponse> call, Response<SOAnswersResponse> response) {
Toast.makeText(Testttt.this, "333333333333333333"+response.body(), Toast.LENGTH_SHORT).show();
if(response.isSuccessful()) {
mAdapter.updateAnswers(response.body().getStuffModels());
Log.d("AnswersPresenter", "posts loaded from API");
}else {
int statusCode = response.code();
}
}
#Override
public void onFailure(Call<SOAnswersResponse> call, Throwable t) {
showErrorMessage();
Log.d("AnswersPresenter", "error loading from API");
}
});
}
public void showErrorMessage() {
Toast.makeText(this, "Error loading posts", Toast.LENGTH_SHORT).show();
}
}
The first thing that came in my mind:
Your
public static final String BASE_URL = "http://demo.museum.vebrary.vn/api/";
has a "/" at the the end and your
#GET("/stuff/getall")
Call<SOAnswersResponse> getAnswers();
starts with a "/". So there is a double backslash in the url that might leads to the 404 code. Does this solve the problem?
When i call your URL i receive XML. Maybe the API is not configured correctly?
Change your Service interface
public interface SOService {
#GET("stuff/getall")
Call<SOAnswersResponse> getAnswers();
}
it occurred because you have use start with backslash it already added in your base url
am trying to Fetch the movies data from Mysql DB and show it to Recycler view
but when i run the app nothing shows
here is code i am using Retrofite Library
but i can't parse the Data to the Recycler view
i've made Adapter and Model Class normally like the Json
MainActivity.class
public class MainActivity extends AppCompatActivity {
private static final String url="http://192.168.1.109/stu/";
RecyclerView recyclerViewMovies;
List<MovieListsModels> movies;
MoviesAdapter adapter;
TextView Errortxt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Errortxt = (TextView)findViewById(R.id.txterror);
recyclerViewMovies = (RecyclerView)findViewById(R.id.recyclerview);
recyclerViewMovies.setHasFixedSize(true);
recyclerViewMovies.setLayoutManager(new LinearLayoutManager(this));
movies = new ArrayList<>();
loadDatafromServer();
}
private void loadDatafromServer() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create())
.build();
Api api = retrofit.create(Api.class);
Call<MovieListsModels> call = api.ShowMoviesData();
call.enqueue(new Callback<MovieListsModels>() {
#Override
public void onResponse(Call<MovieListsModels> call, Response<MovieListsModels> response) {
try {
MovieListsModels movie = response.body();
adapter = new MoviesAdapter(MainActivity.this, (List<MovieListsModels>) movie);
recyclerViewMovies.setAdapter(adapter);
}
catch (Exception e) {
e.printStackTrace();
}
}
#Override
public void onFailure(Call<MovieListsModels> call, Throwable t) {
Errortxt.setText(t.getMessage().toString());
}
});
}
this is the interface of the methods
Api.class Interface
public interface Api {
#GET("config.php")
Call<MovieListsModels> ShowMoviesData();
}
MovieLists.class
public class MovieListsModels {
public MovieListsModels() {
}
int id;
String movie_name;
String movie_image;
String movie_genre;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getMovie_name() {
return movie_name;
}
public void setMovie_name(String movie_name) {
this.movie_name = movie_name;
}
public String getMovie_image() {
return movie_image;
}
public void setMovie_image(String movie_image) {
this.movie_image = movie_image;
}
public String getMovie_genre() {
return movie_genre;
}
public void setMovie_genre(String movie_genre) {
this.movie_genre = movie_genre;
}
public MovieListsModels(int id, String movie_name, String movie_image, String movie_genre) {
this.id = id;
this.movie_name = movie_name;
this.movie_image = movie_image;
this.movie_genre = movie_genre;
}
}
MovieAdapter.class
public class MoviesAdapter extends RecyclerView.Adapter<MoviesAdapter.MovieHolderView> {
private Context mContext;
private List<MovieListsModels> MovieList = new ArrayList<>();
public MoviesAdapter(Context mContext, List<MovieListsModels> movieList) {
this.mContext = mContext;
MovieList = movieList;
}
#NonNull
#Override
public MovieHolderView onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_item,parent,false);
MovieHolderView holder = new MovieHolderView(view);
return holder;
}
#Override
public void onBindViewHolder(#NonNull MovieHolderView holder, int position) {
MovieListsModels list = MovieList.get(position);
holder.txtName.setText(list.getMovie_name());
holder.txtGenre.setText(list.getMovie_genre());
Picasso.get()
.load(list.getMovie_image())
.into(holder.imgMovie);
}
#Override
public int getItemCount() {
return MovieList.size();
}
public class MovieHolderView extends RecyclerView.ViewHolder {
TextView txtName,txtGenre;
ImageView imgMovie;
public MovieHolderView(View itemView) {
super(itemView);
txtName =(TextView)itemView.findViewById(R.id.movieName);
txtGenre =(TextView)itemView.findViewById(R.id.movieGenre);
imgMovie =(ImageView)itemView.findViewById(R.id.movieImg);
}
}
}
If you receive a list of movies is better because you expect a list, I suppose
public void onResponse(Call<MovieListsModels> call, Response<MovieListsModels> response) {
try {
List<MovieListsModels> movie = response.body();
adapter = new MoviesAdapter(MainActivity.this, movies);
And I believe that not executing the notifyDataSetChanged, you can added like that:
private Context mContext;
private List<MovieListsModels> MovieList = new ArrayList<>();
public MoviesAdapter(Context mContext, List<MovieListsModels> movieList) {
this.mContext = mContext;
MovieList = movieList;
notifiyDataSetChanged();
If you are having json response of the form {..}, you are having an object response and you should expect an object as you have done i.e, Call<YourObject>
If you are having json response of the form [..], you are having an array response and you should expect an array i.e, Call<List<YourObject>>
In your case, i hope its an array(second case), So make changes as per the above answer done by #Guillodacosta
First don't forget to add the internet permission in your manifest file
<uses-permission android:name="android.permission.INTERNET" />
Second try this
Picasso.with(mContext).load(list.getMovie_image()).into(holder.imgMovie);