Use fetched data from Json for other Json parse - android

https://launchlibrary.net/1.3/launch shows json data. If you paste the id after this url, there is more in depth information about the launch, for example https://launchlibrary.net/1.3/launch/2053.
I want to get the image url of the corresponding launch but it doesn't work how I'm doing it. This is the code from where I parse the JSON data.
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
v = inflater.inflate(R.layout.activity_launches, container, false);
myRecyclerView = (RecyclerView) v.findViewById(R.id.recycler_view);
myRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
launchesList = new ArrayList<>();
temporaryList = new ArrayList<>();
myRequestQueue = Volley.newRequestQueue(getContext());
parseJSON();
return v;
}
private void parseJSON(){
String url = "https://launchlibrary.net/1.3/launch";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("launches");
for(int i = 0; i < jsonArray.length(); i++){
JSONObject launch = jsonArray.getJSONObject(i);
String launchName = launch.getString("name");
String launchDate = launch.getString("net");
launchId = launch.getString("id");
String imageUrl = getImageUrl();
Launch newLaunch = new Launch(launchName, launchDate, imageUrl);
launchesList.add(newLaunch);
}
myRecyclerViewAdapter = new RecyclerViewAdapter(getContext(), launchesList);
myRecyclerView.setAdapter(myRecyclerViewAdapter);
myRecyclerViewAdapter.setOnItemClickListener(LaunchesActivity.this);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
myRequestQueue.add(request);
}
public String getImageUrl(){
String url = "https://launchlibrary.net/1.3/launch/" + launchId;
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("launches");
JSONObject launch = jsonArray.getJSONObject(0);
JSONObject rocket = launch.getJSONObject("rocket");
String url = rocket.getString("imageURL");
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
myRequestQueue.add(request);
return url;
}
This is the adapter I created to change the text in the TextViews and ImageView.
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.RecyclerViewHolder>{
private Context myContext;
private ArrayList<Launch> launchList;
private OnItemClickListener myListener;
public interface OnItemClickListener{
void onItemClick(int position);
}
public void setOnItemClickListener(OnItemClickListener listener){
myListener = listener;
}
public RecyclerViewAdapter(Context context, ArrayList<Launch> launchList){
myContext = context;
this.launchList = launchList;
}
#NonNull
#Override
public RecyclerViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(myContext).inflate(R.layout.launch_item, parent, false);
return new RecyclerViewHolder(v);
}
#Override
public void onBindViewHolder(#NonNull RecyclerViewHolder holder, int position) {
Launch currentItem = launchList.get(position);
String name = currentItem.getName();
String date = currentItem.getDate();
String imageUrl = currentItem.getImageUrl();
holder.myTextViewName.setText(name);
holder.myTextViewDate.setText(date);
Picasso.get().load(imageUrl).fit().centerInside().into(holder.myImageViewImage);
}
#Override
public int getItemCount() {
return launchList.size();
}
public class RecyclerViewHolder extends RecyclerView.ViewHolder{
public TextView myTextViewName;
public TextView myTextViewDate;
public ImageView myImageViewImage;
public RecyclerViewHolder(#NonNull View itemView) {
super(itemView);
myTextViewName = itemView.findViewById(R.id.launch_name);
myTextViewDate = itemView.findViewById(R.id.date);
myImageViewImage = itemView.findViewById(R.id.thumbnail);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(myListener != null){
int position = getAdapterPosition();
if(position != RecyclerView.NO_POSITION){
myListener.onItemClick(position);
}
}
}
});
}
}
A launch object is just this:
public Launch(String name, String date, String imageUrl) {
this.name = name;
this.date = date;
this.imageUrl = imageUrl;
}
What am I doing wrong?
Recyclerview item layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_marginTop="5dp"
android:padding="8dp"
android:background="#323131">
<ImageView
android:id="#+id/thumbnail"
android:layout_width="100dp"
android:layout_height="match_parent"
android:scaleType="centerCrop"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="130dp"
android:layout_margin="8dp"
android:orientation="vertical">
<TextView
android:id="#+id/launch_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Launch name"
android:textSize="18sp"
android:textStyle="bold"
android:textColor="#android:color/white"/>
<TextView
android:id="#+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Date"
android:textColor="#android:color/white"/>
</LinearLayout>
Recyclerview layout
Recyclerview layout:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/recycler_view"
android:background="#323131"/>

