Recyclerview not display vediothumbnail - android

i am working and i want to display a video in recyclerview from server its geting data not displaying video thumbnail its showing only text and displaing error thumbnail i dont know where i am wrong please review my code and tell me where i am wrong ,
myActivity
public class TestingActivity extends AppCompatActivity {
private List<TestingModel> listiIems;
//Creating Views
private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
private RecyclerView.Adapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.testingactivity);
//Initializing Views
recyclerView = (RecyclerView) findViewById(R.id.recyclerView_test);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
listiIems = new ArrayList<>();
//Calling method to get data
getData();
}
//This method will get data from the web api
private void getData() {
String Data_Url="http://www.example.com/vedio.json";
//Showing a progress dialog
final ProgressDialog loading = ProgressDialog.show(this, "Loading Data", "Please wait...", false, false);
//Creating a json array request
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Data_Url,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
//Dismissing progress dialog
loading.dismiss();
//calling method to parse json array
parseData(response);
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
//Creating request queue
RequestQueue requestQueue = Volley.newRequestQueue(this);
//Adding request to the queue
requestQueue.add(jsonArrayRequest);
}
//This method will parse json data
private void parseData(JSONArray array) {
for (int i = 0; i < array.length(); i++) {
TestingModel item = new TestingModel();
JSONObject json = null;
try {
json = array.getJSONObject(i);
item.setUrl_image(json.getString("audio"));
item.setTest_name(json.getString("txt"));
} catch (JSONException e) {
e.printStackTrace();
}
listiIems.add(item);
}
//Finally initializing our adapter
adapter = new TestingAdapter(listiIems, this);
//Adding adapter to recyclerview
recyclerView.setAdapter(adapter);
}
}
Model
public class TestingModel {
public String getUrl_image() {
return url_image;
}
public void setUrl_image(String url_image) {
this.url_image = url_image;
}
public String getTest_name() {
return test_name;
}
public void setTest_name(String test_name) {
this.test_name = test_name;
}
private String url_image;
private String test_name;
}
Adapter
public class TestingAdapter extends RecyclerView.Adapter<TestingAdapter.ViewHolder> {
private ImageLoader imageLoader;
private Context context;
String url;
//List of superHeroes
List<TestingModel> listItems;
public TestingAdapter(List<TestingModel> listItems, Context context) {
super();
//Getting all the superheroes
this.listItems = listItems;
this.context = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.testing_list, parent, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolder holder, final int position) {
TestingModel items = listItems.get(position);
imageLoader = CustomVolleyRequest1.getInstance(context).getImageLoader();
imageLoader.get(items.getUrl_image(), ImageLoader.getImageListener(holder.imageView, R.drawable.placeholder, android.R.drawable.ic_dialog_alert));
holder.imageView.setImageUrl(items.getUrl_image(), imageLoader);
holder.textViewName.setText(items.getTest_name());
holder.imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
url = listItems.get(position).getUrl_image();
Log.e("urlis","==>"+url);
}
});
}
#Override
public int getItemCount() {
return listItems.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
public NetworkImageView imageView;
public TextView textViewName;
public ViewHolder(View itemView) {
super(itemView);
imageView = (NetworkImageView) itemView.findViewById(R.id.testingimage);
textViewName = (TextView) itemView.findViewById(R.id.texting_text);
}
}
}
help me

First of all, Check in adapter. Are you getting url is image or video. If url is contain image then need to check in ImageLoader Library. you can better use with glide and picasso.
Its very easy to use picasso. You can found picasso from this link
http://square.github.io/picasso/
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
If your url contain video then you have to create thumbnail from url using Thumnail Util
Bitmap thumb = ThumbnailUtils.createVideoThumbnail(filePath,
Thumbnails.MINI_KIND); imageview.setBitmapImage(thumb);

Related

I am facing "No adapter attached; skipping layout" error

