I have responsed JSONObject 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;
List<AppShowModule> GetDataAdapter1;
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 );
text = (TextView) getView().findViewById(R.id.textView2);
AppRecyclerView.setHasFixedSize( true );
GetDataAdapter1 = new ArrayList<>();
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" );
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject json1 = jsonArray.getJSONObject( i ).getJSONObject("im:name");
AppShowModule appShowModule111 = new AppShowModule();
String name = response.getJSONObject("feed").getJSONArray("entry").getJSONObject(i).getJSONObject("im:name").getString("label").toString();
text.setText(name);
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 RecyclerViewAdapter(GetDataAdapter1,this);
AppRecyclerView.setAdapter(AppRecyclerViewadapter);
}
}
and this is my adapter
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
Context context;
List<AppShowModule> getDataAdapter;
Context MContext;
public RecyclerViewAdapter(List<AppShowModule> getDataAdapter, ListViewActivityFragment context){
super();
this.getDataAdapter = getDataAdapter;
}
#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;}
}
Yo are creating the AppShowModule appShowModule111 = new AppShowModule(); Object but you never populated your adaapter with that object. Your JsonObjectRequest onResponse() should look like this;
#Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONObject("feed").getJSONArray( "entry" );
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject json1 = jsonArray.getJSONObject( i ).getJSONObject("im:name");
AppShowModule appShowModule111 = new AppShowModule();
String name = response.getJSONObject("feed").getJSONArray("entry").getJSONObject(i).getJSONObject("im:name").getString("label").toString();
text.setText(name);
appShowModule111.setAppName((name));
GetDataAdapter1.add(appShowModule111);
}
AppRecyclerViewadapter = new RecyclerViewAdapter(GetDataAdapter1,ListViewActivityFragment.this);
AppRecyclerView.setAdapter(AppRecyclerViewadapter);
} catch (JSONException e) {
e.printStackTrace();
}}
Related
I am trying to add recyclerview to another recyclerview.
Like Sub Category Recyclerview in its Category RecyclerViews.
problem is that I am getting All categorie's sub category item list in last item of category list.
means
Cat1
cat2
catn(then below this list of subcat1, subcat2....so on)
but it should be like cat1 then in below subcat1 and so on...
here is MainActivity code from where I am calling the api and passing adapter to recyclerview
private void getCategory() {
StringRequest stringRequest = new StringRequest(Request.Method.GET, CategoryUrl, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
JSONObject obj = new JSONObject(response);
JSONArray jsonArray = obj.getJSONArray("DATA");
Log.v("Cat Response", response.toString());
for (int i = 0; i < jsonArray.length(); i++) {
DataModel Model = new DataModel();
JSONObject jsonObject = jsonArray.getJSONObject(i);
catId = jsonObject.getString("ID");
catTitle = jsonObject.getString("TITLE");
Model.setTITLE(catTitle);
Model.setID(catId);
catList.add(Model);
categoryAdaptor.getSubCategory(catId);
}
categoryAdaptor.notifyDataSetChanged();
} catch (JSONException e) {
System.out.print("Exception is :" + e.getMessage());
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
private void setCatAdaptor() {
#SuppressLint("WrongConstant") LinearLayoutManager gridLayoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayout.VERTICAL, false);
recyclerView1.setLayoutManager(gridLayoutManager); // set LayoutManager to RecyclerView
// call the constructor of CustomAdapter to send the reference and data to Adapter
categoryAdaptor = new CategoryAdaptor(this, catList);
recyclerView1.setAdapter(categoryAdaptor);
}
here is category adapter
public class CategoryAdaptor extends RecyclerView.Adapter<CategoryAdaptor.MyViewHlder> {
private Activity activity;
private ArrayList<DataModel> list;
private SubCategoryAdaptor subcategoryAdaptor;
private ArrayList<SubCatModel> subCatList = new ArrayList<>();
private String SubCategoryUrl = "http://philiabeauty.com/reader-choice/controller-api.php?action=get_subcategory_by_category&id=";
private String Token = "&access_token=Ym9va19kZXRhaWxz";
private String imgRoot = "http://philiabeauty.com/reader-choice/";
private String subCatId, subCatTitle, subCatImage;
public CategoryAdaptor(Activity activity, ArrayList<DataModel> list) {
this.activity = activity;
this.list = list;
}
#NonNull
#Override
public MyViewHlder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.category_list_layout, viewGroup, false);
MyViewHlder my = new MyViewHlder(view);
return my;
}
#Override
public void onBindViewHolder(#NonNull MyViewHlder myViewHlder, int i) {
final DataModel model = list.get(i);
myViewHlder.catText.setText(model.getTITLE());
myViewHlder.catId.setText(model.getID());
setSubCatAdaptor(myViewHlder);
}
#Override
public int getItemCount() {
return list.size();
}
public void getSubCategory(String ID) {
String SubCategoryUrlFinal = SubCategoryUrl + ID + Token;
StringRequest stringRequest = new StringRequest(Request.Method.GET, SubCategoryUrlFinal, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
JSONObject obj = new JSONObject(response);
JSONArray jsonArray = obj.getJSONArray("DATA");
Log.v("SubCat Response", response.toString());
for (int i = 0; i < 3; i++) {
SubCatModel Model = new SubCatModel();
JSONObject jsonObject = jsonArray.getJSONObject(i);
subCatId = jsonObject.getString("ID");
subCatTitle = jsonObject.getString("TITLE");
subCatImage = imgRoot + jsonObject.getString("IMG_SRC");
Model.setSubCatIMG(subCatImage);
Model.setSubCatTITLE(subCatTitle);
Model.setSubCatID(subCatId);
subCatList.add(Model);
}
subcategoryAdaptor.notifyDataSetChanged();
} catch (JSONException e) {
System.out.print("Exception is :" + e.getMessage());
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
RequestQueue requestQueue = Volley.newRequestQueue(activity);
requestQueue.add(stringRequest);
}
private void setSubCatAdaptor(MyViewHlder myView) {
Log.v("test", "test" );
#SuppressLint("WrongConstant") LinearLayoutManager gridLayoutManager = new LinearLayoutManager(activity, LinearLayout.HORIZONTAL, false);
myView.recyclerviewSubCat.setLayoutManager(gridLayoutManager); // set LayoutManager to RecyclerView
// call the constructor of CustomAdapter to send the reference and data to Adapter
subcategoryAdaptor = new SubCategoryAdaptor(activity, subCatList);
myView.recyclerviewSubCat.setAdapter(subcategoryAdaptor);
}
class MyViewHlder extends RecyclerView.ViewHolder {
private TextView catText, viewAllBtn, catId, tag;
private RecyclerView recyclerviewSubCat;
public MyViewHlder(#NonNull View itemView) {
super(itemView);
catText = itemView.findViewById(R.id.cat_name);
viewAllBtn = itemView.findViewById(R.id.viewAllBtn);
catId = itemView.findViewById(R.id.cat_id);
recyclerviewSubCat = itemView.findViewById(R.id.recyclerviewSubCat);
}
}
}
and here is the sub category adapter
public class SubCategoryAdaptor extends RecyclerView.Adapter<SubCategoryAdaptor.MyViewHlder> {
private Activity activity;
private ArrayList<SubCatModel> list;
public SubCategoryAdaptor(Activity activity, ArrayList<SubCatModel> list) {
this.activity = activity;
this.list = list;
}
#NonNull
#Override
public MyViewHlder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.subcat_list, viewGroup, false);
MyViewHlder my = new MyViewHlder(view);
return my;
}
#Override
public void onBindViewHolder(#NonNull MyViewHlder myViewHlder, int i) {
final SubCatModel model = list.get(i);
Log.v("data", model.getSubCatTITLE());
myViewHlder.subCatTitle.setText(model.getSubCatTITLE());
myViewHlder.subCatId.setText(model.getSubCatID());
String img = model.getSubCatIMG();
Picasso.get()
.load(img)
.placeholder(R.drawable.ic_book_black_24dp)
.fit()
.into(myViewHlder.subCatImage);
}
#Override
public int getItemCount() {
return list.size();
}
class MyViewHlder extends RecyclerView.ViewHolder {
private TextView subCatTitle, subCatId, subCatTag;
private ImageView subCatImage;
public MyViewHlder(#NonNull View itemView) {
super(itemView);
subCatTitle = itemView.findViewById(R.id.subcat_name);
subCatId = itemView.findViewById(R.id.subcat_id);
subCatTag = itemView.findViewById(R.id.subcat_tag);
subCatImage = itemView.findViewById(R.id.subcat_image);
}
}
}
Changed the sub category adapter declaration to viewHolder,
public class CategoryAdaptor extends RecyclerView.Adapter<CategoryAdaptor.MyViewHlder> {
private Activity activity;
private ArrayList<DataModel> list;
private String SubCategoryUrl = "http://philiabeauty.com/reader-choice/controller-api.php?action=get_subcategory_by_category&id=";
private String Token = "&access_token=Ym9va19kZXRhaWxz";
private String imgRoot = "http://philiabeauty.com/reader-choice/";
private String subCatId, subCatTitle, subCatImage;
public CategoryAdaptor(Activity activity, ArrayList<DataModel> list) {
this.activity = activity;
this.list = list;
}
#NonNull
#Override
public MyViewHlder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.category_list_layout, viewGroup, false);
MyViewHlder my = new MyViewHlder(view);
return my;
}
#Override
public void onBindViewHolder(#NonNull MyViewHlder myViewHlder, int i) {
final DataModel model = list.get(i);
myViewHlder.catText.setText(model.getTITLE());
myViewHlder.catId.setText(model.getID());
getSubCategory(myViewHlder,model.getID());
}
#Override
public int getItemCount() {
return list.size();
}
public void getSubCategory(MyViewHlder myViewHlder,String ID) {
String SubCategoryUrlFinal = SubCategoryUrl + ID + Token;
StringRequest stringRequest = new StringRequest(Request.Method.GET, SubCategoryUrlFinal, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
JSONObject obj = new JSONObject(response);
JSONArray jsonArray = obj.getJSONArray("DATA");
Log.v("SubCat Response", response.toString());
for (int i = 0; i < 3; i++) {
SubCatModel Model = new SubCatModel();
JSONObject jsonObject = jsonArray.getJSONObject(i);
subCatId = jsonObject.getString("ID");
subCatTitle = jsonObject.getString("TITLE");
subCatImage = imgRoot + jsonObject.getString("IMG_SRC");
Model.setSubCatIMG(subCatImage);
Model.setSubCatTITLE(subCatTitle);
Model.setSubCatID(subCatId);
myViewHlder.subCatList.add(Model);
}
myViewHlder.subcategoryAdaptor.notifyDataSetChanged();
} catch (JSONException e) {
System.out.print("Exception is :" + e.getMessage());
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
RequestQueue requestQueue = Volley.newRequestQueue(activity);
requestQueue.add(stringRequest);
}
class MyViewHlder extends RecyclerView.ViewHolder {
private TextView catText, viewAllBtn, catId, tag;
private RecyclerView recyclerviewSubCat;
private SubCategoryAdaptor subcategoryAdaptor;
private ArrayList<SubCatModel> subCatList = new ArrayList<>();
public MyViewHlder(#NonNull View itemView) {
super(itemView);
catText = itemView.findViewById(R.id.cat_name);
viewAllBtn = itemView.findViewById(R.id.viewAllBtn);
catId = itemView.findViewById(R.id.cat_id);
recyclerviewSubCat = itemView.findViewById(R.id.recyclerviewSubCat);
LinearLayoutManager gridLayoutManager = new LinearLayoutManager(activity, LinearLayout.HORIZONTAL, false);
recyclerviewSubCat.setLayoutManager(gridLayoutManager); // set LayoutManager to RecyclerView
// call the constructor of CustomAdapter to send the reference and data to Adapter
subcategoryAdaptor = new SubCategoryAdaptor(activity, subCatList);
recyclerviewSubCat.setAdapter(subcategoryAdaptor);
}
}
}
I have a fragment, the name is galery. in the fragment galery i'll showing a recycleView that contains name list. but i find an error in this script :
LinearLayoutManager llm = new LinearLayoutManager(this);
and this my full code :
public class galery extends Fragment {
private RecyclerView lvhape;
private RequestQueue requestQueue;
private StringRequest stringRequest;
ArrayList<HashMap<String, String>> list_data;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_galery, container, false);
String url = "http://bls.hol.es/read.php";
lvhape = (RecyclerView) rootView.findViewById(R.id.lvhape);
LinearLayoutManager llm = new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
lvhape.setLayoutManager(llm);
requestQueue = Volley.newRequestQueue(galery.this);
list_data = new ArrayList<HashMap<String, String>>();
stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.d("response", response);
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray = jsonObject.getJSONArray("result");
for (int a = 0; a < jsonArray.length(); a++) {
JSONObject json = jsonArray.getJSONObject(a);
HashMap<String, String> map = new HashMap<String, String>();
map.put("id", json.getString("id"));
map.put("nama", json.getString("nama"));
map.put("alamat", json.getString("alamat"));
map.put("poto", json.getString("poto"));
list_data.add(map);
AdapterList adapter = new AdapterList(galery.this, list_data);
lvhape.setAdapter(adapter);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getActivity().getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
requestQueue.add(stringRequest);
return rootView;
}
}
Try This On Fragment Create Adapter And Model Class And RecyclerView in .xml Class.
public class Event extends Fragment {
RecyclerView recyclerView;
private Event_Adapter event_adapter;
public Event_Model event_model;
public static List<Event_Model> list;
RecyclerView.LayoutManager mLayoutManager;
private static final String FETCH_ID = "id";
private static final String FETCH_NAME = "event_type";
private static final String FETCH_MOBILE = "contact";
private static final String FETCH_DATE = "date";
private static final String FETCH_IMAGE = "uri";
public static final String FETCH_DETAIL = "Your Url";
String type,number,date,uri,id;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v= inflater.inflate(R.layout.fragment_event, container, false);
list = new ArrayList<Event_Model>();
recyclerView = (RecyclerView) v.findViewById(R.id.event_recycler);
mLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(mLayoutManager);
fetchdetails();
return v;
}
private void fetchdetails(){
StringRequest jor = new StringRequest(Request.Method.POST, FETCH_DETAIL,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
/* Toast.makeText(getActivity(),response,Toast.LENGTH_LONG).show();*/
try {
JSONArray ja = new JSONArray(response);
for (int i = 0; i < ja.length(); i++) {
JSONObject jsonObject = ja.getJSONObject(i);
Integer.parseInt(jsonObject.optString("id").toString());
type = jsonObject.getString(FETCH_NAME);
number = jsonObject.getString(FETCH_MOBILE);
date = jsonObject.getString(FETCH_DATE);
uri = jsonObject.getString(FETCH_IMAGE);
id = jsonObject.getString(FETCH_ID);
event_model = new Event_Model(type,number,date,uri,id);
list.add(event_model);
}
} catch (JSONException e) {
e.printStackTrace();
}
event_adapter= new Event_Adapter(getActivity(), list);
recyclerView.setAdapter(event_adapter);
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e("Volley", "Error");
}
}
);
RetryPolicy retryPolicy=new DefaultRetryPolicy(60000,0,DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
jor.setRetryPolicy(retryPolicy);
VolleySingleton.getInstance(getActivity()).addToRequestQueue(jor);
}
}
I did some changes it works fine in my app try it and if any error occurred mention in comment.
public class GalaryFragment extends Fragment {
private RecyclerView recyclerView;
private List<ProductModel> list1;
private ProductModel productMode;
private ProductAdapter productAdapter;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.frament_galary, container, false);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
String url = "http://bls.hol.es/read.php";
mRecyclerView = (RecyclerView) view.findViewById(R.id.lvhape);
list1 = new ArrayList<>();
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false));
cashbackAdapter = new ProductAdapter(getActivity(), list1);
mRecyclerView2.setAdapter(productAdapter);
final ProgressDialog dialog = new ProgressDialog(getActivity());
dialog.setMessage("Loading......");
dialog.show();
StringRequest stringRequest = new StringRequest(Method.POST, url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
//Toast.makeText(getActivity(),response+"",Toast.LENGTH_LONG).show();
try {
JSONArray jsonArray = new JSONArray(response);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String id = jsonObject.getString("id");
String name = jsonObject.getString("name");
String image = jsonObject.getString("image");
productModel = new ProductModel(id, name, image);
list1.add(productModel);
}
productAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e("Volley", "Error");
}
});
VolleySingleton.getInstance(getActivity()).addToRequestQueue(stringRequest);
}
}
ProductAdapter code:
public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.MyViewHolder> {
private List<ProductModel> list;
private Context mContext;
public ProductAdapter(Context mContext, List<ProductModel> list) {
this.mContext = mContext;
this.list=list;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_galary, parent, false);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
final ProductModel productModel=list.get(position);
holder.name.setText(productModel.getName());
Picasso.with(mContext)
.load(productModel.getImage())
.placeholder(R.mipmap.ic_launcher)
.error(R.mipmap.ic_launcher)
.into(holder.iv);
}
#Override
public int getItemCount() {
return list.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView name;
public ImageView iv;
public MyViewHolder(View view) {
super(view);
name = (TextView) view.findViewById(R.id.name);
iv = (ImageView) view.findViewById(R.id.image);
}
}
}
PoductModel code:
public class ProductModel {
String id;
String name;
String image;
public nameModel(String id, String name, String image) {
this.id = id;
this.name = name;
this.image = image;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getname() {
return name;
}
public void setname(String name) {
this.name = name;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
}
VolleySingleton class:
public class VolleySingleton {
private static VolleySingleton mInstance;
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private static Context mCtx;
private VolleySingleton(Context context) {
mCtx = context;
mRequestQueue = getRequestQueue();
mImageLoader = new ImageLoader(mRequestQueue,
new ImageLoader.ImageCache() {
private final LruCache<String, Bitmap>
cache = new LruCache<String, Bitmap>(20);
#Override
public Bitmap getBitmap(String url) {
return cache.get(url);
}
#Override
public void putBitmap(String url, Bitmap bitmap) {
cache.put(url, bitmap);
}
});
}
public static synchronized VolleySingleton getInstance(Context context) {
if (mInstance == null) {
mInstance = new VolleySingleton(context);
}
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
// getApplicationContext() is key, it keeps you from leaking the
// Activity or BroadcastReceiver if someone passes one in.
mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
}
return mRequestQueue;
}
public <T> void addToRequestQueue(Request<T> req) {
getRequestQueue().add(req);
}
public ImageLoader getImageLoader() {
return mImageLoader;
}
}
try this
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_galery, container, false);
String url = "http://bls.hol.es/read.php";
lvhape = (RecyclerView) getView().findViewById(R.id.lvhape);
LinearLayoutManager llm = new LinearLayoutManager(gallary.getContext());
llm.setOrientation(LinearLayoutManager.VERTICAL);
lvhape.setLayoutManager(llm);
requestQueue = Volley.newRequestQueue(galery.this);
list_data = new ArrayList<HashMap<String, String>>();
stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.d("response", response);
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray = jsonObject.getJSONArray("result");
for (int a = 0; a < jsonArray.length(); a++) {
JSONObject json = jsonArray.getJSONObject(a);
HashMap<String, String> map = new HashMap<String, String>();
map.put("id", json.getString("id"));
map.put("nama", json.getString("nama"));
map.put("alamat", json.getString("alamat"));
map.put("poto", json.getString("poto"));
list_data.add(map);
AdapterList adapter = new AdapterList(galery.this, list_data);
lvhape.setAdapter(adapter);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getActivity().getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
requestQueue.add(stringRequest);
return rootView;
}
onCreate() will be called before onCreateView() methods so getView() will return null
Try like this
public class galery extends Fragment {
private RecyclerView lvhape;
private RequestQueue requestQueue;
private StringRequest stringRequest;
ArrayList<HashMap<String, String>> list_data;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_galery, container, false);
return rootView;
}
public void onViewCreated(View view, Bundle savedInstanceState) {
String url = "http://bls.hol.es/read.php";
lvhape = (RecyclerView) view.findViewById(R.id.lvhape);
LinearLayoutManager llm = new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
lvhape.setLayoutManager(llm);
requestQueue = Volley.newRequestQueue(getActivity());
list_data = new ArrayList<HashMap<String, String>>();
stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.d("response", response);
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray = jsonObject.getJSONArray("result");
for (int a = 0; a < jsonArray.length(); a++) {
JSONObject json = jsonArray.getJSONObject(a);
HashMap<String, String> map = new HashMap<String, String>();
map.put("id", json.getString("id"));
map.put("nama", json.getString("nama"));
map.put("alamat", json.getString("alamat"));
map.put("poto", json.getString("poto"));
list_data.add(map);
AdapterList adapter = new AdapterList(galery.this, list_data);
lvhape.setAdapter(adapter);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getActivity().getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
requestQueue.add(stringRequest);
}
i have this Json Data
and i prasing the data in in this json to this ,,
enter image description here
now i wont when click on the image of the application ,, open another activty and display the images in the "im:image" Jsonarray in the json !
this code give to me all images ,, but i wont just the image thats i clicked
this is my Module
private List<String> Allimage = new ArrayList<String>();
public List<String> getAllimage() {
return Allimage;}
public void setAllimage(List<String> allimage) {
Allimage = allimage;}
the Adapter
public class ImageListAdapter extends RecyclerView.Adapter<ImageListAdapter.ViewHolder> {
List<AppShowModule> appShowModules;
List<AppShowModule> imageUrls ;
Context context;
public ImageListAdapter(List<AppShowModule> appShowModules, Context context){
super();
this.appShowModules = appShowModules;
this.context = context;}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from( parent.getContext() ).inflate( R.layout.imagelayout, parent,false );
ViewHolder viewHolder = new ViewHolder( v );
return viewHolder;}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
final AppShowModule appShowModule = appShowModules.get( position );
Picasso.with(context).load( appShowModule.getAllimage() ).into( holder.appImage );
}
#Override
public int getItemCount() {
return appShowModules.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
public ImageView appImage;
public ViewHolder(View itemView) {
super(itemView);
appImage = (ImageView) itemView.findViewById( R.id.appImage);
}}}
and this is the Fragment `
public class ImageListFragment extends Fragment {
List<AppShowModule> appShowModules;
Context context;
List<AppShowModule> imagesModule;
RecyclerView AppRecyclerView;
ImageView Imageview;
RecyclerView.Adapter imageRecyclerViewadapter;
List<String> imageUrls;
String feedKey = "feed";
String entryKey = "entry";
String nameKey = "im:name";
String imageKey = "im:image";
String labelKey = "label";
String artistKey = "im:artist";
String contentTypeKey = "im:contentType";
String attribueKey = "attributes";
String rightsKey = "rights";
String categoryKey = "category";
String relaseDateKey = "im:releaseDate";
String linkKey = "link";
String hrefKey = "href";
String summaryKey = "summary";
String jsonUrl = "https://itunes.apple.com/jo/rss/topfreeapplications/limit=50/json";
RequestQueue requestQueue;
private RecyclerView.LayoutManager mLayoutManager;
public ImageListFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_image_list, container, false);
}
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
AppRecyclerView = (RecyclerView) getView().findViewById(R.id.imageRecyclerView);
imagesModule = new ArrayList<>();
appShowModules = new ArrayList<>();
imageUrls = new ArrayList<>();
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 appShowModule = new AppShowModule();
for (int i = 0; i < jsonArray.length(); i++) {
String image = response.getJSONObject(feedKey).getJSONArray(entryKey).getJSONObject(0).getJSONArray(imageKey).getJSONObject( 0 ).getString(labelKey).toString();
imageUrls.add(image);
appShowModule.setAllimage(imageUrls);
appShowModules.add(appShowModule);
}
imageRecyclerViewadapter = new ImageListAdapter(appShowModules,getContext());
AppRecyclerView.setAdapter(imageRecyclerViewadapter);
} 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);
mLayoutManager = new GridLayoutManager( getContext().getApplicationContext(),3);
AppRecyclerView.setLayoutManager(mLayoutManager); }}
You are trying to load complete list string by doing appShowModule.getAllimage() which is wrong. try to do following.
Picasso.with(context).load(appShowModule.getAllimage().get(position)).into(holder.appImage);
hello i have this Json Data a link
and i parsing it like that
when i click into the image i want to parsing the JsonArray for the image to piccaso in recycler view but i got all image in the Json like that
for example if i click in "emo" i wont just image for "emo" not for all images !
any idea ?
this is my module :
public class AppShowModule
{
private List<String> Allimage = new ArrayList<String>();
public List<String> getAllimage() {
return Allimage;}
public void setAllimage(List<String> allimage) {
Allimage = allimage;}
this is my Fragment
public class ImageListFragment extends Fragment {
List<AppShowModule> appShowModules;
List<AppShowModule> imagesModule;
RecyclerView AppRecyclerView;
RecyclerView.Adapter imageRecyclerViewadapter;
List<String> imageUrls;
String feedKey = "feed";
String entryKey = "entry";
String imageKey = "im:image";
String labelKey = "label";
String jsonUrl = "https://itunes.apple.com/jo/rss/topfreeapplications/limit=50/json";
RequestQueue requestQueue;
private RecyclerView.LayoutManager mLayoutManager;
public ImageListFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_image_list, container, false);
}
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
AppRecyclerView = (RecyclerView) getView().findViewById(R.id.imageRecyclerView);
imagesModule = new ArrayList<>();
appShowModules = new ArrayList<>();
imageUrls = new ArrayList<>();
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(feedKey).getJSONArray( entryKey );
AppShowModule appShowModule = new AppShowModule();
for (int i = 0; i < jsonArray.length(); i++) {
JSONArray imageArray = response.getJSONObject(feedKey).getJSONArray(entryKey).getJSONObject(i).getJSONArray(imageKey);
for (int j = 0; j < imageArray.length(); j++) {
String image = imageArray.getJSONObject(j).getString(labelKey).toString();
imageUrls.add(image);
appShowModule.setAllimage(imageUrls);
appShowModules.add(appShowModule);}}
imageRecyclerViewadapter = new ImageListAdapter(appShowModules,getContext(),imageUrls);
AppRecyclerView.setAdapter(imageRecyclerViewadapter);
} 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);
mLayoutManager = new GridLayoutManager( getContext().getApplicationContext(),3);
AppRecyclerView.setLayoutManager(mLayoutManager); }}
and this is the Recycler adapter
public class ImageListAdapter extends RecyclerView.Adapter<ImageListAdapter.ViewHolder> {
List<AppShowModule> appShowModules;
List<String> imageUrl;
Context context;
public ImageListAdapter(List<AppShowModule> appShowModules, Context context ,List<String>imageUrls
){
super();
this.imageUrl =imageUrls;
this.appShowModules = appShowModules;
this.context = context;}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from( parent.getContext() ).inflate( R.layout.imagelayout, parent,false );
ViewHolder viewHolder = new ViewHolder( v );
return viewHolder;}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
Picasso.with(context).load(imageUrl.get(position)).into(holder.appImage);
}
public int getItemCount() {
return imageUrl.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
public ImageView appImage;
public ViewHolder(View itemView) {
super(itemView);
appImage = (ImageView) itemView.findViewById( R.id.appImage);
}}}
and this is the activity that have the image view where click
public class ListViewDetailsFragment extends Fragment {
ImageView AppImage;
TextView AppName,AppArtist,AppContentType,AppRights,AppCategory,AppRealseDate,AppSammary;
ImageButton AppLink;
Context context;
public ListViewDetailsFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_list_view_details, container, false);}
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
AppImage = (ImageView) getView().findViewById(R.id.imageView);
AppName = (TextView) getView().findViewById(R.id.textname);
AppArtist = (TextView) getView().findViewById(R.id.textartest);
AppContentType = (TextView) getView().findViewById(R.id.textcontent);
AppRights = (TextView) getView().findViewById(R.id.textrights);
AppCategory = (TextView) getView().findViewById(R.id.textCategory);
AppRealseDate = (TextView) getView().findViewById(R.id.textRelease);
AppSammary = (TextView) getView().findViewById(R.id.textSummary);
AppLink = (ImageButton) getView().findViewById(R.id.imageButton);
String name = getActivity().getIntent().getExtras().getString("App_name");
final String image = getActivity().getIntent().getExtras().getString("App_image");
String artist = getActivity().getIntent().getExtras().getString("App_artist");
String contentType = getActivity().getIntent().getExtras().getString("App_ContentType");
String rights = getActivity().getIntent().getExtras().getString("App_Rights");
String category = getActivity().getIntent().getExtras().getString("App_Category");
String realse = getActivity().getIntent().getExtras().getString("App_ReleaseDate");
final String link = getActivity().getIntent().getExtras().getString("App_link");
String sammary = getActivity().getIntent().getExtras().getString("App_summary");
AppName.setText(name);
AppArtist.setText(artist);
AppContentType.setText(contentType);
AppRights.setText(rights);
AppCategory.setText(category);
AppRealseDate.setText(realse);
AppSammary.setText(sammary);
Picasso.with(context).load(image).into(AppImage);
AppLink.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity().getBaseContext(),
WebView.class);
intent.putExtra("App_link", link);
getActivity().startActivity(intent);}});
AppImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String id = (String) view.getTag();
Intent intent = new Intent(getActivity().getBaseContext(), ImageList.class);
intent.putExtra("id", id);
getActivity().startActivity(intent);
}});}}
If you see the JSON properly, there are three types of images with differenct heights i.e. small, medium and large inside im:image.
You need only one not all three.
You have to change below code in your ImageListFragment inside JsonAppShowData() method.
AppShowModule appShowModule = new AppShowModule();
for (int i = 0; i < jsonArray.length(); i++) {
String image = response.getJSONObject(feedKey).getJSONArray(entryKey)
.getJSONObject(i).getJSONArray(imageKey)
/* this is main change -> */.get(2).getString(labelKey).toString();
imageUrls.add(image);
appShowModule.setAllimage(imageUrls);
appShowModules.add(appShowModule);
}
imageRecyclerViewadapter = new ImageListAdapter(appShowModules, getContext(), imageUrls);
Hope it helps you.
sample json: https://jsonblob.com/57d5cff8e4b0dc55a4f46fa8
Suggestion: You should try using GSON for JSON parsing. It is easy to understand and implement.
Dam, this is weard kind of coding. Anyway.. try to set set scaleType in imageview on your xml to centerInside, center, matrix or what ever it fits.
I've a recyclerview adapter with 3 textviews. I used a model class to set texts. Now I want to use the same adapter with different layout and in a different class which only have 1 textview. When I tried, I got NullPointerException (may be becuz the other 2 textviews are blank). Is there any way to use same adapter with different layout and in different class?
// second class - I used 1 textview
public class Customers extends AppCompatActivity{
private CShowProgress cShowProgress;
private RecyclerView mRecyclerView;
private TimeLineAdapter mTimeLineAdapter;
private List<TimeLineModel> mDataList = new ArrayList<>();
private static final String CUSTOMERS = "http://192.168.200.3/ubooktoday/android/showspacustomerlist";
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.customers);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
cShowProgress = CShowProgress.getInstance();
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
mRecyclerView.setLayoutManager(getLinearLayoutManager());
mRecyclerView.setHasFixedSize(true);
showCustomers();
}
private void showCustomers() {
if(mDataList!=null )mDataList.clear();
cShowProgress.showProgress(Customers.this);
StringRequest stringRequest = new StringRequest(Request.Method.POST, CUSTOMERS,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
cShowProgress.hideProgress();
try {
JSONArray jsonArray = new JSONArray(response);
for(int i=0; i<jsonArray.length(); i++){
JSONObject obj = jsonArray.getJSONObject(i);
TimeLineModel model = new TimeLineModel();
model.setCustname(obj.getString("customername"));
mDataList.add(model);
mTimeLineAdapter = new TimeLineAdapter(getApplicationContext(), R.layout.item_row_customer, mDataList);
mRecyclerView.setAdapter(mTimeLineAdapter);
}
mTimeLineAdapter.notifyDataSetChanged();
} catch (JSONException e) {
// JSON error
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), "VolleyError" + error.toString(), Toast.LENGTH_LONG).show();
}
}) {
#Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("spaid", "145");
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
requestQueue.add(stringRequest);
}
private LinearLayoutManager getLinearLayoutManager() {
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext());
return linearLayoutManager;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
break;
}
return true;
}
}
// first class - I used all 3 textviews
public class Walkin extends Fragment{
private RecyclerView mRecyclerView;
private TimeLineAdapter mTimeLineAdapter;
private List<TimeLineModel> mDataList = new ArrayList<>();
private static final String DASHBOARD = "My API";
#Nullable
#Override
#Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.walkin, container, false);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
mRecyclerView.setLayoutManager(getLinearLayoutManager());
mRecyclerView.setHasFixedSize(true);
showDashboard();
}
private void showDashboard() {
if(mDataList!=null )mDataList.clear();
cShowProgress.showProgress(getActivity());
StringRequest stringRequest = new StringRequest(Request.Method.POST, DASHBOARD,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray = jsonObject.getJSONArray("walkinlist");
for(int i=0; i<jsonArray.length(); i++){
JSONObject obj = jsonArray.getJSONObject(i);
TimeLineModel model = new TimeLineModel();
model.setCustname(obj.getString("customername"));
model.setTime(obj.getString("serviceDuration"));
model.setServname(obj.getString("service"));
mDataList.add(model);
mTimeLineAdapter = new TimeLineAdapter(getActivity(), R.layout.item_row_dashboard, mDataList);
mRecyclerView.setAdapter(mTimeLineAdapter);
}
mTimeLineAdapter.notifyDataSetChanged();
} catch (JSONException e) {
// JSON error
e.printStackTrace();
Toast.makeText(getActivity(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getActivity(), "VolleyError" + error.toString(), Toast.LENGTH_LONG).show();
}
}) {
#Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("spaid", "145");
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
requestQueue.add(stringRequest);
}
private LinearLayoutManager getLinearLayoutManager() {
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
return linearLayoutManager;
}
}
// Adapter class
public class TimeLineAdapter extends RecyclerView.Adapter<TimeLineViewHolder> {
private List<TimeLineModel> mFeedList;
int resource;
private Context mContext;
public TimeLineAdapter(Context mContext, int resource, List<TimeLineModel> feedList) {
this.resource = resource;
this.mContext = mContext;
mFeedList = feedList;
}
#Override
public TimeLineViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = View.inflate(parent.getContext(), resource, null);
return new TimeLineViewHolder(view, viewType);
}
#Override
public void onBindViewHolder(TimeLineViewHolder holder, int position) {
TimeLineModel timeLineModel = mFeedList.get(position);
holder.servicename.setText(timeLineModel.getServname());
holder.custname.setText(timeLineModel.getCustname());
holder.time.setText(timeLineModel.getTime());
}
#Override
public int getItemCount() {
return (mFeedList!=null? mFeedList.size():0);
}
}
// ViewHolder class
public class TimeLineViewHolder extends RecyclerView.ViewHolder {
public TextView servicename, custname, time;
public TimeLineViewHolder(View itemView, int viewType) {
super(itemView);
servicename = (TextView) itemView.findViewById(R.id.tv_service);
custname = (TextView) itemView.findViewById(R.id.tv_cust);
time = (TextView) itemView.findViewById(R.id.tv_time);
}
}
You can use the getItemViewType().
Make your adapter(here TimeLineAdapter) extend RecyclerView.Adapter only.
You can change the Adapter's constructer to recieve ItemType and use it in the getItemViewType
Override the int getItemViewType (int position) method in your adapter.
In the onCreateViewHolder you can differentiate between which layout you want to inflate by using the viewType parameter.
In onBindViewHolder use instanceof to check which ViewHolder was created and call the related bind functions
A Sample Code-
// Adapter class
public class TimeLineAdapter extends RecyclerView.Adapter{
.....
int type;
public TimeLineAdapter(Context mContext, int resource, List<TimeLineModel> feedList,int layoutType) {
.....
.....
type=layoutType;
}
#Override
public int getItemViewType(int position) {
return type;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if(viewType==1){
View view = View.inflate(parent.getContext(), resource, null);
return new TimeLineViewHolder(view, viewType);
}else{
View view = View.inflate(parent.getContext(), resource, null);
return new TimeLineViewHolder2(view, viewType);
}
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if(holder instanceof TimeLineViewHolder){
TimeLineModel timeLineModel = mFeedList.get(position);
((TimeLineViewHolder) holder).servicename.setText(timeLineModel.getServname());
......
}
else if(holder instanceof TimeLineViewHolder2){
....
....
}
}
#Override
public int getItemCount() {
return (mFeedList!=null? mFeedList.size():0);
}
}