make getImage function get the index of array object and i will tell you why
String imageUrl = getImageUrl(i);
public String getImageUrl(final int index)
after getting url adding that
String url = rocket.getString("imageURL");
launchesList.get(index).imageUrl = url;
myRecyclerViewAdapter.notifyItemChanged(index);
now i tell the thread of get image get image and when finish notify adapter
it work for me

In your getImageUrl() you are returning the wrong url which is this:
String url = "https://launchlibrary.net/1.3/launch/" + launchId;
instead do this:
public String getImageUrl(){
String url = "https://launchlibrary.net/1.3/launch/" + launchId;
//add this
String imageURL = null;
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("launches");
JSONObject launch = jsonArray.getJSONObject(0);
JSONObject rocket = launch.getJSONObject("rocket");
imageURL = rocket.getString("imageURL");
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
myRequestQueue.add(request);
//return it here
return imageURL;
}

my friend above help u in parsing but let me know why you use two request one for parsing object and second one for get image but u should know that here you will use two thread so we will work on make them in one thread
and check if catch print any log
please send object in array to getImage function and continue to parse
String imageUrl = getImageUrl(launch );
and fix getImageUrl function
public String getImageUrl(JSONObject launch ){
JSONObject rocket = launch.getJSONObject("rocket");
String url = rocket.getString("imageURL");
return url;
}
try this if error still exist please inform me and i will try it on my code

Related

swipe to refresh recyclerview duplicate item json