When I am writing the title of my question, then this site recommends me some answer but I can't find my answer that's why I am writing a new one. I am trying simple RecyclerView and it's run successfully but when I am fetching data from internet using volley library then I face this error
here is my code
BusinessActivity.java
public class BusinessActivity extends AppCompatActivity {
private final String URL = "https://api.myjson.com/bins/a44ec";
private JsonArrayRequest request;
private RequestQueue requestQueue;
private List<ArticleModel> articleList;
private RecyclerView recyclerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_business);
articleList = new ArrayList<>();
recyclerView = findViewById(R.id.business_recyclerView);
jsonParse();
}
private void jsonParse() {
request = new JsonArrayRequest(URL, new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
JSONObject jsonObject = null;
for (int i=0;i<response.length();i++){
try {
jsonObject = response.getJSONObject(i);
ArticleModel article = new ArticleModel();
article.setTitle(jsonObject.getString("title"));
article.setAuthor(jsonObject.getString("author"));
article.setDescription(jsonObject.getString("description"));
article.setPublishedAt(jsonObject.getString("publishedAt"));
article.setUrlToImage(jsonObject.getString("urlToImage"));
articleList.add(article);
} catch (JSONException e) {
e.printStackTrace();
}
}
setRecyclerView(articleList);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue = Volley.newRequestQueue(BusinessActivity.this);
requestQueue.add(request);
}
private void setRecyclerView(List<ArticleModel> articleList) {
BusinessAdapter adapter = new BusinessAdapter(articleList,this);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
}
}
BusinessAdapter.java
public class BusinessAdapter extends RecyclerView.Adapter<BusinessAdapter.MyViewHolder> {
private List<ArticleModel> articleModels;
private Context context;
RequestOptions options;
public BusinessAdapter(List<ArticleModel> articleModels, Context context) {
this.articleModels = articleModels;
this.context = context;
options = new RequestOptions().centerCrop().placeholder(R.drawable.ic_launcher_background).error(R.drawable.ic_launcher_background);
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.category_row,parent,false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, int position) {
ArticleModel articleModel = articleModels.get(position);
holder.title.setText(articleModel.getTitle());
holder.author.setText(articleModel.getAuthor());
holder.desc.setText(articleModel.getDescription());
holder.date.setText(articleModel.getPublishedAt());
Glide.with(context).load(articleModel.getUrlToImage()).apply(options).into(holder.image);
}
#Override
public int getItemCount() {
return articleModels.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView title,author,desc,date;
ImageView image;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
title = itemView.findViewById(R.id.title);
author = itemView.findViewById(R.id.author);
desc = itemView.findViewById(R.id.detail);
date = itemView.findViewById(R.id.date);
image = itemView.findViewById(R.id.image);
}
}
}
Error log
2020-04-01 00:15:53.117 8821-8821/com.atc.newsappproject E/RecyclerView: No adapter attached; skipping layout
2020-04-01 00:15:53.118 1752-1787/? E/storaged: getDiskStats failed with result NOT_SUPPORTED and size 0
How can I solve this?
Thank you.
This error usually happens when you don't set the RecyclerView when you create your Fragment or Activity. To fix this you need to:
Set your RecyclerView in onCreate instead of when you pick up result. You set it with an empty list.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_business);
articleList = new ArrayList<>();
recyclerView = findViewById(R.id.business_recyclerView);
setRecyclerView(articleList);
jsonParse();
}
You create a setter in your Adapter
public void setArticles(ArrayList<ArticleModel> articles){
this.articles = articles;
notifyDataSetChanged();
}
Create private BusinessAdapter adapter; below private RecyclerView recyclerView;
Finally in your onResponse you call adapter.setArticles(articlesList);
Here is a link with a lot of solutions if you still got issue: recyclerview No adapter attached; skipping layout

recyclerview doesn't show any thing in fragment

