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;
}
Related
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");
My recyclerview is not populating inside fragment. I am getting proper response from volley. I have parsed the JSON in arraylist in proper format only problem is that recyclerview is not populating.
MyFragment.java
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_undekha_tadka, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
myVolleyOperation(view);
return view;
}
private void myVolleyOperation(View v) {
volleySingleton = VolleySingleton.getInstance();
requestQueue = volleySingleton.getRequestQueue();
String url = "http://abcd.com/undekha.html";
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
jsonParsing(response);
Toast.makeText(getContext(), response, Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getContext(), "ERROR\n" + error.toString(), Toast.LENGTH_SHORT).show();
}
});
requestQueue.add(stringRequest);
recyclerViewAdapter = new RecyclerViewAdapter(undekhaList, v.getContext());
layoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(recyclerViewAdapter);
}
RecyclerViewAdapter.java
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.RecyclerViewHolder> {
ArrayList<UndekhaModel> myArrayList = new ArrayList<>();
Context context;
VolleySingleton volleySingleton;
ImageLoader imageLoader;
public RecyclerViewAdapter(ArrayList<UndekhaModel> myArrayList, Context context) {
this.myArrayList = myArrayList;
this.context = context;
volleySingleton = VolleySingleton.getInstance();
imageLoader = volleySingleton.getImageLoader();
}
#Override
public RecyclerViewAdapter.RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_row, parent, false);
RecyclerViewHolder recyclerViewHolder = new RecyclerViewHolder(view, context, myArrayList);
return recyclerViewHolder;
}
#Override
public void onBindViewHolder(final RecyclerViewAdapter.RecyclerViewHolder holder, int position) {
UndekhaModel undekhaModel = myArrayList.get(position);
holder.title.setText(undekhaModel.getTitle());
String loadUrl = "http://img.youtube.com/vi/" + undekhaModel.getId() + "/0.jpg";
if (loadUrl!=null){
imageLoader.get(loadUrl, new ImageLoader.ImageListener() {
#Override
public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
holder.thumbnail.setImageBitmap(response.getBitmap());
}
#Override
public void onErrorResponse(VolleyError error) {
holder.thumbnail.setImageResource(R.mipmap.ic_launcher);
}
});
}
}
#Override
public int getItemCount() {
return myArrayList.size();
}
public static class RecyclerViewHolder extends RecyclerView.ViewHolder {
TextView title;
ImageView thumbnail;
ArrayList<UndekhaModel> modelArrayList = new ArrayList<>();
Context context;
public RecyclerViewHolder(View itemView, final Context ctx, final ArrayList<UndekhaModel> modelArrayList) {
super(itemView);
this.context = ctx;
this.modelArrayList = modelArrayList;
title = (TextView) itemView.findViewById(R.id.tvTitle);
thumbnail = (ImageView) itemView.findViewById(R.id.ivShow);
}
}
}
Your method jsonParsing() needs to populate data for your RecyclerViewAdapter, needs to notify of the update and the adapter needs to return the correct count of items it is managing. Right now its getItemCount() is always returning 0, so the RecyclerView does not think there is anything to show.
Same issue than here
https://stackoverflow.com/a/35802948/2144418
Try setting the adapter before the layout manager
Change the last two lines on myVolleyOperation to this
recyclerView.setAdapter(recyclerViewAdapter);
recyclerView.setLayoutManager(layoutManager);
i responsed json object from api and i tried to send the response to the module using setter then to the Recyclerview adapter but its not working
This is my fragment
public class ListViewActivityFragment extends Fragment {
List<AppShowModule> appShowModule;
RecyclerView AppRecyclerView;
RecyclerView.LayoutManager AppRecyclerViewlayoutManager;
RecyclerView.Adapter AppRecyclerViewadapter;
String jsonUrl = "https://itunes.apple.com/jo/rss/topfreeapplications/limit=50/json";
TextView text;
Context context;
RequestQueue requestQueue;
public ListViewActivityFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_list_view, container, false);
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
appShowModule = new ArrayList<>();
AppRecyclerView = (RecyclerView) getView().findViewById( R.id.AppRecyclerView );
AppRecyclerView.setHasFixedSize( true );
AppRecyclerView.setLayoutManager( AppRecyclerViewlayoutManager );
JsonAppShowData();}
public void JsonAppShowData() {
final JsonObjectRequest jsonObjectRequest = new JsonObjectRequest( jsonUrl, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONObject("feed").getJSONArray( "entry" );
AppShowModule appShowModule111 = new AppShowModule();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject json1 = jsonArray.getJSONObject( i ).getJSONObject("im:name");
String name = response.getJSONObject("feed").getJSONArray("entry").getJSONObject(i).getJSONObject("im:name").getString("label").toString();
appShowModule111.setAppName(name);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e( "LOG", error.toString() );
}
} );
requestQueue = Volley.newRequestQueue( getContext() );
requestQueue.add(jsonObjectRequest);
AppRecyclerView.setLayoutManager(new LinearLayoutManager(context));
AppRecyclerViewadapter = new ListViewAdapter(appShowModule,getContext() );
AppRecyclerView.setAdapter(AppRecyclerViewadapter);
}
}
And this is my adapter
public class ListViewAdapter extends RecyclerView.Adapter<ListViewAdapter.ViewHolder> {
Context context;
List<AppShowModule> getDataAdapter;
public ListViewAdapter(List<AppShowModule> 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.appitem, parent, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
#Override
public void onBindViewHolder(final ViewHolder holder, int position)
{
final AppShowModule getDataAdapter1 = getDataAdapter.get( position );
holder.NameTextView.setText( getDataAdapter1.getAppName() );
Picasso.with( context ).load( getDataAdapter1.getAppImageUrl() ).into( holder.imgPost );
}
#Override
public int getItemCount() {
return getDataAdapter.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
public TextView NameTextView;
public ImageView imgPost;
LinearLayout lnrLayout;
public ViewHolder(View itemView) {
super(itemView);
NameTextView = (TextView) itemView.findViewById(R.id.appName);
imgPost = (ImageView) itemView.findViewById(R.id.appImage);
lnrLayout = (LinearLayout)itemView.findViewById(R.id.lnrLayout);
}
}
}
And this is my module
public class AppShowModule
{
private String appName;
private String appImageUrl;
public String getAppName() {
return appName;}
public void setAppName(String appName) {
this.appName = appName;}
public String getAppImageUrl() {
return appImageUrl;}
public void setAppImageUrl(String appImageUrl) {
this.appImageUrl = appImageUrl;}
}
I think you should not initialize your adapter when you get a response from the web service. Do it on activity create and when you get your data, update the adapter.
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);
This is my Fragment class in here I can't create instance of Person class. While try am to create instance of Person class, showing error as Person(String,String) in Person can not be applied to zero.
public class MainActivityFragment extends Fragment {
private final List<Person> mPersonList = new ArrayList<>();
public MainActivityFragment() {
}
#Override
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://192.16.1.100/api/persons";
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
try {
for (int i = 0; i < response.length(); i++) {
JSONObject jsonObject = response.getJSONObject(i);
Person person=new Person();
person.phone_name = jsonObject.getString("name");
person.phone_price = jsonObject.getString("dddd");
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
class Person {
public String phone_name;
public String phone_price;
Person(String phone_name, String phone_price) {
this.phone_name = phone_name;
this.phone_price = phone_price;
} }
My Adapter class is
public class RVAdapter extends
RecyclerView.Adapter<RVAdapter.PersonViewHolder> {
List<Person> persons;
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) {
holder.phoneName.setText(persons.get(position).phone_name);
holder.phonePrice.setText(persons.get(position).phone_price);
}
#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;
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);
}
} }
please try to change the following things
String phone_name = jsonObject.getString("name"
String phone_price = jsonObject.getString("dddd");
Person person=new Person(phone_name,phone_price);