I have a problem, when i swipe to refresh the data, the first swipe is ok but after that every swipe reload and add the same data over and over again, by the end i have a list with same items over and over... I'm using a loader. I tried to clear before but i don't understand what's wrong with my code, if someone could explain it to me. Thank You.
Here my code :
public static final String EXTRA_URL = "imageUrl";
public static final String EXTRA_APKNAME = "apkname";
public static final String EXTRA_APKSIZE = "apksize";
public static final String EXTRA_APKFILE = "apkfile";
private RecyclerView mRecyclerView;
private MyAdapter mExampleAdapter;
private ArrayList<one_item> mExampleList;
private RequestQueue mRequestQueue;
private SwipeRefreshLayout swipeRefreshLayout;
#SuppressLint("RestrictedApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setTitle("APK VIP CHINA");
getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
swipeRefreshLayout = findViewById(R.id.refreshlayout);
mRecyclerView = findViewById(R.id.recycler_view);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mExampleList = new ArrayList<>();
mRequestQueue = Volley.newRequestQueue(this);
parseJSON();
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
parseJSON();
swipeRefreshLayout.setRefreshing(false);
}
});
}
private void parseJSON() {
String url = "http://apkvip.net/json2.php";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("data");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject hit = jsonArray.getJSONObject(i);
String creatorName = hit.getString("ten");
String creatorsize = hit.getString("apk_size");
String creatorfile = hit.getString("link_apk");
String imageUrl = hit.getString("link_img");
mExampleList.add(new one_item(imageUrl, creatorName, creatorsize, creatorfile));
}
mExampleAdapter = new MyAdapter(MainActivity.this, mExampleList);
mRecyclerView.setAdapter(mExampleAdapter);
mExampleAdapter.setOnItemClickListener(MainActivity.this);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
mRequestQueue.add(request);
}
public void onItemClick(int position) {
Intent detailIntent = new Intent(this, activity_detail.class);
one_item clickedItem = mExampleList.get(position);
detailIntent.putExtra(EXTRA_URL, clickedItem.getmImageUrl());
detailIntent.putExtra(EXTRA_APKNAME, clickedItem.getMapkname());
detailIntent.putExtra(EXTRA_APKSIZE, clickedItem.getMapksize());
detailIntent.putExtra(EXTRA_APKFILE, clickedItem.getMapkfile());
startActivity(detailIntent);
}}
My adapter:
private ArrayList<one_item> mExampleList;
private OnItemClickListener mListener;
public interface OnItemClickListener {
void onItemClick(int position);
}
public void setOnItemClickListener(OnItemClickListener listener) {
mListener = listener;
}
public MyAdapter(Context context, ArrayList<one_item> exampleList) {
mContext = context;
mExampleList = exampleList;
}
#Override
public ExampleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(mContext).inflate(R.layout.one_item, parent, false);
return new ExampleViewHolder(v);
}
#Override
public void onBindViewHolder(ExampleViewHolder holder, int position) {
one_item currentItem = mExampleList.get(position);
String imageUrl = currentItem.getmImageUrl();
String apkname = currentItem.getMapkname();
String apksize = currentItem.getMapksize();
String apkfile = currentItem.getMapkfile();
holder.mTextViewname.setText(apkname);
holder.mTextViewsize.setText(apksize);
holder.mTextViewfile.setText(apkfile);
Picasso.get().load(imageUrl).fit().centerInside().into(holder.mImageView);
}
#Override
public int getItemCount() {
return mExampleList.size();
}
#Override
public long getItemId(int position) {
return position;
}
public class ExampleViewHolder extends RecyclerView.ViewHolder {
public ImageView mImageView;
public TextView mTextViewname, mTextViewsize, mTextViewfile;
public ExampleViewHolder(View itemView) {
super(itemView);
mImageView = itemView.findViewById(R.id.image);
mTextViewname = itemView.findViewById(R.id.name);
mTextViewsize = itemView.findViewById(R.id.size);
mTextViewfile = itemView.findViewById(R.id.file);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mListener != null) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
mListener.onItemClick(position);
}
}
}
});}}}
my activitymain.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="#FFFFFF"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Recommended Apps"
android:textColor="#000"
android:textStyle="bold"
android:textSize="20sp"
android:layout_marginBottom="10dp"
/>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="#+id/refreshlayout"
android:layout_width="wrap_content"
android:layout_height="match_parent"
>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
android:padding="5dp" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
Just use mExampleList.clear() at the first line of your parseJSON() function
and then you'll be able to add the "updated" items.
In addition I'd suggest that you move swipeRefreshLayout.setRefreshing(false) to the end of Volley onResponse and onError() implementation -
Right now you're telling the swipeRefreshLayout to stop refreshing before the request(that happens asynchronously) has finished
You are initializing your adapter every time you do a swipe.
Change this constructor of MyAdapter code.
public MyAdapter(Context context, ArrayList<one_item> exampleList) {
mContext = context;
mExampleList = exampleList;
}
to,
public MyAdapter(Context context) {
mContext = context;
}
public setExampleList(ArrayList<one_item> exampleList){
mExampleList = exampleList;
}
Now change your onCreate function to.
#SuppressLint("RestrictedApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setTitle("APK VIP CHINA");
getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
swipeRefreshLayout = findViewById(R.id.refreshlayout);
mRecyclerView = findViewById(R.id.recycler_view);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mExampleList = new ArrayList<>();
mRequestQueue = Volley.newRequestQueue(this);
//Changed Code
mExampleAdapter = new MyAdapter(MainActivity.this);
mRecyclerView.setAdapter(mExampleAdapter);
mExampleAdapter.setOnItemClickListener(MainActivity.this);
parseJSON();
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
parseJSON();
swipeRefreshLayout.setRefreshing(false);
}
});
}
private void parseJSON() {
String url = "http://apkvip.net/json2.php";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("data");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject hit = jsonArray.getJSONObject(i);
String creatorName = hit.getString("ten");
String creatorsize = hit.getString("apk_size");
String creatorfile = hit.getString("link_apk");
String imageUrl = hit.getString("link_img");
mExampleList.add(new one_item(imageUrl, creatorName, creatorsize, creatorfile));
}
//Changed Code
mExampleAdapter.setExampleList(mExampleList);
mExampleAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
mRequestQueue.add(request);
}
I have added a comment of the code block where I have made the changes.
Feel free to comment in case you have any queries.