hi guys i have a card view that populate it's data from a json. I parse json with volley and it is successfull .
but in card view there is nothing to show
here is my json
{"photo":[{"id":"4","profile_id":"mahdi","caption":"hello","image_url":"https:\/\/hemispheric-suggest.000webhostapp.com\/img\/img\/birjand.jpg","profile_img_url":"https:\/\/hemispheric-suggest.000webhostapp.com\/prof\/prof\/prof_1.jpg","insert_time":"2017-07-09 11:21:44"}
and my fragment adapter :
private RecyclerView recyclerView;
private RequestQueue mRequestQueue;
ArrayList<Card> list = new ArrayList<>();
String url = "My url";
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag,container,false);
recyclerView = (RecyclerView) view.findViewById(R.id.recycler);
mRequestQueue = Volley.newRequestQueue(getActivity());
list();
LinearLayoutManager MyLayoutManager = new LinearLayoutManager(getActivity());
MyLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setAdapter(new Adapter(list));
return view;
}
public class Adapter extends RecyclerView.Adapter<Adapter.MyviewHolder>{
// private ArrayList<Card> list;
public Adapter(ArrayList<Card> data) {
list = data;
}
public class MyviewHolder extends RecyclerView.ViewHolder{
public TextView textView;
public ImageView imageView;
public MyviewHolder(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.textView);
imageView = (ImageView) itemView.findViewById(R.id.imageView);
}
}
#Override
public MyviewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_row,parent,false);
return new MyviewHolder(view);
}
#Override
public void onBindViewHolder(MyviewHolder holder, int position) {
Card card = list.get(position);
holder.textView.setText(card.getName());
Glide.with(getActivity()).load(card.getImg()).into(holder.imageView);
}
#Override
public int getItemCount() {
return list.size();
}
}
public void list() {
JsonObjectRequest object = new JsonObjectRequest(Request.Method.GET, url,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try{
JSONArray array = response.getJSONArray("photo");
for (int i = 0; i <array.length() ; i++) {
JSONObject temp = array.getJSONObject(i);
Card card = new Card();
card.setName(temp.getString("profile_id"));
card.setImg(temp.getString("image_url"));
list.add(card);
}
}catch (Exception ex){
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
}
);
mRequestQueue.add(object);
}
}
Volley is asynchronus, so when you call:
recyclerView.setAdapter(new Adapter(list));
just a few milliseconds after you enqueued your request, It's very likely that the list is still empty because Volley hasn't received any response yet. You should set your adapter once you get the response inside the onResponse callback.

how to auto update RecyclerView.Adapter in android?

