Can anyone please suggest binding for TextView inside ViewPager's fragment.
Please have look into below code for investigation.
ViewPager's fragment resource file (about_fragment.axml)
Here i'm trying to bind AboutDetail with ViewModel.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#android:color/white">
<TextView
android:id="#+id/idAboutDetail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="32.5dp"
android:layout_marginLeft="25dp"
android:layout_marginRight="8dp"
android:textColor="#android:color/black"
android:textSize="17sp"
local:MvxBind="Text AboutDetail"/>
</LinearLayout>
ViewPager's fragment VM
DetailViewModel belong to DetailFragment which contains ViewPager.
[AddINotifyPropertyChangedInterface]
public class AboutDetailViewModel : MvxViewModel<DetailViewModel>
{
public override void Prepare(DetailViewModel parameter)
{
AboutDetail = "Yes loaded";
}
private string _aboutDetail;
public string AboutDetail
{
get
{
return _aboutDetail;
}
set
{
_aboutDetail = value;
RaisePropertyChanged(() => AboutDetail);
}
}
}
ViewPager's Fragment
public class UCAboutFragment :BaseFragment<AboutDetailViewModel>
{
protected override int FragmentId => Resource.Layout.fragment_details_about;
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = base.OnCreateView(inflater, container, savedInstanceState);
return view;
}
}
DetailFragment code which contains ViewPager.
DetailPagerAdapter pageAdapter = new DetailPagerAdapter(ChildFragmentManager);
ViewPager pager = thisView.FindViewById<ViewPager>(Resource.Id.viewpager);
pager.Adapter = pageAdapter;
TabLayout tabLayout = thisView.FindViewById<TabLayout>(Resource.Id.tabs);
tabLayout.SetupWithViewPager(pager);
DetailPagerAdapter
private class DetailPagerAdapter : FragmentPagerAdapter
{
private List<Android.Support.V4.App.Fragment> mFragmentList;
private List<string> mFragmentTitleList = new List<string>();
public SectionsPagerAdapter(Android.Support.V4.App.FragmentManager fm) : base(fm)
{
this.mFragmentList = new List<Android.Support.V4.App.Fragment>();
addFragment(new UCAboutFragment(), "About");
addFragment(new UCServicesFragment(), "Services");
addFragment(new UCInsuranceFragment(), "Insurance");
}
#region implemented abstract members of PagerAdapter
public override int Count
{
get
{
return mFragmentList.Count;
}
}
#endregion
#region implemented abstract members of FragmentPagerAdapter
public override Android.Support.V4.App.Fragment GetItem(int position)
{
return mFragmentList[position];
}
#endregion
public override ICharSequence GetPageTitleFormatted(int position)
{
return new Java.Lang.String(mFragmentTitleList[position].ToLower());
}
private void addFragment(Android.Support.V4.App.Fragment fragment, string title)
{
mFragmentList.Add(fragment);
mFragmentTitleList.Add(title);
}
}
BaseFragment
{
public abstract class BaseFragment : MvxFragment
{
protected Toolbar Toolbar { get; private set; }
protected BaseFragment()
{
RetainInstance = true;
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var ignore = base.OnCreateView(inflater, container, savedInstanceState);
var view = this.BindingInflate(FragmentId, null);
return view;
}
protected abstract int FragmentId { get; }
}
public abstract class BaseFragment<TViewModel> : BaseFragment where TViewModel : class, IMvxViewModel
{
public new TViewModel ViewModel
{
get { return (TViewModel)base.ViewModel; }
set { base.ViewModel = value; }
}
}
}
In the Fragments you have in your ViewPager, you need to use BindingInflate to inflate your View:
public class UCAboutFragment :BaseFragment<AboutDetailViewModel>
{
protected override int FragmentId => Resource.Layout.fragment_details_about;
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var _ = base.OnCreateView(inflater, container, savedInstanceState);
var view = this.BindingInflate(FragmentId, container, false);
return view;
}
}
BindingInflate is what discovers the binding expressions in your layout and connects the ViewModel with the View iteself.
Related
I try to do recyclerView with retrofit2, but I do in my code: recyclerView Adapter Constructor and I get a error in my MainActivity part of this line -
"(flowersList, this)": I get error: List anonymous retrofit2.Callback
RecyclerViewFlowersAdapter recyclerViewAdapter = new RecyclerViewFlowersAdapter(flowersList, this);
my code my MainActivity is:
try {
APIService service = ApiClient.getRetrofit().create(APIService.class);
retrofit2.Call<List<Flower>> call = service.getFlowerData();
call.enqueue(new Callback<List<Flower>>() {
#Override
public void onResponse(retrofit2.Call<List<Flower>> call, Response<List<Flower>> response) {
List<Flower> flowersList = response.body();
mLinearLayoutManager = new LinearLayoutManager(MainActivity.this);
mRecyclerView = findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(mLinearLayoutManager);
RecyclerViewFlowersAdapter recyclerViewAdapter = new RecyclerViewFlowersAdapter(flowersList, this);
mRecyclerView.setAdapter(recyclerViewAdapter);
}
and the code in RecyclerViewFlowersAdapter is:
public class RecyclerViewFlowersAdapter extends RecyclerView.Adapter<RecyclerViewHolder> {
private List<Flower> mFlowers;
private Context mContext;
public RecyclerViewFlowersAdapter(List<Flower> flowers, Context context) {
mContext = context;
mFlowers = flowers;
}
#Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.flower_item_card, null);
RecyclerViewHolder recyclerViewHolder = new RecyclerViewHolder(view);
return recyclerViewHolder;
}
#Override
public void onBindViewHolder(RecyclerViewHolder holder, int position) {
holder.mTextViewTitle.setText(mFlowers.get(position).getName());
Picasso.with(mContext)
.load(mFlowers.get(position).getPhoto()).into(holder.mImageViewFlower);
}
#Override
public int getItemCount() {
return mFlowers.size();
}
}
and my code in RecyclerViewHolder is:
public class RecyclerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView mTextViewTitle;
public ImageView mImageViewFlower;
public RecyclerViewHolder(View itemView){
super(itemView);
itemView.setOnClickListener(this);
mTextViewTitle = itemView.findViewById(R.id.title);
mImageViewFlower = itemView.findViewById(R.id.imageViewFlower);
}
#Override
public void onClick(View v) {
}
}
I try to do alot of thing but is still error.
thanks for help :)
You are probably using a wrong context, try using MainActivity.this instead of this. Change this
RecyclerViewFlowersAdapter recyclerViewAdapter = new RecyclerViewFlowersAdapter(flowersList, this);
to this
RecyclerViewFlowersAdapter recyclerViewAdapter = new RecyclerViewFlowersAdapter(flowersList, MainActivity.this);
I have a recyclerView and customAdaprer. I pass an list of an object (earthquakeList)to recycleradapter and then i do setAdapter:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
earthquakeList = new ArrayList<>();
adapter = new RecyclerViewAdapter(earthquakeList);
recyclerView.setAdapter(adapter);
}
I create AsyncTask on onResume method:
#Override
protected void onResume() {
super.onResume();
// Kick off an {#link AsyncTask} to perform the network request
new EarthQuakeAsyncTask().execute();
}
in AsyncTask my class i get a new List from my object that i get from internet and when I replaced this new list with older list and call notifyDataSetChanged, recyclerView still nothing show??
I debug my app and I get object from net.
I do in this way on list view but recylerview seems efferent.
I replace old list with the new list one like blow:
private class EarthQuakeAsyncTask extends AsyncTask<Void, Void, List<Earthquake>> {
#Override
protected List<Earthquake> doInBackground(Void... urls) {
// Create URL object
URL url = HttpRequest.createUrl(USGS_REQUEST_URL);
// perform HTTP request to the URL and receive a JSON response back
String jsonResponse = "";
try {
jsonResponse = HttpRequest.makeHttpRequest(url);
} catch (IOException e) {
e.printStackTrace();
}
List<Earthquake> earthquakes = HttpRequest.extractFeaturesFromJson(jsonResponse);
return earthquakes;
}
#Override
protected void onPostExecute(List<Earthquake> earthquakeList) {
super.onPostExecute(earthquakeList);
MainActivity.this.earthquakeList.clear();
MainActivity.this.earthquakeList.addAll(earthquakeList);
adapter.notifyDataSetChanged();
}
what is exactly my wrong?
************************ EDIT ********************
this is Adapter :
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyHolder> {
List<Earthquake> earthquakes;
public RecyclerViewAdapter(List<Earthquake> earthquakes) {
this.earthquakes = earthquakes;
}
#Override
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(
R.layout.earthquak_list_item, parent, false);
MyHolder myHolder = new MyHolder(view);
return myHolder;
}
#Override
public void onBindViewHolder(MyHolder holder, int position) {
Earthquake earthquake = earthquakes.get(position);
holder.magnitude.setText(earthquake.getmMagnitude());
holder.location.setText(earthquake.getmLocation());
holder.time.setText(earthquake.getmDate());
}
#Override
public int getItemCount() {
return (null != earthquakes ? earthquakes.size() : 0);
}
public void setItems(List<Earthquake> earthquakeList) {
this.earthquakes = earthquakeList;
}
public class MyHolder extends RecyclerView.ViewHolder {
#BindView(R.id.magnitude)
TextView magnitude;
#BindView(R.id.location)
TextView location;
#BindView(R.id.date)
TextView time;
public MyHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}
Ops ,I forgot put LayoutManager for recyclerView .
this is right code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
earthquakeList = new ArrayList<>();
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
adapter = new RecyclerViewAdapter(earthquakeList);
recyclerView.setAdapter(adapter);
}
I'm trying to use a recyclerView into a fragment used into a sliding tab and I'm facing with this issue, normally is a problem about the LayoutManager but in my code there is all needed according to the other answer on stackoverflow.
The crash is caused by this ecception:
java.lang.NullPointerException at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:1764)
but I really can't understand where is the problem since I use the LinearLayoutManager
tab_scheda.java
public class Tab_scheda extends Fragment {
private RecyclerView recyclerView;
private CustomViewAdapter adapter;
public Tab_scheda() {
// Required empty public constructor
}
public static List<scheda> getdata() {
List<scheda> data = new ArrayList<>();
int[] icons = {R.drawable.ic_launcher,R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher };
String[] titles = {"row1", "row2", "row3", "row4" };
for (int i = 0; i < titles.length; i++) {
scheda current = new scheda();
current.id = icons[i];
current.name = titles[i];
}
return data;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.tab_scheda, container, false);
recyclerView = (RecyclerView) v.findViewById(R.id.drawerList_sch);
adapter = new CustomViewAdapter(getActivity(), getdata());
recyclerView.setAdapter(adapter);
LinearLayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(mLayoutManager);
return v;
}
}
tab_scheda.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id = "#+id/drawerList_sch"
android:layout_height = "wrap_content"
android:layout_width="match_parent">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
CustomViewAdapter.java
public class CustomViewAdapter extends RecyclerView.Adapter<CustomViewAdapter.MyViewHolder> {
private LayoutInflater inflater;
List<scheda> data = Collections.emptyList();
public CustomViewAdapter(Context context, List<scheda> data){
inflater = LayoutInflater.from(context);
this.data = data;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view=inflater.inflate(R.layout.row_scheda, parent ,false);
MyViewHolder myViewHolder = new MyViewHolder(view);
return myViewHolder;
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
scheda current = data.get(position);
holder.image.setImageResource(current.id);
holder.text.setText(current.name);
}
#Override
public int getItemCount() {
return data.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
private TextView text;
private ImageView image;
public MyViewHolder(View itemView) {
super(itemView);
text = (TextView) itemView.findViewById(R.id.text_scheda);
image = (ImageView) itemView.findViewById(R.id.img_scheda);
}
}
}
row_scheda.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/img_scheda"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:src="#drawable/ic_launcher"
android:layout_gravity="center_vertical" />
<TextView
android:id="#+id/text_scheda"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:layout_gravity="center_vertical" />
</LinearLayout>
If somebody could help me to fix this issue will be really appriciated
I am trying to take data from web and by the help of volley and display it in Recylerview.
I can easily display data in toast but in case of recylerview I am having problem. I have follow Slidenerd from youtube:
https://www.youtube.com/playlist?list=PLonJJ3BVjZW6CtAMbJz1XD8ELUs1KXaTD
but it not displaying in Recyclerview, any ideas?
FragmentCollege.java class:
public class FragmentCollege extends Fragment {
public static final String URL_EDUSANJAL_COLLEGE = "http://edusanjal.com/api/edusanjal/college/?format=json";
private VolleySingleton volleySingleton;
private ImageLoader imageLoader;
private RequestQueue requestQueue;
private ArrayList<College> listCollege = new ArrayList<>();
private RecyclerView listCollegeHits;
private AdapterCollege mAdapterCollege;
public FragmentCollege() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
volleySingleton = VolleySingleton.getInstance();
requestQueue = volleySingleton.getRequestQueue();
}
private void sendJsonRequest() {
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET,
URL_EDUSANJAL_COLLEGE,
(String) null,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
listCollege = parseJSONResponse(response);
mAdapterCollege.setCollegeList(listCollege);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue.add(request);
}
private ArrayList<College> parseJSONResponse(JSONObject response) {
ArrayList<College> listCollege = new ArrayList<>();
if (response == null || response.length() == 0) {
try {
StringBuilder data = new StringBuilder();
JSONArray arrayCollege = response.getJSONArray(KEY_OBJECTS);
for (int i = 0; i < arrayCollege.length(); i++) {
JSONObject currentCollege = arrayCollege.getJSONObject(i);
Long id = currentCollege.getLong(KEY_ID);
String title = currentCollege.getString(KEY_TITLE);
String city = currentCollege.getString(KEY_CITY);
String phone = currentCollege.getString(KEY_PHONE);
data.append(id + "\n" + title + "\n" + city + "\t" + phone + "\n" + "\n");
College college = new College();
college.setId(id);
college.setTitle(title);
college.setCity(city);
college.setPhone(phone);
listCollege.add(college);
}
} catch (JSONException e) {
}
L.T(getActivity(), listCollege.toString());
}
return listCollege;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_college, container, false);
listCollegeHits = (RecyclerView) view.findViewById(R.id.listCollegeHits);
listCollegeHits.setLayoutManager(new LinearLayoutManager(getActivity()));
mAdapterCollege = new AdapterCollege(getActivity());
listCollegeHits.setAdapter(mAdapterCollege);
sendJsonRequest();
// Inflate the layout for this fragment
return view;
}
}
fragemnt_college.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.edusanjal.edusanjal.activity.CollegeFragment">
<!-- TODO: Update blank fragment layout -->
<android.support.v7.widget.RecyclerView
android:id="#+id/listCollegeHits"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="#+id/textVolleyError"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="#dimen/size_word"
android:text="#FF4444"
android:visibility="gone"/>
custom_college_layout.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="#dimen/size_byte"
android:layout_marginLeft="#dimen/size_word"
android:layout_marginRight="#dimen/size_word"
android:layout_marginTop="#dimen/size_byte">
<ImageView
android:id="#+id/collegeLogo"
android:layout_width="#dimen/college_thumbnail_width"
android:layout_height="#dimen/college_thumbnail_height"
android:layout_centerVertical="true"
android:src="#mipmap/ic_launcher"/>
<TextView
android:id="#+id/collegeTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/collegeLogo"
android:layout_marginLeft="56dp"
android:alpha="0.87"
android:gravity="right"
android:padding="#dimen/size_half_byte"
android:text="Aadikabi Bhanubhakta Higher Secondary School"
android:textSize="#dimen/size_text_primary"/>
<TextView
android:id="#+id/collegePhone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/collegeTitle"
android:layout_marginLeft="56dp"
android:alpha="0.87"
android:gravity="right"
android:padding="#dimen/size_half_byte"
android:text="Phone"
android:textSize="#dimen/size_text_primary"/>
<TextView
android:id="#+id/collegeCity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/collegePhone"
android:layout_marginLeft="56dp"
android:alpha="0.87"
android:gravity="right"
android:padding="#dimen/size_half_byte"
android:text="city"
android:textSize="#dimen/size_text_primary"/>
<TextView
android:id="#+id/collegeCreatedOn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#id/collegeCity"
android:alpha="0.87"
android:padding="#dimen/size_half_byte"
android:text="31-31-31"
android:textSize="#dimen/size_text_secondary"/>
AdapterCollege.java:
public class AdapterCollege extends RecyclerView.Adapter<AdapterCollege.ViewHolderCollege> {
private ArrayList<College> listCollege = new ArrayList<>();
private LayoutInflater mLayoutInflater;
private VolleySingleton volleySingleton;
private ImageLoader imageLoader;
private DateFormat dateFormatter=new SimpleDateFormat("yyyy-MM-dd");
public AdapterCollege(Context context) {
mLayoutInflater = mLayoutInflater.from(context);
volleySingleton = VolleySingleton.getInstance();
imageLoader = volleySingleton.getImageLoader();
}
public void setCollegeList(ArrayList<College> listCollege) {
this.listCollege = listCollege;
notifyItemRangeChanged(0, listCollege.size());
}
#Override
public ViewHolderCollege onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mLayoutInflater.inflate(R.layout.custom_college_layout, parent, false);
ViewHolderCollege viewHolder = new ViewHolderCollege(view);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolderCollege holder, int position) {
College currentCollege = listCollege.get(position);
holder.collegeTitle.setText(currentCollege.getTitle());
Date collegeCreatedOn=currentCollege.getCreatedOn();
if(collegeCreatedOn!=null){
String formattedDate=dateFormatter.format(collegeCreatedOn);
holder.collegeCreatedOn.setText(formattedDate);
}
else{
holder.collegeCreatedOn.setText(Constants.NA);
}
holder.collegeCity.setText(currentCollege.getCity());
holder.collegePhone.setText(currentCollege.getPhone());
String urlLogo = currentCollege.getUrlLogo();
loadImages(urlLogo, holder);
}
private void loadImages(String urlLogo, final ViewHolderCollege holder) {
if (!urlLogo.equals(Constants.NA)) {
imageLoader.get(urlLogo, new ImageLoader.ImageListener() {
#Override
public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
holder.collegeLogo.setImageBitmap(response.getBitmap());
}
#Override
public void onErrorResponse(VolleyError error) {
}
});
}
}
#Override
public int getItemCount() {
return listCollege.size();
}
static class ViewHolderCollege extends RecyclerView.ViewHolder {
ImageView collegeLogo;
TextView collegeTitle;
TextView collegeCreatedOn;
TextView collegePhone;
TextView collegeCity;
public ViewHolderCollege(View itemView) {
super(itemView);
collegeLogo = (ImageView) itemView.findViewById(R.id.collegeLogo);
collegeTitle = (TextView) itemView.findViewById(R.id.collegeTitle);
collegeCreatedOn = (TextView) itemView.findViewById(R.id.collegeCreatedOn);
collegePhone = (TextView) itemView.findViewById(R.id.collegePhone);
collegeCity = (TextView) itemView.findViewById(R.id.collegeCity);
}
}
pojo class => College.java:
public class College {
private long id;
private String title;
private Date createdOn;
private String phone;
private String city;
private String urlLogo;
public College(){
}
public College(long id,
String title,
Date createdOn,
String phone,
String city,
String urlLogo){
this.id=id;
this.title=title;
this.createdOn=createdOn;
this.phone=phone;
this.city=city;
this.urlLogo=urlLogo;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public Date getCreatedOn() {
return createdOn;
}
public void setCreatedOn(Date createdOn) {
this.createdOn = createdOn;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getUrlLogo() {
return urlLogo;
}
public void setUrlLogo(String urlLogo) {
this.urlLogo = urlLogo;
}
#Override
public String toString() {
return "ID: "+id+
"Title "+title+
"Date "+createdOn+
"Phone "+phone+
"City "+city+
"urlLogo "+urlLogo+
"\n";
}
}
network class => VolleySingleton.java:
public class VolleySingleton {
private static VolleySingleton sInstance = null;
private ImageLoader mImageLoader;
private RequestQueue mRequestQueue;
private VolleySingleton() {
mRequestQueue = Volley.newRequestQueue(MyApplication.getAppContext());
mImageLoader = new ImageLoader(mRequestQueue, new ImageLoader.ImageCache() {
private LruCache<String, Bitmap> cache = new LruCache<>((int) (Runtime.getRuntime().maxMemory() / 1024) / 8);
#Override
public Bitmap getBitmap(String url) {
return cache.get(url);
}
#Override
public void putBitmap(String url, Bitmap bitmap) {
cache.put(url, bitmap);
}
});
}
public static VolleySingleton getInstance() {
if (sInstance == null) {
sInstance = new VolleySingleton();
}
return sInstance;
}
public RequestQueue getRequestQueue() {
return mRequestQueue;
}
public ImageLoader getImageLoader(){
return mImageLoader;
}
I run this at the application Start Up
public class ConfigurationFacility : AbstractFacility {
private readonly List<string> configuredComponents = new List<string>();
protected override void Init() {
Kernel.ComponentRegistered += OnComponentRegistered;
// add environment configurators
}
private void OnComponentRegistered(string key, IHandler handler) {
// if the component is a configurator then run conf settings and add it to configuredComponents
}}
Question: How to hook tear down and to call explicit release for each ?
Thanks
You can use either the ComponentDestroyed event of IKernel or just implement IDisposable in your components. Here's a little sample code:
namespace WindsorInitConfig {
[TestFixture]
public class ConfigurationFacilityTests {
[Test]
public void tt() {
OneDisposableComponent component = null;
using (var container = new WindsorContainer()) {
container.AddFacility<ConfigurationFacility>();
container.AddComponent<OneDisposableComponent>();
component = container.Resolve<OneDisposableComponent>();
}
Assert.IsTrue(component.Disposed);
Assert.Contains(component, ConfigurationFacility.DestroyedComponents);
}
public class OneDisposableComponent : IDisposable {
public bool Disposed { get; private set; }
public void Dispose() {
Disposed = true;
}
}
public class ConfigurationFacility : AbstractFacility {
private readonly List<string> configuredComponents = new List<string>();
public static readonly ArrayList DestroyedComponents = new ArrayList();
protected override void Init() {
Kernel.ComponentRegistered += OnComponentRegistered;
Kernel.ComponentDestroyed += Kernel_ComponentDestroyed;
// add environment configurators
}
private void Kernel_ComponentDestroyed(ComponentModel model, object instance) {
DestroyedComponents.Add(instance);
// uninitialization, cleanup
}
private void OnComponentRegistered(string key, IHandler handler) {
// if the component is a configurator then run conf settings and add it to configuredComponents
configuredComponents.Add(key);}
}
}
}
The static ArrayList is only for demo purposes, of course.