why my data is not showing in recycler view in android

I am making an app using youtube data api and I got stuck at a problem and not able to find a solution, so any help will be appreciated
below is my code i have tried a lot but did not find any solution and I am beginner to this so kindly help me on this ,
in onResponse method the toast messege is working that means the code is getting response but those data are not showing in recycler view
public class MainActivity extends AppCompatActivity {
RecyclerView list;
LinearLayoutManager linearLayoutManager;
ArrayList<VideosList> videosLists;
CustomRecyclerAdapter listViewAdapter;
String url = "https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=myId&maxResults=3&key=myApiKey";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = findViewById(R.id.rv);
videosLists = new ArrayList<>();
linearLayoutManager = new LinearLayoutManager(this);
RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Toast.makeText(MainActivity.this,"Success",Toast.LENGTH_LONG).show();
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray = jsonObject.getJSONArray("items");
for (int i = 0; i < jsonArray.length(); i++ ) {
JSONObject jsonObject1 = jsonArray.getJSONObject(i);
JSONObject jsonObjectVideoId = jsonObject1.getJSONObject("id");
JSONObject jsonObjectSnippet = jsonObject1.getJSONObject("snippet");
JSONObject jsonObjectthumb = jsonObject1.getJSONObject("thumbnails").getJSONObject("medium");
String videoId = jsonObjectVideoId.getString("videoId");
String thumb = jsonObjectthumb.getString("url");
VideosList videosList = new VideosList();
videosList.setVideoId(videoId);
videosList.setTitle(jsonObjectSnippet.getString("title"));
videosList.setThumbnail(thumb);
videosLists.add(videosList);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this,"failed",Toast.LENGTH_LONG).show();
}
});
requestQueue.add(stringRequest);
list.setHasFixedSize(true);
list.setLayoutManager(linearLayoutManager);
listViewAdapter = new CustomRecyclerAdapter(this, videosLists);
list.setAdapter(listViewAdapter);
}
}
Below is layout for above code
<?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="wrap_content"
tools:context="com.creativeapps.entertainmentkidsyouth.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="#+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
Below is the code for adapter
public class CustomRecyclerAdapter extends RecyclerView.Adapter<CustomRecyclerAdapter.ViewHolder> {
private ArrayList<VideosList> videosList;
private Context context;
public CustomRecyclerAdapter(Context context,ArrayList<VideosList> videoList) {
this.videosList = videoList;
this.context = context;
}
#Override
public CustomRecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview, parent, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
#Override
public void onBindViewHolder(final CustomRecyclerAdapter.ViewHolder holder, int position) {
holder.name.setText(videosList.get(position).getTitle());
Picasso.get().load(videosList.get(position).getThumbnail()).into(holder.imageView);
}
#Override
public int getItemCount() {
return videosList != null ?videosList.size():0;
}
public class ViewHolder extends RecyclerView.ViewHolder{
public TextView name;
ImageView imageView;
private ViewHolder(View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.thumbnail);
name = itemView.findViewById(R.id.title);
}
}
}
Below is the class for videolist
public class VideosList {
public String thumbnail, videoId, title;
public VideosList(String thumbnail, String videoId, String title) {
this.thumbnail = thumbnail;
this.videoId = videoId;
this.title = title;
}
public VideosList() {
}
public String getThumbnail() {
return thumbnail;
}
public void setThumbnail(String thumbnail) {
this.thumbnail = thumbnail;
}
public String getVideoId() {
return videoId;
}
public void setVideoId(String videoId) {
this.videoId = videoId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
layout for custom recycler view
<?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"
android:background="#fff"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/root"
android:orientation="horizontal"
android:gravity="center"
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/thumbnail"
android:src="#mipmap/ic_launcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:textSize="16sp"
android:layout_marginStart="10dp"
android:padding="10dp"
android:textColor="#111"
android:id="#+id/title"
android:text="Video"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
you are incorrectly parsing JSON from API
Replace
JSONObject jsonObjectthumb = jsonObject1.getJSONObject("thumbnails").getJSONObject("medium");
with
JSONObject jsonObjectthumb = jsonObject1.getJSONObject("snippet").getJSONObject("thumbnails").getJSONObject("medium");
"thumbnails" is inside "snippet" JSONObject
Replace this line
JSONObject jsonObjectthumb = jsonObject1.getJSONObject("thumbnails").getJSONObject("medium");
With
JSONObject jsonObjectthumb = jsonObjectSnippet.getJSONObject("thumbnails").getJSONObject("medium");
The 'thumbnails' object is inside the 'snippet' object.

Two recyclerview is not showing in one layout

i am trying to implement two recyclerview in one layout with same adapter. but only one recyclerview is showing.
MainActivity.java
public class MainActivity extends AppCompatActivity {
List<DataAdapter> ListOfdataAdapter;
String HTTP_JSON_URL = "http://example.com";
String Image_Name_JSON = "Menu_name";
String Image_URL_JSON = "Menu_image";
JsonArrayRequest RequestOfJSonArray ;
RequestQueue requestQueue ;
View view ;
int RecyclerViewItemPosition ;
RecyclerView.LayoutManager layoutManagerOfrecyclerView;
RecyclerView.Adapter recyclerViewadapter;
ArrayList<String> ImageTitleNameArrayListForClick;
long Category_ID;
String MenuAPI;
private RecyclerView secondRecyclerView;
private RecyclerView.LayoutManager secondLayoutManager;
private RecyclerView firstrecyclerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageTitleNameArrayListForClick = new ArrayList<>();
// menu API url
Intent iGet = getIntent();
Category_ID = iGet.getLongExtra("category_id",0);
MenuAPI += Category_ID;
ListOfdataAdapter = new ArrayList<>();
firstrecyclerView = (RecyclerView) findViewById(R.id.recyclerview1);
firstrecyclerView.setHasFixedSize(true);
layoutManagerOfrecyclerView = new LinearLayoutManager(MainActivity.this, LinearLayoutManager.HORIZONTAL, true);
firstrecyclerView.setLayoutManager(layoutManagerOfrecyclerView);
firstrecyclerView.post(new Runnable() {
#Override
public void run() {
JSON_HTTP_CALL();// a method which requests remote data
}
});
secondRecyclerView = (RecyclerView) findViewById(R.id.recyclerview2);
secondRecyclerView.setHasFixedSize(true);
secondLayoutManager = new LinearLayoutManager(MainActivity.this, LinearLayoutManager.VERTICAL, true);
secondRecyclerView.setLayoutManager(secondLayoutManager);
secondRecyclerView.post(new Runnable() {
#Override
public void run() {
JSON_HTTP_CALL2();// a method which requests remote data
}
});
// Implementing Click Listener on RecyclerView.
}
public void JSON_HTTP_CALL(){
RequestOfJSonArray = new JsonArrayRequest(HTTP_JSON_URL + "/api/example.php" +"&category_id=2",
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
ParseJSonResponse(response);
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue = Volley.newRequestQueue(MainActivity.this);
requestQueue.add(RequestOfJSonArray);
}
public void ParseJSonResponse(JSONArray array){
for(int i = 0; i<array.length(); i++) {
DataAdapter GetDataAdapter2 = new DataAdapter();
JSONObject json = null;
try {
json = array.getJSONObject(i);
GetDataAdapter2.setImageTitle2(json.getString(Image_Name_JSON));
// Adding image title name in array to display on RecyclerView click event.
ImageTitleNameArrayListForClick.add(json.getString(Image_Name_JSON));
GetDataAdapter2.setImageUrl2(HTTP_JSON_URL + "/" + json.getString(Image_URL_JSON));
} catch (JSONException e) {
e.printStackTrace();
}
ListOfdataAdapter.add(GetDataAdapter2);
}
recyclerViewadapter = new RecyclerViewAdapter(ListOfdataAdapter, getApplicationContext());
firstrecyclerView.setAdapter(recyclerViewadapter);
}
public void JSON_HTTP_CALL2(){
RequestOfJSonArray = new JsonArrayRequest(HTTP_JSON_URL + "/api/example.php" +"&category_id=2",
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
ParseJSonResponse2(response);
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue = Volley.newRequestQueue(MainActivity.this);
requestQueue.add(RequestOfJSonArray);
}
public void ParseJSonResponse2(JSONArray array){
for(int i = 0; i<array.length(); i++) {
DataAdapter GetDataAdapter2 = new DataAdapter();
JSONObject json = null;
try {
json = array.getJSONObject(i);
GetDataAdapter2.setImageTitle2(json.getString(Image_Name_JSON));
// Adding image title name in array to display on RecyclerView click event.
ImageTitleNameArrayListForClick.add(json.getString(Image_Name_JSON));
GetDataAdapter2.setImageUrl2(HTTP_JSON_URL + "/" + json.getString(Image_URL_JSON));
} catch (JSONException e) {
e.printStackTrace();
}
ListOfdataAdapter.add(GetDataAdapter2);
}
recyclerViewadapter = new RecyclerViewAdapter(ListOfdataAdapter, getApplicationContext());
secondRecyclerView.setAdapter(recyclerViewadapter);
}
}
Adapter class
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
Context context;
List<DataAdapter> dataAdapters;
ImageLoader imageLoader;
private Activity activity;
private static final int CITY_TYPE = 0;
JsonArrayRequest RequestOfJSonArray;
private static final int EVENT_TYPE = 1;
public RecyclerViewAdapter(Activity act) {
this.activity = act;
}
public RecyclerViewAdapter(List<DataAdapter> getDataAdapter, Context context){
super();
this.dataAdapters = getDataAdapter;
this.context = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
switch (viewType) {
case CITY_TYPE:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview, parent, false);
return new ViewHolder(view);
case EVENT_TYPE:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card2, parent, false);
return new ViewHolder(view);
}
return null;
}
#Override
public void onBindViewHolder(ViewHolder Viewholder, int position) {
DataAdapter dataAdapterOBJ = dataAdapters.get(position);
imageLoader = ImageAdapter.getInstance(context).getImageLoader();
if (dataAdapterOBJ != null) {
switch (dataAdapterOBJ.getType()) {
case CITY_TYPE:
imageLoader.get(dataAdapterOBJ.getImageUrl2(),
ImageLoader.getImageListener(
Viewholder.VollyImageView,//Server Image
R.mipmap.ic_launcher,//Before loading server image the default showing image.
android.R.drawable.ic_dialog_alert //Error image if requested image dose not found on server.
)
);
Viewholder.VollyImageView.setImageUrl(dataAdapterOBJ.getImageUrl2(), imageLoader);
Viewholder.ImageTitleTextView.setText(dataAdapterOBJ.getImageTitle2());
break;
case EVENT_TYPE:
imageLoader.get(dataAdapterOBJ.getImageUrl2(),
ImageLoader.getImageListener(
Viewholder.VollyImageView,//Server Image
R.mipmap.ic_launcher,//Before loading server image the default showing image.
android.R.drawable.ic_dialog_alert //Error image if requested image dose not found on server.
)
);
Viewholder.VollyImageView.setImageUrl(dataAdapterOBJ.getImageUrl2(), imageLoader);
Viewholder.ImageTitleTextView.setText(dataAdapterOBJ.getImageTitle2());
break;
}
}
}
#Override
public int getItemCount() {
return dataAdapters.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
public TextView ImageTitleTextView;
public NetworkImageView VollyImageView ;
public ViewHolder(View itemView) {
super(itemView);
ImageTitleTextView = (TextView) itemView.findViewById(R.id.MenuNameTV) ;
VollyImageView = (NetworkImageView) itemView.findViewById(R.id.VolleyImageView) ;
}
}
}
main activity xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Most Watched"/>
<!-- A RecyclerView to display horizontal list -->
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerview1"
android:scrollbars="none"
android:layout_width="fill_parent"
android:layout_height="240dp"
android:paddingLeft="0dp"
android:paddingRight="15dp"
android:paddingTop="15dp"
android:paddingBottom="25dp"
android:background="#ffc000"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="All Dramas"/>
<!-- A RecyclerView to display vertical list -->
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerview2"
android:layout_width="fill_parent"
android:layout_height="240dp"
android:paddingLeft="0dp"
android:paddingRight="15dp"
android:paddingTop="15dp"
android:paddingBottom="25dp"/>
i try every possible way available on internet but dont get solution.
Anything wrong i am doing?
any idea how to run two recyclerview with above code.
thankx
You can do this-
<android.support.v4.widget.NestedScrollView>
<LinearLayout>
.
.
.
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
This is used to attach 2 or more scrolling views together and one more thing you can do is to make the height of recycler view as "wrap_content".
Hope this helps.