This is My Code:
public class ItemsListActivity extends AppCompatActivity {
private List<AllItems> listItems;
private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
private RecyclerView.Adapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_items_list);
Button camera=(Button)findViewById(R.id.camera);
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
listItems = new ArrayList<>();
camera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(ItemsListActivity.this, Camera.class);
startActivity(i);
}
});
getData();
}
private void getData(){
final ProgressDialog loading = ProgressDialog.show(this,"Loading Data", "Please wait...",false,false);
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Config.DATA_URL,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
loading.dismiss();
parseData(response);
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(jsonArrayRequest);
}
private void parseData(JSONArray array){
for(int i = 0; i<array.length(); i++) {
AllItems items = new AllItems();
JSONObject json = null;
try {
json = array.getJSONObject(i);
items.setImageUrl(json.getString(Config.TAG_IMAGE_URL));
items.setName(json.getString(Config.TAG_NAME));
} catch (JSONException e) {
e.printStackTrace();
}
listItems.add(items);
}
adapter = new CardAdapter(listItems, this);
recyclerView.setAdapter(adapter);
adapter.notifyItemRangeChanged(0, listItems.size());
}
}
And This Is CardAdapter:
public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder> {
private ImageLoader imageLoader;
private Context context;
List<AllItems> superHeroes;
public CardAdapter(List<AllItems> superHeroes, Context context){
super();
this.superHeroes = superHeroes;
this.context = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.all_items_lists, parent, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
AllItems superHero = superHeroes.get(position);
imageLoader = CustomVolleyRequest.getInstance(context).getImageLoader();
imageLoader.get(superHero.getImageUrl(), ImageLoader.getImageListener(holder.imageView, R.mipmap.ic_launcher, android.R.drawable.ic_dialog_alert));
holder.imageView.setImageUrl(superHero.getImageUrl(), imageLoader);
holder.textViewName.setText(superHero.getName());
}
#Override
public int getItemCount() {
return superHeroes.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
public NetworkImageView imageView;
public TextView textViewName;
public ViewHolder(View itemView) {
super(itemView);
imageView = (NetworkImageView) itemView.findViewById(R.id.imageViewHero);
textViewName = (TextView) itemView.findViewById(R.id.textViewName);
}
}
}
how do I update adapter data in order to display newly received items?
You are creating instance of adapter every time you parsing the data,
just create its instance once in onCreate() method like below:
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
listItems = new ArrayList<>();
adapter = new CardAdapter(listItems, this);
recyclerView.setAdapter(adapter);
and just notify the adapter with your parse data like below:
listItems.add(items);
adapter.notifyDataSetChanged();
will work for you.
Here what is happening that evertime your are calling getData() you are initializing adapter again . Instead you should initialize your adapter in onCreate and in getData just update the list that you are passing in adapter and call adapter.notifyDataSetChanged().
private List<AllItems> listItems;
private RecyclerView recyclerView; private RecyclerView.LayoutManager layoutManager;
private RecyclerView.Adapter adapter; #Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_items_list); Button camera=(Button)findViewById(R.id.camera); recyclerView = (RecyclerView) findViewById(R.id.recyclerView); recyclerView.setHasFixedSize(true); layoutManager = new LinearLayoutManager(this); recyclerView.setLayoutManager(layoutManager); listItems = new ArrayList<>();
adapter = new CardAdapter(listItems, this);
recyclerView.setAdapter(adapter);
camera.setOnClickListener(new View.OnClickListener() { #Override public void onClick(View view) { Intent i = new Intent(ItemsListActivity.this, Camera.class); startActivity(i); } }); getData(); }
private void getData(){
final ProgressDialog loading = ProgressDialog.show(this,"Loading Data", "Please wait...",false,false);
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Config.DATA_URL, new Response.Listener<JSONArray>() { #Override public void onResponse(JSONArray response) { loading.dismiss(); parseData(response); }
}, new Response.ErrorListener() {
#Override public void onErrorResponse(VolleyError error) { } }); RequestQueue requestQueue = Volley.newRequestQueue(this); requestQueue.add(jsonArrayRequest); }
private void parseData(JSONArray array){
for(int i = 0; i<array.length(); i++) {
AllItems items = new AllItems(); JSONObject json = null;
try
{
json = array.getJSONObject(i); items.setImageUrl(json.getString(Config.TAG_IMAGE_URL)); items.setName(json.getString(Config.TAG_NAME)); } catch (JSONException e) { e.printStackTrace(); } listItems.add(items); }
}
adapter.notifyDataSetChanged().
}
And if you don't want to show previous items call lisItems.clear() before adding new items

android - recyclerview cannot click and get position

I have tried some code to can click recyclerview item and get position, but I can't click the recyclerview item.. the data I get from url. So I wan't switch activity with get the item user click..
how I can fix that?
this code for adapter
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
Context context;
private static final String TAG = RecyclerViewAdapter.class.getSimpleName();
List<GetDataAdapter> getDataAdapter;
ImageLoader imageLoader1;
public RecyclerViewAdapter(List<GetDataAdapter> getDataAdapter, Context context) {
super();
this.getDataAdapter = getDataAdapter;
this.context = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_items, parent, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolder Viewholder, int position) {
GetDataAdapter getDataAdapter1 = getDataAdapter.get(position);
imageLoader1 = ServerImageParseAdapter.getInstance(context).getImageLoader();
imageLoader1.get(getDataAdapter1.getImageServerUrl(),
ImageLoader.getImageListener(
Viewholder.networkImageView,//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.networkImageView.setImageUrl(getDataAdapter1.getImageServerUrl(), imageLoader1);
Viewholder.ImageTitleNameView.setText(getDataAdapter1.getImageTitleName());
}
#Override
public int getItemCount() {
return getDataAdapter.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
public TextView ImageTitleNameView;
public NetworkImageView networkImageView;
public ViewHolder(View itemView) {
super(itemView);
ImageTitleNameView = (TextView) itemView.findViewById(R.id.textView_item);
networkImageView = (NetworkImageView) itemView.findViewById(R.id.VollyNetworkImageView1);
}
}
}
and this code for mainactivity
public class MainActivity extends AppCompatActivity {
List<GetDataAdapter> GetDataAdapter1;
RecyclerView recyclerView;
RecyclerView.LayoutManager recyclerViewlayoutManager;
RecyclerView.Adapter recyclerViewadapter;
String GET_JSON_DATA_HTTP_URL = "http://androidblog.esy.es/ImageJsonData.php";
String JSON_IMAGE_TITLE_NAME = "image_title";
String JSON_IMAGE_URL = "image_url";
JsonArrayRequest jsonArrayRequest ;
RequestQueue requestQueue ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GetDataAdapter1 = new ArrayList<>();
recyclerView = (RecyclerView) findViewById(R.id.recyclerview1);
recyclerView.setHasFixedSize(true);
recyclerViewlayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(recyclerViewlayoutManager);
JSON_DATA_WEB_CALL();
}
public void JSON_DATA_WEB_CALL(){
jsonArrayRequest = new JsonArrayRequest(GET_JSON_DATA_HTTP_URL,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
JSON_PARSE_DATA_AFTER_WEBCALL(response);
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue = Volley.newRequestQueue(this);
requestQueue.add(jsonArrayRequest);
}
public void JSON_PARSE_DATA_AFTER_WEBCALL(JSONArray array){
for(int i = 0; i<array.length(); i++) {
GetDataAdapter GetDataAdapter2 = new GetDataAdapter();
JSONObject json = null;
try {
json = array.getJSONObject(i);
GetDataAdapter2.setImageTitleNamee(json.getString(JSON_IMAGE_TITLE_NAME));
GetDataAdapter2.setImageServerUrl(json.getString(JSON_IMAGE_URL));
} catch (JSONException e) {
e.printStackTrace();
}
GetDataAdapter1.add(GetDataAdapter2);
}
recyclerViewadapter = new RecyclerViewAdapter(GetDataAdapter1, this);
recyclerView.setAdapter(recyclerViewadapter);
}
}
getdataadapter
public class GetDataAdapter {
public String ImageServerUrl;
public String ImageTitleName;
public String getImageServerUrl() {
return ImageServerUrl;
}
public void setImageServerUrl(String imageServerUrl) {
this.ImageServerUrl = imageServerUrl;
}
public String getImageTitleName() {
return ImageTitleName;
}
public void setImageTitleNamee(String Imagetitlename) {
this.ImageTitleName = Imagetitlename;
}
}
You need to set onClickListener to your item view. You can do by following below steps:
Set onCLickListener to root view of your list item.
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_items, parent, false);
Add below code in onCreateViewHolder:
v.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Add your logic here
}
});
You can add listener while onBindViewHolder:
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Add your logic here
}
});
Thanks.
Put a listener on your recycler item view and then in onclick, start intent to open new activity. Pass the data object you want to pass with the intent.
//make sure your model class GetDataAdapter implements Parcelable
holder.itemView.setOnclickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(this, ActivityToOpen.class);
intent.putExtra("key",Your Object to pass);// getDataAdapter1 in your case
context.startActivity(intent);
}
});
then in your Startted activity receive your object like this
YourObject obj = getIntent().getParcelableExtra("key");
//Your case
GetDataAdapter obj = getIntent().getParcelableExtra("key");

