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.
Related
New to Android, I got a simple application with spinners and associated ArrayAdapters working: when things get selected, I seem to be able and trigger some calculations. I am then saving the current selected item.
At some point, I retrieve the saved value, and want to position the spinner at that value: basically setPosition() the spinner to that object.
I have found lots of tutorials with the same format I have: use the getPosition() on the ArrayAdapter, and pass in the object you are looking for... Trouble is, it keeps returning -1 (not found).
Debugging, I have verified that the object I pass is not null, and so is the ArrayAdapter, and also the ArrayAdapter getCount returns me the items it should have (so it's not empty).
I'm at loss. Appreciate any... pointers? :-)
/* ArrayAdapter class looks like this*/
public class MyAdapter extends ArrayAdapter<MyClass> {
// Constructor
MyAdapter(#NonNull Context context, int resource, #NonNull List<MyClass> objects) {
super(context, resource, objects);
}
}
/* Fragment looks like this*/
final MyAdapter mAdapter = new MyAdapter(
requireActivity(),
android.R.layout.simple_spinner_item,
objects);
Spinner mySpinner = fragment_view.findViewById(R.id.my_spinner);
mySpinner.setAdapter(mAdapter);
// assume I have one "object" of MyClass,
// and want to search for it in "MyAdapter"
int spinnerPosition = mAdapter.getPosition(objectToBeFound); // returns -1
mySpinner.setSelection(spinnerPosition);
Adapter internally works with List
public int getPosition(#Nullable T item) {
return mObjects.indexOf(item);
}
so getPosition internally depends upon List#indexOf(T) and which relies on equals method
(o==null ? get(i)==null : o.equals(get(i)))
so you are getting -1 because you haven't implemented equals and hashcode method properly in MyClass so implement both methods and you will be able to get the precise index.
Referene:
Use Auto Generate
difference between equals() and hashCode()
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<>
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.
I have an AutoCompleteTextView inside a RelativeLayout inside a FrameLayout. I want to populate the completion list using a class declared as follows:
public class AutoCompleteAdapter extends ArrayAdapter<String> implements Filterable {
...
public AutoCompleteAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
data = new ArrayList<String>();
}
...
}
I attached the adapter as follows:
AutoCompleteTextView tv = (AutoCompleteTextView) v.findViewById(R.id.editTextClient);
AutoCompleteAdapter adapter = new AutoCompleteAdapter(getActivity(), R.layout.fragment_main_right);
tv.setAdapter(adapter);
where R.layout.fragment_main_right is the enclosing FrameLayout mentioned above. When I start to input text, I get a ClassCastException with the following message: "android.widget.FrameLayout cannot be cast to android.widget.TextView". I understand this to mean that the second parameter in the ArrayAdapter constructor should be the id of something derived from a TextView. All the examples show this parameter as being the enclosing layout. Can someone clear up my confusion?
For the second parameter in your adapter constructor, pass in android.R.layout.simple_dropdown_item_1line or android.R.layout.simple_list_item_1 instead.
The hint is given away that it expects a textview by the name of the parameter: textViewResoureceId.
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!