Error in initialization of custom array adapter - android

I am trying to make a custom listview. The list is declared as below
List<DocRow> doctors = new ArrayList<>();
This list is then being populated.
My custom array adapter is in a separate class with its constructor declared as below.
public class DocAdapter extends ArrayAdapter<DocRow>{
Context context;
int resource;
ArrayList<DocRow> doctors;
private LayoutInflater inflater;
public DocAdapter(#NonNull Context context, #LayoutRes int resource, ArrayList<DocRow> doctors) {
super(context, resource, doctors);
this.context = context;
this.resource = resource;
this.doctors = doctors;
inflater = LayoutInflater.from(context);
}
Now in my main activity, I am trying to create a new custom array adapter by passing off my list (which is a valid parameter), it isn't accepted. The code for creation and setting of adapter for linking the listview with the list is below.
DocAdapter adapter = new DocAdapter(getApplicationContext(), R.layout.doc_row, doctors);
docList.setAdapter(adapter);
Can anyone explain what is the issue? The link for error screenshot is above. I tried searching for this specific issue, but haven't been able to find a solution that works.

Change your constructor argument to List instead of ArrayList as you are passing list in it.
List<DocRow> doctors;
public DocAdapter(#NonNull Context context, #LayoutRes int resource, List<DocRow> doctors) {
super(context, resource, doctors);
this.context = context;
this.resource = resource;
this.doctors = doctors;
inflater = LayoutInflater.from(context);
}
As pointed by #Tim, here is a little detail about why this is needed.
When an instance is initialized, it may be initialized with one of its child classes but the object remains an instance of Super class only(Due to runtime polymorphism) and therefore the methods that consume this instance either expect super class or the instance should be casted to superclass before passing it on.
The easiest way to identify is to always look at the type on the left-hand side instead.
List a=new ArrayList();
In above example, the instance is actually an arraylist but it is of Type List.

A parent class's reference can store subclass's object, but the reverse is not true.
Here, in the constructor of your adapter, you have ArrayList<DocRow> as your parameter type, but your doctors list is of type List<DocRow>. You, you're passing a List<> object to an ArrayList<> reference.
To solve it, either change your doctors variable type to ArrayList<>, or your constructor parameter type to List<>

Related

How to access variable in a Activity, from childView?

I have a view as following hierarchy.
The main Activity has
ArraylistArrayListArraylist()
I want to access and change the data of the Arraylist from the button of the Card view. I'm using Custom ArrayAdapter. So is there are any way to do this?
You just need to pass your list data from activity to your custom adapter by calling adapter's constructor. See below how you can pass list data and that from adapter.
public TestAdapter extends ArrayAdapter<String>{
private Context mContext;
private List<String> list = new ArrayList<>();
public MovieAdapter(#NonNull Context context, #LayoutRes ArrayList<String> list) {
super(context, 0 , list);
this.mContext = context;
this.list = list;
}
.............//use "list" in your adapter
}
In activity you have below list.
Arraylist a = new Arraylist();
a.add("test");
a.add("test1");
a.add("test2");
a.add("test3");
TestAdapter testadapter=new TestAdapter(this,a);
Now you have that list in adapter and you can use list in your adapter.

How to use Picasso in an Adapter without using context in Fragment

When I try to use context in my fragment I get the error:
constructor Adapter in class Adapter cannot be applied to given types;
required:Context,List<ListItem>,OnItemClickListener
I have declared my context in my adapter as follows:
private Context mContext;
then i initialized the context:
public MyAdapter(Context context,List<ListItem> listItems, OnItemClickListener callback) {
this.listItems = listItems;
this.callback = callback;
this.mContext = context;
}
And used mContext to get my imageurl in the onBindViewHolder using picasso
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
ListItem listItem = listItems.get(position);
Picasso.with(mContext).load(listItem.getImageurl()).into(holder.imageUrl;
}
But now I can't seem to get around using this context in my Fragment.
This is what I've tried: In my Fragment:
//an error occurs
adapter = new MyAdapter(this,listItems);
so I tried this:
//still get an error
adapter = new MyAdapter(getContext(),this);
I also tried getActivity but still get an error
adapter = new MyAdapter(getActivity());
where am I going wrong?
All I am really trying to do is display the image in my listfragment but I don't know how to use Picasso with using context, and MyAdapter does not require context to function properly. I've been using it without declaring context and the data displays properly. Onclick is also working and displaying strings from firebase, but now I need to display images from Firebase using Picasso into my listfragment. Everything else works fine except this line of code in my Fragment:
adapter = new MyAdapter(getActivity());
You try Get context from any View object in Holder.
Example:
mContext = holder.imageView.getContext()
In your adapter initialization you pass 2 parameters, but your constructor requires 3 parameters.
so try to initialize using 3 params:
adapter=new MyAdapter(getContext(), listItems, this);
getContext()= context of fragment.
lisItems= your list.
this=is your click interface listener (make sure you implemented the listener in your fragment).
Try this, I think you forgot last argument
OnItemClickListener listener = OnItemClickListener {
void onItemClick(int position) {
//some code
}
}
adapter = new MyAdapter(this, listItems, listener);
Fragments are inflated inside an Activity.
In Fragment, you either use the context of the Activity or the context of the whole application.
Plus you have missed passing one more parameter in the Adapter, ie your click listener.
Define the adapter like this -
OnItemClickListener mOnItemClickListener = OnItemClickListener {
void onItemClick(int position) {
}
}
adapter = new MyAdapter(getActivity(), listItems, mOnItemClickListener);
or
adapter = new MyAdapter(getActivity().getApplicationContext(), listItems, mOnItemClickListener);

"ArrayAdapter requires the resource ID to be a TextView"

I basically know where this exception comes from and what causes it. It's because my textview is wrapped in an RelativeLayout.
But can you tell me why this works
playerAdapter = new PlayerAdapter(this,R.layout.item_lv_player,playerList);
lvPlayer.setAdapter(playerAdapter);
While this doesn't
PlayerAdapter playerAdapter1 = new PlayerAdapter(this,R.layout.item_lv_player,playerAdapterSource[0]);
spinnerPlayer1.setAdapter(playerAdapter1);
Shouldn't both of this throw an exception?
PlayerAdapater is a class that extends ArrayAdapter.
Is it because in the first example I'm using a ListView, and in the second one it's a Spinner? That wouldn't sound logical to me at all since the problem is the creation of the Adapter.
exception is not depends upon your adapter,its depends upon widgets.Every widgets has some properties,based on that only it will raise a exception,
Listview always depends upon Textview for all positions,but spinner not depending on that.If you run ListView without R.id.TextView it raise a nullpointer exception because of you have not initialized the textview in the adapter.But spinner doesnot because it not depending on the textview.
You have ArrayAdapter with 3 parameters passed to it.
The 3 parameter ArrayAdapter constructor can take any of the following parameters -
1) ArrayAdapter(Context context, int resource, int textViewResourceId)
2) ArrayAdapter(Context context, int resource, T[] objects)
The first one Explanation -
public ArrayAdapter (Context context, int resource, int
textViewResourceId)
Added in API level 1 Constructor
Parameters
context The current context.
resource The resource ID for a layout file containing a layout to
use when instantiating views.
textViewResourceId The id of the TextView within the layout
resource to be populated
The second one Explanation -
public ArrayAdapter (Context context, int resource, T[] objects)
Added in API level 1 Constructor
Parameters
context The current context.
resource The resource ID for a layout file containing a TextView to use when instantiating views.
objects The objects to represent in the ListView.
Having explained that for -
PlayerAdapter playerAdapter1 = new PlayerAdapter(this,
R.layout.item_lv_player,playerAdapterSource[0]);
spinnerPlayer1.setAdapter(playerAdapter1);
Is the last parameter a textViewResourceId ? > NO
Is the last parameter a ListView ? > NO
From developer docs android ArrayAdapter.