How to show details from my web service to recycler view

hi i try to show some details from my web service to recycler view ,i can take the other details except the images to my recyclerview.Pls help me
My fragment is:
public class MainActivityFragment extends Fragment {
private static final String TAG = "RecyclerViewExample";
private final List<Person> mPersonList = new ArrayList<>();
public MainActivityFragment() {
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container, false);
Context context = getActivity();
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
LinearLayoutManager layoutManager = new LinearLayoutManager(context);
recyclerView.setLayoutManager(layoutManager);
final RVAdapter rvAdapter = new RVAdapter(mPersonList);
recyclerView.setAdapter(rvAdapter);
RequestQueue requestQueue = Volley.newRequestQueue(context);
String url = "http://onam.leah.in/new/item_details.php";
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
try {
for (int i = 0; i < response.length(); i++) {
Log.d(TAG, response.toString());
JSONObject jsonObject = response.getJSONObject(i);
String phone_name = jsonObject.getString("Phone");
String phone_price = jsonObject.getString("Price");
String thumbnail=jsonObject.getString("Image");
Person person=new Person(phone_name,phone_price,thumbnail);
mPersonList.add(i, person);
}
rvAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e("VOLLEY", error.toString());
}
});
requestQueue.add(jsonArrayRequest);
return view;
}
}
my person class is
class Person {
public String phone_name;
public String phone_price;
public String thumbnail;
Person(String phone_name, String phone_price,String thumbnail) {
this.phone_name = phone_name;
this.phone_price = phone_price;
this.thumbnail=thumbnail;
}
}
adapter class is
public class RVAdapter extends
RecyclerView.Adapter<RVAdapter.PersonViewHolder> {
List<Person> persons; // private Context mContext;
private ImageLoader mImageLoader;
RVAdapter(List<Person> persons) {
this.persons = persons;
}
#Override
public PersonViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview,
parent, false);
PersonViewHolder pvh = new PersonViewHolder(v);
return pvh;
}
#Override
public void onBindViewHolder(PersonViewHolder holder, int position) {
mImageLoader = AppController.getInstance().getImageLoader(); //
holder.thumbnail.setImageUrl(persons.get(position).thumbnail,
mImageLoader);
holder.phoneName.setText(persons.get(position).phone_name);
holder.phonePrice.setText(persons.get(position).phone_price);
if (mImageLoader == null)
mImageLoader = AppController.getInstance().getImageLoader();
holder.thumbnail.setImageUrl(persons.get(position).thumbnail,
mImageLoader);
}
#Override
public int getItemCount() {
if (persons != null) {
return persons.size();
}
return 0;
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
public static class PersonViewHolder extends RecyclerView.ViewHolder {
CardView cv;
TextView phoneName;
TextView phonePrice;
NetworkImageView thumbnail;
PersonViewHolder(View itemView) {
super(itemView);
cv = (CardView) itemView.findViewById(R.id.card_view);
phoneName = (TextView) itemView.findViewById(R.id.phone_name);
phonePrice = (TextView) itemView.findViewById(R.id.phone_price);
thumbnail=(NetworkImageView) itemView.findViewById(R.id.thumbnail);
}
}
while am runing the app i got error as:
java.lang.NullPointerException: Attempt to invoke virtual method 'com.android.volley.toolbox.ImageLoader harico.mylastrecycle.AppController.getImageLoader()' on a null object reference
at harico.mylastrecycle.RVAdapter.onBindViewHolder(RVAdapter.java:39)
at harico.mylastrecycle.RVAdapter.onBindViewHolder(RVAdapter.java:20)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5138)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4433)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4326)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1955)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1364)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1327)
Firstly,do not use Volley.newRequestQueue() all the time you want make a new call.Each time you use this a new RequestQueue will be added which will degrade performance of your app.Instead use one RequestQueue and add all the request to the queue as following:
AppController.getInstance().addToRequestQueue(jsonArrayRequest , "SOITEM");
The best and easy way to add images to your Recycler View is by using Picasso library.
(or)
use:
imageLoader = MySingleton.getInstance().getImageLoader(getActivity());
NetworkImageView niv_networkimageview = (NetworkImageView)convertView.findViewById(R.id.niv_networkimageview);
niv_networkimageview.setImageUrl(al_ImageUrls.get(position), imageLoader);
niv_networkimageview.setPadding(5, 0, 5, 0);
write the above code in getView() method.
also create a ImageLoader class in MySingleton like this:
public RequestQueue getRequestQueue(Context context){
mRequestQueue = Volley.newRequestQueue(context);
return this.mRequestQueue;
}
public ImageLoader getImageLoader(Context context){
mImageLoader = new ImageLoader(getRequestQueue(context), new ImageLoader.ImageCache() {
private final LruCache<String, Bitmap> mCache = new LruCache<String, Bitmap>(10);
public void putBitmap(String url, Bitmap bitmap) {
mCache.put(url, bitmap);
}
public Bitmap getBitmap(String url) {
return mCache.get(url);
}
});
return this.mImageLoader;
}

Categories

Resources