Issue in loading new Images in gridview

I'm making an app that loads the images from server in gridview. I'm using Volley for Http request, glide to download image. I can load the image when app is install and open first time, but when new images are added, it cannot load new images.
URLS
there was 7 urls when app is installed, so it loaded 7 images. but after refresh or reopen app it cannot load 8th image
{"result":[{"url":"http://smilestechno.000webhostapp.com/ImagesUpload/Desert.jpg"},{"url":"http://smilestechno.000webhostapp.com/ImagesUpload/Jellyfish.jpg"},{"url":"http://smilestechno.000webhostapp.com/ImagesUpload/child.png"},{"url":"http://smilestechno.000webhostapp.com/ImagesUpload/clamber-512.png"},{"url":"http://smilestechno.000webhostapp.com/ImagesUpload/image:52.jpg"},{"url":"http://smilestechno.000webhostapp.com/ImagesUpload/image:52.jpg"},{"url":"http://smilestechno.000webhostapp.com/ImagesUpload/Temp20181016_051043.jpg"},{"url":"http://smilestechno.000webhostapp.com/ImagesUpload/My Children20182917_022900.jpg"},{"url":"http://smilestechno.000webhostapp.com/ImagesUpload/MyChildren1412414122.jpg"}]}
Images fregment where gridview is displayed
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
// return inflater.inflate(R.layout.fragment_images, container, false);
View v = inflater.inflate(R.layout.fragment_images, container, false);
FloatingActionButton btnCamera = (FloatingActionButton) v.findViewById(R.id.btnCamera);
FloatingActionButton btnFolder = (FloatingActionButton) v.findViewById(R.id.btnFolder);
refreshMainGrid = (SwipeRefreshLayout) v.findViewById(R.id.refreshManGrid);
imgGrid = (GridView) v.findViewById(R.id.imgGrid);
getData();
imgGrid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Intent intent = new Intent(getContext(), SingleImage.class);
intent = intent.putExtra("id", (ImageObject) myGridAdapter.getItem(position));
startActivity(intent);
}
});
refreshMainGrid.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
refreshData();
}
});
return v;
}
private void getData(){
RequestQueue queue = Volley.newRequestQueue(getContext());
StringRequest stringRequest = new StringRequest(Request.Method.GET, IMAGE_URL, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray = jsonObject.optJSONArray("result");
if (jsonArray != null && jsonArray.length() > 0) {
ArrayList<ImageObject> imageObjects = new ArrayList<>();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonitem = jsonArray.optJSONObject(i);
imageObjects.add(new ImageObject(jsonitem));
}
myGridAdapter = new MyGridAdapter(getContext(), imageObjects);
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
imgGrid.setAdapter(myGridAdapter);
}
});
}
} catch (Exception e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
stringRequest.setRetryPolicy(new DefaultRetryPolicy(6000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
queue.add(stringRequest);
}
private void refreshData(){
RequestQueue queue = Volley.newRequestQueue(getContext());
StringRequest stringRequest = new StringRequest(Request.Method.GET, IMAGE_URL, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray = jsonObject.optJSONArray("result");
if (jsonArray != null && jsonArray.length() > 0) {
ArrayList<ImageObject> imageObjects = new ArrayList<>();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonitem = jsonArray.optJSONObject(i);
imageObjects.add(new ImageObject(jsonitem));
}
myGridAdapter = new MyGridAdapter(getContext(), imageObjects);
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
imgGrid.setAdapter(myGridAdapter);
}
});
}
} catch (Exception e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
stringRequest.setRetryPolicy(new DefaultRetryPolicy(6000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
queue.add(stringRequest);
refreshMainGrid.setRefreshing(false);
}
MyGridAdapter
public class MyGridAdapter extends BaseAdapter {
private Context context;
private ArrayList<ImageObject> imageObjects;
private LayoutInflater mLayoutInflate;
public MyGridAdapter(Context context, ArrayList<ImageObject> imageObjects) {
this.context = context;
this.imageObjects = imageObjects;
this.mLayoutInflate = LayoutInflater.from(context);
}
public int getCount() {
if (imageObjects != null) return imageObjects.size();
return 0;
}
#Override
public Object getItem(int position) {
if (imageObjects != null && imageObjects.size() > position)
return imageObjects.get(position);
return null;
}
#Override
public long getItemId(int position) {
if (imageObjects != null && imageObjects.size() > position)
return imageObjects.get(position).getId();
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = mLayoutInflate.inflate(R.layout.imageitem, parent,
false);
viewHolder.imageView = (ImageView) convertView.findViewById(R.id.image);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
ImageObject imageObject = (ImageObject) getItem(position);
if (imageObject != null) {
Glide
.with(context)
.load(imageObject.getImageUrl())
.centerCrop()
.crossFade()
.into(viewHolder.imageView);
}
return convertView;
}
}
ImageObject
public class ImageObject implements Parcelable{
private int id;
private String imageUrl;
public ImageObject(Parcel in){
id = in.readInt();
imageUrl = in.readString();
}
public ImageObject(JSONObject jsonObject){
if(jsonObject == null) return;
this.id = jsonObject.optInt("id");
this.imageUrl = jsonObject.optString("url");
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(id);
dest.writeString(imageUrl);
}
public static final ImageObject.Creator<ImageObject> CREATOR = new ImageObject.Creator<ImageObject>(){
#Override
public ImageObject createFromParcel(Parcel in) {
return new ImageObject(in);
}
#Override
public ImageObject[] newArray(int size) {
return new ImageObject[size];
}
};
}
Even after refresh or reopen app it does not load 8th image.
one more isuue is that it loading image from bottom not from top item.
Images xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.smiles.mychildren.Images"
android:orientation="vertical">
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/refreshManGrid"
android:layout_marginTop="110dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<GridView
android:id="#+id/imgGrid"
android:numColumns="auto_fit"
android:columnWidth="135dp"
android:layout_marginTop="110dp"
android:clipToPadding="false"
android:padding="0dp"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:verticalSpacing="0dp"
android:horizontalSpacing="0dp"
android:stretchMode="none"
android:stackFromBottom="false">
</GridView>
</android.support.v4.widget.SwipeRefreshLayout>
I found the solution.
The problem was with Volley cache.
stringRequest.setShouldCache(false);
By this we can stop saving cache.

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

Categories

Resources