How to inherit from ArrayAdapter?

So I found some code that I am trying to get working. I am new to Android/Exclipse/Java and still trying to wrap my head around the whole thing. However, I am starring myself blind.
Say I have this:
private class RSSListAdapter extends ArrayAdapter<MyRSSItem> {
private List<MyRSSItem> objects = null;
public RSSListAdapter(Context context, int viewid, List<MyRSSItem> objects) {
super(context, viewid, objects);
this.objects = objects;
}
}
and then elsewhere this:
myRssAdapter = new RSSListAdapter(thisActivityContext, newsListView, myItemsDataArrayList);
where myItemsDataArrayList is declared like this:
ArrayList<MyRSSItem> myItemsDataArrayList = new ArrayList<MyRSSItem>();
I get this error:
The constructor News.RSSListAdaptor(Context, ListView, ArrayList) is undefined
Of course I have tried Google, but, well, no luck.
The constructor's definition is this:
public RSSListAdapter(Context context, int viewid, List<MyRSSItem> objects) {
This means it accepts in only a Context, an int, and a List <MyRSSItem>
However, you call the constructor with these arguments:
new RSSListAdapter(thisActivityContext, newsListView, myItemsDataArrayList);
the second argument is a newsListView, which isn't an int, it is a ListView. They are not the same type, hence the compile-time error.
If you change to android.R.id.text1, it should work, since that is a valid TextView id, like the superclass constructor requires.
And definitely don't forget to set the ListView's adapter to your RSSListAdapter after creating it.
For more detailed info, this tutorial is pretty helpful.

Regarding Activity.getApplicationContext()

I have created a custom adapter for a list view. In the constructor of the adapter i do the following:
public CustomListAdapter(Context context, int textViewResourceId,
ArrayList<CatalogueItemListData> items,int width) {
super(context, textViewResourceId, items);
this.context =context;
mInflater = LayoutInflater.from(context);
}
In my activity class i call the adaper as below:
this.m_adapter = new CustomListAdapter(this,
android.R.layout.simple_list_item_1, m_orders,displayMetrics.widthPixels);
listView.setAdapter(this.m_adapter);
and i get the output as in the image below:
Now when i change the constructor of my adapter as below:
public CustomListAdapter(Activity activity, int textViewResourceId,
ArrayList<CatalogueItemListData> items,int width) {
super(activity.getApplicationContext(), textViewResourceId, items);
this.context =activity.getApplicationContext();
mInflater = LayoutInflater.from(context);
}
I get the following output:
Could someone kindly tell me what could be the possible reason for the difference in the outputs? Thanks in advance.
I would say that you have applied a different theme to your specific activity then the entire application. This could cause the difference in styles based on which context is used. I am not sure if this is what is happening but it would make sense.
Your application context will use previously defined theme (or default theme) where was your activity context will not unless you call it explicitly.
Hope this helps!

Categories

Resources