why my data cant show, the error in logcat is "No adapter attached; skipping layout" and "the mapper function return null",
this my viewModel i get my api in here Model.java
public class Model extends ViewModel {
private static final String API_KEY = "my-api";
private MutableLiveData<ArrayList<MoviesItems>> listMovies = new MutableLiveData<>();
void setMovies(){
AsyncHttpClient client = new AsyncHttpClient();
final ArrayList<MoviesItems> listItems = new ArrayList<>();
String url = "https://api.themoviedb.org/3/discover/movie?api_key=" + API_KEY + "&language=en-US";
client.get(url, new AsyncHttpResponseHandler() {
#Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
try {
String result = new String(responseBody);
JSONObject object = new JSONObject(result);
JSONArray list = object.getJSONArray("results");
for (int i = 0;1 < list.length(); i++){
JSONObject movies = list.getJSONObject(i);
MoviesItems moviesItems = new MoviesItems(movies);
listItems.add(moviesItems);
}
listMovies.getValue();
}catch (Exception e){
Log.d("Exception", e.getMessage());
}
}
#Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
Log.d("onFailure", error.getMessage());
}
});
}
public LiveData<ArrayList<MoviesItems>> getMovies(){
return listMovies;
}
}
and this my fragment i think my error in here MoviesFragment.java
View rootView = inflater.inflate(R.layout.fragment_movies, container, false);
viewModel = ViewModelProviders.of(this).get(Model.class);
viewModel.getMovies().observe(this, getMovie);
//pb = rootView.findViewById(R.id.pb);
moviesAdapter = new MoviesAdapter(getActivity());
moviesAdapter.notifyDataSetChanged();
rv_grid_movies = rootView.findViewById(R.id.rv_grid_movies);
RecyclerView.LayoutManager manager = new GridLayoutManager(getActivity(), 2);
rv_grid_movies.setLayoutManager(manager);
rv_grid_movies.addItemDecoration(new GridSpacingItemDecoration(2, dpToPx(3), true));
rv_grid_movies.setItemAnimator(new DefaultItemAnimator());
rv_grid_movies.setAdapter(moviesAdapter);
rv_grid_movies.setHasFixedSize(true);
return rootView;
}
private Observer<ArrayList<MoviesItems>> getMovie = new Observer<ArrayList<MoviesItems>>() {
#Override
public void onChanged(#Nullable ArrayList<MoviesItems> moviesItems) {
if (moviesItems != null){
moviesAdapter.setData(moviesItems);
//loadData(false);
}
}
};
this my modelitems this place i get jsonObject MoviesItems.java
public MoviesItems(JSONObject object){
try {
//JSONArray array = object.getJSONArray("results");
int id = object.getInt("id");
String title = object.getString("title");
String overview = object.getString("overview");
String poster_path = object.getString("poster_path");
String release_date = object.getString("release_date");
this.id = id;
this.title = title;
this.overview = overview;
this.poster_path = poster_path;
this.release_date = release_date;
}catch (Exception e){
e.printStackTrace();
}
}
and this my adapter MoviesAdapter.java
public class MoviesAdapter extends RecyclerView.Adapter<MoviesAdapter.MoviesViewHolder> {
private ArrayList<MoviesItems> mItems;
private Context context;
public MoviesAdapter(Context context){
this.context = context;
}
public void setData(ArrayList<MoviesItems> mItems) {
mItems.clear();
mItems.addAll(mItems);
notifyDataSetChanged();
}
public void addItems(final MoviesItems moviesItems){
mItems.add(moviesItems);
notifyDataSetChanged();
}
#Override
public void onAttachedToRecyclerView(#NonNull RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
#Override
public void onDetachedFromRecyclerView(#NonNull RecyclerView recyclerView) {
super.onDetachedFromRecyclerView(recyclerView);
}
private void clearData(){
mItems.clear();
}
#NonNull
#Override
public MoviesViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.grid_movies, viewGroup, false);
final MoviesViewHolder holder = new MoviesViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(#NonNull MoviesViewHolder moviesViewHolder, int i) {
moviesViewHolder.bind(mItems.get(i));
}
#Override
public int getItemCount() {
return (mItems != null) ? mItems.size(): 0;
}
public class MoviesViewHolder extends RecyclerView.ViewHolder {
TextView txt_title_movies, txt_date_movies, txt_description_movies;
ImageView img_movies;
String url = "https://image.tmdb.org/t/p/original";
public MoviesViewHolder(#NonNull View itemView) {
super(itemView);
txt_title_movies = itemView.findViewById(R.id.txt_titlemovie);
txt_date_movies = itemView.findViewById(R.id.txt_datemovie);
txt_description_movies = itemView.findViewById(R.id.txt_descriptionmovie);
img_movies = itemView.findViewById(R.id.img_movie);
}
public void bind(MoviesItems moviesItems) {
txt_title_movies.setText(moviesItems.getTitle());
txt_date_movies.setText(moviesItems.getRelease_date());
txt_description_movies.setText(moviesItems.getOverview());
Glide.with(context)
.load(url+moviesItems.getPoster_path())
.into(img_movies);
}
}
}
and this json i want get
{
"page": 1,
"total_results": 432039,
"total_pages": 21602,
"results": [
{
"vote_count": 672,
"id": 384018,
"video": false,
"vote_average": 6.5,
"title": "Fast & Furious Presents: Hobbs & Shaw",
"popularity": 317.725,
"poster_path": "/keym7MPn1icW1wWfzMnW3HeuzWU.jpg",
"original_language": "en",
"original_title": "Fast & Furious Presents: Hobbs & Shaw",
"genre_ids": [
28
],
"backdrop_path": "/hpgda6P9GutvdkDX5MUJ92QG9aj.jpg",
"adult": false,
"overview": "A spinoff of The Fate of the Furious, focusing on Johnson's US Diplomatic Security Agent Luke Hobbs forming an unlikely alliance with Statham's Deckard Shaw.",
"release_date": "2019-08-01"
},
and this my error
java.lang.NullPointerException: The mapper function returned a null value.
at io.reactivex.d.b.b.a(ObjectHelper.java:39)
at io.reactivex.d.e.b.g$a.onNext(ObservableMap.java:59)
at io.reactivex.d.e.b.i$a.run(ObservableScalarXMap.java:248)
at io.reactivex.d.e.b.f.b(ObservableJust.java:35)
at io.reactivex.h.a(Observable.java:11442)
at io.reactivex.d.e.b.g.b(ObservableMap.java:33)
at io.reactivex.h.a(Observable.java:11442)
at io.reactivex.d.e.b.l$b.run(ObservableSubscribeOn.java:96)
at io.reactivex.a.b.b$b.run(HandlerScheduler.java:109)
at android.os.Handler.handleCallback(Handler.java:754)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:165)
at android.app.ActivityThread.main(ActivityThread.java:6375)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:802)
Screen Image
In MoviesFragment.java inside observer of ArrayList<MoviesItems> onChanged() method call moviesAdapter.notifyDataSetChanged(); after the moviesAdapter.setData(moviesItems);
On response of your API, you aren't setting up newly received value in LiveData variable.
So make below changes and your code will work.
void setMovies(){
AsyncHttpClient client = new AsyncHttpClient();
final ArrayList<MoviesItems> listItems = new ArrayList<>();
String url = "https://api.themoviedb.org/3/discover/movie?api_key=" + API_KEY + "&language=en-US";
client.get(url, new AsyncHttpResponseHandler() {
#Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
try {
String result = new String(responseBody);
JSONObject object = new JSONObject(result);
JSONArray list = object.getJSONArray("results");
for (int i = 0;1 < list.length(); i++){
JSONObject movies = list.getJSONObject(i);
MoviesItems moviesItems = new MoviesItems(movies);
listItems.add(moviesItems);
}
// Replace below line.
// listMovies.getValue();
// With
listMovies.setValue(listItems);
}catch (Exception e){
Log.d("Exception", e.getMessage());
}
}
#Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
Log.d("onFailure", error.getMessage());
}
});
}
UPDATE 2:
Change position of code as below in fragment.
viewModel = ViewModelProviders.of(this).get(Model.class);
//pb = rootView.findViewById(R.id.pb);
moviesAdapter = new MoviesAdapter(getActivity());
moviesAdapter.notifyDataSetChanged();
rv_grid_movies = rootView.findViewById(R.id.rv_grid_movies);
RecyclerView.LayoutManager manager = new GridLayoutManager(getActivity(), 2);
rv_grid_movies.setLayoutManager(manager);
rv_grid_movies.addItemDecoration(new GridSpacingItemDecoration(2, dpToPx(3), true));
rv_grid_movies.setItemAnimator(new DefaultItemAnimator());
rv_grid_movies.setAdapter(moviesAdapter);
rv_grid_movies.setHasFixedSize(true);
// Add observer here, after initialising adapter and recyclerview.
viewModel.getMovies().observe(this, getMovie);
Related
I want to know how to get a URL in a json and open it in a setOnClickListener of a RecyclerView
My API Json
{
{
"link": "https://example.com"
},
{
...
}
}
My URLModel
public class URLModel {
String Link;
public MonlixModel(String link) {
Link = link;
}
public String getLink() {
return Link;
}
public void setLink(String link) {
Link = link;
}
}
I think the problem comes from my Adapter but I don't know where and how to fix it
public class URLAdapter extends RecyclerView.Adapter<URLAdapter.URLHolder> {
Context mContext;
List<URLModel> urlModels;
public URLAdapter(Context mContext, List<URLAdapter> urlModels) {
this.mContext = mContext;
this.urlModels= urlModels;
}
#NonNull
#Override
public URLHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.recycler_view, parent, false);
return new URLHolder(view);
}
#Override
public void onBindViewHolder(#NonNull URLHolder holder, int position) {
holder.recyclerUrl.setOnClickListener(view -> {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(urlModels.get(position).getLink()));
mContext.startActivity(intent);
});
}
#Override
public int getItemCount() {
return urlModels.size();
}
public static class URLHolder extends RecyclerView.ViewHolder {
private final RecyclerView recyclerUrl;
public URLHolder(#NonNull View itemView) {
super(itemView);
recyclerUrl = itemView.findViewById(R.id.recycler_url);
}
}
}
And finally my Main.java
private void getData() {
RequestQueue requestQueue = Volley.newRequestQueue(this);
#SuppressLint("NotifyDataSetChanged") JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, Apis.JSON_API, null, response -> {
for(int i = 0; i <=response.length(); i++){
try {
JSONObject jsonObject = response.getJSONObject(i);
URLModelList.add(new URLModel(
jsonObject.getString("link")
));
} catch (JSONException e) {
e.printStackTrace();
}
}
URLAdapter adapter = new URLAdapter(getApplicationContext(), URLModelList);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
Toast.makeText(Main.this, "Success", Toast.LENGTH_SHORT).show();
}, error -> Toast.makeText(Main.this, error.getMessage(), Toast.LENGTH_SHORT).show());
requestQueue.add(jsonArrayRequest);
}
I use Volley, I don't know if it's the most adapted but I succeeded with that, I also tried to change in my URLModel the String by a Uri. But I noticed that the JSONObject didn't have a getUri or something like that
In my Logcat it tells me it's a null object reference
Any help will be much appreciated!
Please check your JSON format first,
{
"key_name1": {
"link": "https://example.com"
},
"key_name2":{
...
}
}
make sure your inner object should have key_name as above example or make this JSONObject to JSONArray like this
[
{
"link": "https://example.com"
},
{
}
]
I am having this issue for some time now, when I retrieved data from a database using volley and then show it in a recycler view, if the items are 8 or less than 8 then it shows them without any problem, but if I retrieve more than 8 items from database then the activity closes/crashes and goes back to the main activity. I don't get any error in the run console in android studio. I am retrieving 3 things from database for a single item, 2 strings and an image. I don't think the error is in the php file which is used to get the data as that I have checked and it retrieves without any issue. I have searched android documentation of recycler view but couldn't find anything
NOTE: I am getting image as string and then converting to bitmap.
if there is anything else needed then I can provide them without any issue.
the code is below:
Method which is used to call the recycler view activity which has problems.
public void onViewAttendanceButtonClick(int position, String courseCode,
String courseName, String batchName) {
if (MainActivity.teacherData != null) {
seeAttendanceDetails(MainActivity.teacherData.getEmail(),courseCode,
batchName, courseName, getDateAndTime);
}
}
private void seeAttendanceDetails(final String email,final String
courseCode, final String batchName,final String courseName, final String
date) {
StringRequest stringRequest = new StringRequest(Request.Method.POST,
seeAttendanceDetails, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
JSONArray jsonArray = new JSONArray(response);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject data = jsonArray.getJSONObject(i);
String fullName = data.getString("full_name");
String Email = data.getString("email");
String photo = data.getString("photo");
Toast.makeText(CoursesActivity.this, courseCode,
Toast.LENGTH_LONG).show();
Attendance attendance1 = new Attendance(fullName, Email, photo);
attendanceArrayList.add(attendance1);
}
if (!attendanceArrayList.isEmpty()) {
Intent intent = new Intent(CoursesActivity.this,
ShowAttendanceActivity.class);
Bundle bundle = new Bundle();
bundle.putString("attendanceCourseCode",
courseCode);
bundle.putString("attendanceCourseName",
courseName);
bundle.putString("attendanceBatchName", batchName);
bundle.putSerializable("attendanceList",
attendanceArrayList);
intent.putExtras(bundle);
startActivity(intent);
}
else if (RegistrationActivity.teacherData != null) {
//
seeAttendanceDetails(RegistrationActivity.teacherData.getEmail(),
batchName,courseName, getDateAndTime);
// Toast.makeText(CoursesActivity.this,"No Record
Found",Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(CoursesActivity.this,
error.getMessage().toString(), Toast.LENGTH_SHORT).show();
}
}) {
public Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("email", email);
params.put("batch", batchName);
params.put("date", date);
params.put("courseCode",courseCode);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
}
<br/>
The recycler view activity code
public class ShowAttendanceActivity extends AppCompatActivity implements
ShowAttendanceActivityAdapter.RemoveAttendanceClickListener {
private RecyclerView recyclerView;
private String courseCode,courseName,batchName;
private ArrayList<Attendance> attendanceArrayList = new ArrayList<>();
private TextView
textViewCourseCode,textViewCourseName,textViewBatchName;
private String deleteStudentAttendance =
"https://asuiot.umargulzar.com/Teacher%20API%2
0Files/deleteStudentattendance.php";
int success;
private String TAG_SUCCESS = "success";
private String TAG_MESSAGE = "message";
private String getDateAndTime;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_attendance);
recyclerView = findViewById(R.id.attendance_details_recyclerView);
textViewCourseCode = findViewById(R.id.SA_textView_course_code);
textViewCourseName = findViewById(R.id.SA_textView_course_name);
textViewBatchName = findViewById(R.id.SA_textView_Batch);
LinearLayoutManager layoutManager = new LinearLayoutManager(this, RecyclerView.VERTICAL,false);
recyclerView.setLayoutManager(layoutManager);
Calendar calendar = Calendar.getInstance();
// SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MMM-yyy hh:mm:ss a");
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyy-MM-dd");
String dateTime = simpleDateFormat.format(calendar.getTime());
this.getDateAndTime = dateTime;
courseCode = (String) getIntent().getStringExtra("attendanceCourseCode");
courseName = (String) getIntent().getStringExtra("attendanceCourseName");
batchName = (String) getIntent().getStringExtra("attendanceBatchName");
attendanceArrayList = (ArrayList<Attendance>) getIntent().getExtras().getSerializable("attendanceList");
textViewCourseCode.setText(courseCode);
textViewCourseName.setText(courseName);
textViewBatchName.setText(batchName);
recyclerView.setAdapter(new ShowAttendanceActivityAdapter(attendanceArrayList,this));
}
#Override
public void onRemoveAttendanceClick(int position,String email) {
// Toast.makeText(ShowAttendanceActivity.this,"The Email of Student:"+email,Toast.LENGTH_LONG).show();
if(MainActivity.teacherData !=null){
deleteStudentAttendance(email,getDateAndTime,courseCode);
}
}
private void deleteStudentAttendance(final String email, final String getDateAndTime,final String courseCode) {
StringRequest stringRequest = new StringRequest(Request.Method.POST, deleteStudentAttendance, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
success = jsonObject.getInt(TAG_SUCCESS);
if (success == 1) {
Toast.makeText(ShowAttendanceActivity.this, jsonObject.getString(TAG_MESSAGE)+" Plz refresh the page.", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(ShowAttendanceActivity.this, jsonObject.getString(TAG_MESSAGE), Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
}){
public Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("Email", email);
params.put("Date",getDateAndTime);
params.put("CourseCode",courseCode);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
}
the recycler view activity adapter
public class ShowAttendanceActivityAdapter extends RecyclerView.Adapter<ShowAttendanceActivityAdapter.AttendanceView> {
private ArrayList<Attendance> attendanceData;
Bitmap bitmap;
private RemoveAttendanceClickListener removeAttendanceClickListener;
public ShowAttendanceActivityAdapter(ArrayList<Attendance> attendanceData,RemoveAttendanceClickListener removeAttendanceClickListener){
this.attendanceData = attendanceData;
this.removeAttendanceClickListener = removeAttendanceClickListener;
}
#NonNull
#Override
public AttendanceView onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.show_attendance_data,parent,false);
return new AttendanceView(view,removeAttendanceClickListener);
}
#Override
public void onBindViewHolder(#NonNull AttendanceView holder, int position) {
Attendance attendance = attendanceData.get(position);
holder.textViewName.setText(attendance.getStudentName());
// holder.textViewEmail.setText(attendance.getStudentEmail());
holder.textViewEmail.setText(attendance.getStudentEmail());
decodeStringToImage(attendance.getStudentPhoto());
holder.imageViewPhoto.setImageBitmap(bitmap);
}
#Override
public int getItemCount() {
return attendanceData.size();
}
public void decodeStringToImage(String photo) {
// Bitmap bitmap = photo.
byte[] bytes = Base64.decode(photo, Base64.DEFAULT);
bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
}
public class AttendanceView extends RecyclerView.ViewHolder implements View.OnClickListener{
TextView textViewName, textViewEmail;
ImageView imageViewPhoto;
Button buttonRemoveCourse;
RemoveAttendanceClickListener removeAttendanceClickListener;
public AttendanceView(#NonNull View itemView,RemoveAttendanceClickListener removeAttendanceClickListener) {
super(itemView);
textViewName = itemView.findViewById(R.id.textView_name);
textViewEmail = itemView.findViewById(R.id.textView_Email);
imageViewPhoto = itemView.findViewById(R.id.textView_SA_Photo);
this.removeAttendanceClickListener = removeAttendanceClickListener;
buttonRemoveCourse = itemView.findViewById(R.id.btn_remove_attendance);
buttonRemoveCourse.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_remove_attendance:
removeAttendanceClickListener.onRemoveAttendanceClick(getAdapterPosition(),textViewEmail.getText().toString());
break;
}
}
}
public interface RemoveAttendanceClickListener{
void onRemoveAttendanceClick(int position,String email);
}
}
You are probably getting OutOfMemory exception while converting bytes array in to bitmap if the image sizes are huge.
Add try catch in your decodeStringToImage method where you are converting bitmaps and check the log when you load more than 8 images.
I am new to android development and I started working on an expandable RecyclerView. Now My problem is that my child (sub-modules) list is populated dynamically from the selected parent (Module) on the expandable RecyclerView. I tried using a for loop but only the last child list is populated when I open. I was just wondering if there is a way I could implement an item click listener to just return the single selected module.
Here is my adapter:
public class ReportAdapter extends ExpandableRecyclerViewAdapter<ModuleViewHolder, DoiViewHolder> {
Context context;
public ReportAdapter(List<? extends ExpandableGroup> groups, Context context) {
super(groups);
this.context = context;
}
#Override
public ModuleViewHolder onCreateGroupViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_module,parent,false);
return new ModuleViewHolder(view);
}
#Override
public DoiViewHolder onCreateChildViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_doi,parent,false);
return new DoiViewHolder(view);
}
#Override
public void onBindChildViewHolder(DoiViewHolder holder, int flatPosition, ExpandableGroup group, int childIndex) {
ModuleDoi moduleDoi =(ModuleDoi) group.getItems().get(childIndex);
holder.setDoiName(moduleDoi.getName());
}
#Override
public void onBindGroupViewHolder(ModuleViewHolder holder, int flatPosition, ExpandableGroup group) {
holder.setModuleName(group.getTitle());
}
}
Here is my Parent Holder:
public class ModuleViewHolder extends GroupViewHolder implements View.OnClickListener{
TextView moduleName ;
public ModuleViewHolder(View itemView) {
super(itemView);
moduleName = (TextView) itemView.findViewById(R.id.module_name);
}
public void setModuleName(String name){
moduleName.setText(name);
}
}
Here is my Childs holder (Sub Modules)
public class DoiViewHolder extends ChildViewHolder {
TextView doiName;
public DoiViewHolder(View itemView) {
super(itemView);
doiName = (TextView) itemView.findViewById(R.id.module_doi_name);
}
public void setDoiName(String name){
doiName.setText(name);
}
}
Here is where I set the adapter. I am using the
moduleDoiList = fetchModuleMenus(APPID, MODULENAME);
fetch the submodules list from a json file. the app id in this case is a constant and the MODULENAME is obtained from the Modules Array list.
JSONArray jsonArray = jsonObject.getJSONArray("RESPONSE");
moduleDoiList = new ArrayList<>();
appModulesList = new ArrayList<>();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject eachModuleObject = jsonArray.getJSONObject(i);
MODULES modules = new MODULES(eachModuleObject.getString("MODULENAME"),
eachModuleObject.getString("MODULEID"));
ModulesList.add(modules);
for(int j = 0; j<ModulesList.size(); j++) {
String moduleTitle = ModulesList.get(i).getModuleName();
String MODULENAME = ModulesList.get(i).getModuleName();
moduleDoiList = fetchModuleMenus(APPID, MODULENAME);
appModulesList.add(new AppModules(moduleTitle, moduleDoiList));
}
}
reportAdapter = new ReportAdapter(appModulesList, POIDocsReportsActivity.this);
reportAdapter.notifyDataSetChanged();
rv.setAdapter(reportAdapter);
This is my fetchModulesMenu method:
public List<ModuleDoi> fetchModuleMenus(final String appId, final String moduleId) {
moduleDoiList = new ArrayList<>();
Thread thread = new Thread() {
Handler handler = new Handler();
ProgressDialog progressDialog;
boolean error;
#Override
public void run() {
handler.post(new Runnable() {
#Override
public void run() {
progressDialog = new ProgressDialog(POIDocsReportsActivity.this, R.style.Theme_AppCompat_DayNight_Dialog_Alert);
progressDialog.setMessage("Please wait...");
progressDialog.setCancelable(false);
progressDialog.show();
}
});
String url = AppConfig.SERVER_URL + "load_module_menus.php";
HashMap hashMap = new HashMap();
hashMap.put("APPID", appId);
hashMap.put("MODULEID", moduleId);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,url, new JSONObject(hashMap), new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
progressDialog.dismiss();
JSONObject jsonObject = new JSONObject(response.toString());
error = jsonObject.getBoolean("ERROR");
if (error){
Toast.makeText(POIDocsReportsActivity.this, "MENUS NOT FOUND ", Toast.LENGTH_LONG).show();
}
else{
JSONArray jsonArray = jsonObject.getJSONArray("RESPONSE");
for (int i = 0; i < jsonArray.length(); i++){
JSONObject eachMenu = jsonArray.getJSONObject(i);
ModuleDoi moduleDoi = new ModuleDoi(eachMenu.getString("POINAME"), eachMenu.getString("POICODE"));
moduleDoiList.add(moduleDoi);
}
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(POIDocsReportsActivity.this, "CONNECTION ERROR ", Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
progressDialog.dismiss();
Toast.makeText(POIDocsReportsActivity.this, "NO RESPONSE", Toast.LENGTH_LONG).show();
}
});
AppController.getInstance().addToRequestQueue(jsonObjectRequest);
}
};
thread.run();
return moduleDoiList;
}
I am using the Thoughtbot Custom Expandable Recycler View
i'm tyring to get some informations from Tumblr blogs using Tumblr API ,now i'm tyring to get Name (blog_name) and Image Cover (Header_image) from Json below
Json Tree Picture
my code below works fine but when i set Name and ImageCover into TextView and ImageView something weird happening ,i find name of first blog in 3rd recyclview item or 4th the same thing for Image cover, also when
i scroll top to bottom of recyclview the name and image change randomly i do not know why.
here's myAdapterTumblr.java ,and
sorry if I dont explain well
public class myAdapterTumblr extends RecyclerView.Adapter<myAdapterTumblr.ViewHolder> {
RequestQueue requestQueue;
TextView name;
private List<PostTumblr> postTumblrs;
private Context context;
private RecyclerViewClickListenerTumblr mClickListener;
private String APIKey = "";
public myAdapterTumblr(List<PostTumblr> postTumblrs, Context context, RecyclerViewClickListenerTumblr clickListener) {
this.postTumblrs = postTumblrs;
this.context = context;
this.mClickListener = clickListener;
}
public List<PostTumblr> getItems() {
return postTumblrs;
}
#Override
public myAdapterTumblr.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.tumblr_profil,
parent, false);
return new myAdapterTumblr.ViewHolder(v);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.bindModel(postTumblrs.get(position));
}
#Override
public int getItemCount() {
return postTumblrs.size();
}
public void getJsonDataCover(PostTumblr mPost, final RequestOptions options, final ProgressBar progressBarCover, final ImageView imgcover) {
String jsonStr = "https://api.tumblr.com/v2/blog/" + mPost.getTumblrname() + ".tumblr.com/posts/photo?api_key=" + APIKey;
final StringRequest stringRequest = new StringRequest(Request.Method.GET, jsonStr, new Response.Listener<String>() {
#SuppressLint("CheckResult")
#Override
public void onResponse(String s) {
try {
String cover = null;
JSONObject obj = new JSONObject(s);
JSONObject response = obj.getJSONObject("response");
JSONArray posts = response.getJSONArray("posts");
for (int i = 0; i < posts.length(); ++i) {
JSONObject post = posts.getJSONObject(i);
JSONArray trail = post.getJSONArray("trail");
for (int j = 0; j < trail.length(); ++j) {
JSONObject trailobj = trail.getJSONObject(j);
JSONObject blog = trailobj.getJSONObject("blog");
JSONObject theme = blog.getJSONObject("theme");
cover = theme.getString("header_image");
Glide.with(context).asBitmap()
.load(cover)
.apply(options)
.listener(new RequestListener<Bitmap>() {
#Override
public boolean onLoadFailed(#Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {
return false;
}
#Override
public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
progressBarCover.setVisibility(View.GONE);
return false;
}
}).into(imgcover);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
}
);
requestQueue.add(stringRequest);
}
private void getJsonDataText(PostTumblr mPost, final TextView nametv) {
String jsonStr = "https://api.tumblr.com/v2/blog/" + mPost.getTumblrname() + ".tumblr.com/posts/photo?api_key=" + APIKey;
final StringRequest stringRequest = new StringRequest(Request.Method.GET, jsonStr, new Response.Listener<String>() {
#SuppressLint("CheckResult")
#Override
public void onResponse(String s) {
try {
JSONObject obj = new JSONObject(s);
JSONObject response = obj.getJSONObject("response");
JSONArray posts = response.getJSONArray("posts");
String name = null;
for (int i = 0; i < posts.length(); ++i) {
JSONObject post = posts.getJSONObject(i);
name = post.getString("blog_name");
}
nametv.setText(name);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
}
);
requestQueue.add(stringRequest);
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
PostTumblr mPost;
ImageView image, imgcover;
ProgressBar progressBarProfile, progressBarCover;
TextView facebooktv;
TextView instagramtv;
TextView siettv;
ViewHolder(View v) {
super(v);
image = itemView.findViewById(R.id.profilepic);
imgcover = itemView.findViewById(R.id.coverpic);
progressBarProfile = itemView.findViewById(R.id.progressprofile);
progressBarCover = itemView.findViewById(R.id.progresscover);
requestQueue = Volley.newRequestQueue(context);
name = itemView.findViewById(R.id.name);
facebooktv = itemView.findViewById(R.id.fb);
instagramtv = itemView.findViewById(R.id.instagram);
siettv = itemView.findViewById(R.id.tumblr);
progressBarProfile.getIndeterminateDrawable().setColorFilter(Color.BLACK, PorterDuff.Mode.MULTIPLY);
progressBarCover.getIndeterminateDrawable().setColorFilter(Color.BLACK, PorterDuff.Mode.MULTIPLY);
v.setOnClickListener(this);
}
#SuppressLint("CheckResult")
void bindModel(final PostTumblr postTumblr) {
/*profil*/
this.mPost = postTumblr;
final RequestOptions options = new RequestOptions();
options.diskCacheStrategy(DiskCacheStrategy.ALL)
.priority(Priority.HIGH)
.dontAnimate()
.dontTransform();
Glide.with(context).asBitmap()
.load("https://api.tumblr.com/v2/blog/" + postTumblr.getTumblrname() + ".tumblr.com/avatar/512")
.apply(options)
.listener(new RequestListener<Bitmap>() {
#Override
public boolean onLoadFailed(#Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {
return false;
}
#Override
public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
progressBarProfile.setVisibility(View.GONE);
return false;
}
}).into(image);
getJsonDataText(mPost, name);
getJsonDataCover(mPost, options, progressBarCover, imgcover);
facebooktv.setText(mPost.getTumblrname());
instagramtv.setText(mPost.getTumblrname());
siettv.setText(mPost.getTumblrname() + ".tumblr.com");
}
#Override
public void onClick(View view) {
mClickListener.onClick(view, mPost);
}
}
}
You are misplacing code implementation of data-fetching.
RecyclerView (as its name comes with it) reuse the views it creates, so when the data is being fetched asynchronously after a while the view may not belong to the data that have been fetched.
To solve the problem you should move data-fetching codes outside the myAdapterTumblr codes, and once all the data have been fetched set the data into the adapter and call notifyDataSetChanged() method of the adapter.
I have three classes, my adapter class, Voting class, and an acitivty where my JSONrequest is as shown below.The problem is as you can see that I call my JSONrequest in my activity which will get a list of views, inside that view I have a mVote textview. In my adapter you can also see I have a likeButton when someone presses that I would like mVotes to change from 0 to 1. I get all my data from a server so I am assuming I would need to make a new request, do I need to make a new adapter to? and JSON parseing method? How do I do this?!??!
public class AdapterQuestion extends RecyclerView.Adapter<AdapterQuestion.ViewQuestion>{
private LayoutInflater mLayoutInflater;
private ArrayList<QuestionData> data =new ArrayList<>();
public AdapterQuestion(Context context){
//get from context
mLayoutInflater=LayoutInflater.from(context);
}
public void setBloglist(ArrayList<QuestionData> data){
this.data =data;
notifyItemRangeChanged(0, this.data.size());
}
#Override
public ViewQuestion onCreateViewHolder(ViewGroup parent, int viewType){
ViewQuestion holder=new ViewQuestion(view);
return holder;
}
#Override
public void onBindViewHolder(ViewQuestion holder, int position) {
holder.answerText.setText(currentObj.getMtext());
holder.answerId.setText(currentObj.getId());
holder.mVotes.setText(currentObj.getVotes());
holder.mLikeButton.setTag(currentObj);
}
#Override
public int getItemCount() {
return data.size();
}
public class ViewQuestion extends RecyclerView.ViewHolder{
private TextView answerText;
private TextView answerId;
private TextView mVotes;
private LikeButton mLikeButton;
public ViewQuestion (View itemView){
super(itemView);
answerText=(TextView)itemView.findViewById(R.id.answerText);
answerId=(TextView)itemView.findViewById(R.id.answerId);
mVotes=(TextView)itemView.findViewById(R.id.VoteTextView);
mLikeButton= (LikeButton)itemView.findViewById(R.id.heart_buttons);
mLikeButton.setOnLikeListener(new OnLikeListener() {
#Override
public void liked(LikeButton likeButton) {
Voting vote = new Voting();
vote.onUpVote(answerId());
System.out.print("Adapter Position"+getAdapterPosition());
}
#Override
public void unLiked(LikeButton likeButton) {
Voting onDown=new Voting();
onDown.onDownVote(answerId());
}
});
}
public String getVoteView(){
String voteView=mVotes.getText().toString();
return voteView;
}
public String answerId(){
String converted=answerId.getText().toString();
return converted;
}
public int convertToInt(){
String converted=answerId.getText().toString();
int ConvertedInt=Integer.parseInt(converted);
return ConvertedInt;
}
}
}
Voting
public class Voting {
private VolleySingleton mVolleySingleton;
private RequestQueue mRequestQueue;
private AdapterVoteUpdate mAdapterVotes;
private ArrayList<QuestionData> updateVotes = new ArrayList<>();
public void onUpVote(final String answerId) {
final RequestQueue mrequestQueue = VolleySingleton.getInstance().getRequestQueue();
final String PUT_VOTE_UP = "url" + answerId + "url\n";
StringRequest PostVoteUp = new StringRequest(Request.Method.PUT, PUT_VOTE_UP, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
mrequestQueue.add(PostVoteUp);
}
public void onDownVote(final String answerId) {
System.out.println("Voted Down");
final RequestQueue mrequestQueue = VolleySingleton.getInstance().getRequestQueue();
final String PUT_VOTE_DOWN = "url" + answerId + "urul";
StringRequest PostVoteUp = new StringRequest(Request.Method.PUT, PUT_VOTE_DOWN, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
System.out.println("************Answer" + error + "error");
}
});
mrequestQueue.add(PostVoteUp);
}
JSON RequestClass
public void JsonRequestMethod(String Id) {
mVolleySingleton = VolleySingleton.getInstance();
mRequestQueue = mVolleySingleton.getRequestQueue();
final String URL_ANSWER = "url" + Id + "url";
JsonArrayRequest request = new JsonArrayRequest(Request.Method.GET, URL_ANSWER, (String) null, new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
mListblogs.clear();
mListblogs = parseJSONResponseQuestion(response);
mAdapterQuestion.setBloglist(mListblogs);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
System.out.println(error);
}
});
mRequestQueue.add(request);
}
private ArrayList<QuestionData> parseJSONResponseQuestion(JSONArray response) {
if (!response.equals("")) {
ArrayList<QuestionData> questionDataArrayList = new ArrayList<>();
try {
StringBuilder data = new StringBuilder();
for (int i = 0; i < response.length(); i++) {
JSONObject currentQuestions = response.getJSONObject(i);
String text = currentQuestions.getString("text");
String questionId = currentQuestions.getString("questionId");
String votes = currentQuestions.getString("votes");
System.out.println(votes+" VOTES");
int voteInt=Integer.parseInt(votes);
System.out.println(voteInt);
String Answerid = currentQuestions.getString("id");
String selectedId = currentQuestions.getString("selected");
System.out.println(response.length() + "length");
data.append(text + Answerid + "\n");
System.out.println(data);
QuestionData questionData = new QuestionData();
questionData.setMtext(text);
questionData.setVotes(votes);
questionData.setId(Answerid);
questionData.setSelected(selectedId);
mListblogs.add(questionData);
}
System.out.println(data.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
return mListblogs;
}
"I get all my data from a server so I am assuming I would need to make a new request."
-> Actually you don't need to re-request the data from your server again. You just need to update
private ArrayList<QuestionData> data =new ArrayList<>();
on your AdapterQuestion then call AdapterQuestion.notifyDataSetChanged(); after you get a success response from onUpVote or onDownVote
"Do I need to make a new adapter to? and JSON parseing method? How do I do this?!??!"
-> No need
To change a particular ItemView in your recyclerview use this below code.
notifyItemChanged(index);
If you want to change the particular Textview in onItemclick you have to get the item position.
#Override
public void onBindViewHolder(ViewQuestion holder, int position) {
holder.answerText.setText(currentObj.getMtext());
holder.answerId.setText(currentObj.getId());
holder.mVotes.setText(currentObj.getVotes());
holder.mVotes.setTag(currentObj);
holder.mLikeButton.setTag(holder);
}
In your like button click event change the code like the below
mLikeButton.setOnLikeListener(new OnLikeListener() {
#Override
public void liked(LikeButton likeButton) {
Voting vote = new Voting();
vote.onUpVote(answerId());
final ViewQuestion holder=(ViewQuestionholder)likeButton.getTag();
currentObj co=(currentObj)holder.mVotes.getTag();
holder.mVotes.setText("16");
System.out.print("Adapter Position"+getAdapterPosition());
}
#Override
public void unLiked(LikeButton likeButton) {
Voting onDown=new Voting();
onDown.onDownVote(answerId());
final ViewQuestion holder=(ViewQuestionholder)likeButton.getTag();
currentObj co=(currentObj)holder.mVotes.getTag();
holder.mVotes.setText("14");
}
});