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.
Related
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 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.
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.
hi I created one simple xml based spinner application.i got all value in my xml file using sax parser. I have two spinner and one grid view, the 1st spinner display one array list value 2nd spinner display one array list. The same time grid view display some images in bottom. this process working fine. now i wish to create text with images spinner so i used row.xml file in my resource folder and i change my code also but i am getting error. what mistake i made in my code.....
error line:
label.setText(hltag_List[position]);
If it is a Arraylist , use hltag_List.get(position) .
label.setText(hltag_List.get(position));
please see you are using arraylist in constructor
public MyAdapter(Context context, int textViewResourceId, ArrayList<String> hltagList) {
super(context, textViewResourceId, hltagList);
}
whereas accessing its elements via array. just change your line to
label.setText(hltag_List.get(position));
Change your adapter code to following if, you have an array in datasource:
public MyAdapter(Context context, int textViewResourceId, String[] hltagList) {
super(context, textViewResourceId, hltagList);
}
and keep the line:
label.setText(hltag_List[position]);
I think you should use ArrayList.get(int index) to obtain the i-th object in the list.
I am looking for any way by which I can place android listView with TextView and another horizontal listView corresponding to that listView.
Actually I'm having a List of items and shops corresponding to that item I have sub categories.
And also a shop can have multiple sub categories like it,s different sub categories(child).
The situation to display is just like,
................................................
Eat(TEXT VIew)
Image Image Image (multiple categories in that item) .....
Name Name Name (multiple categories in that item)
............................................................
Use an ArrayAdaptor : look custom ListView on google : (first result) http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/
Basically you need to design the layout of your sublist in XML, then design the layout of your main ListView in XML.
I think you whould use wrap_content for your sublist, width : fill_parent for the main list and height : wrap_content for the main list.
Then you need two classes to hold your required data of each item (one for the main list containing one of your sublist).
Then you create ArrayList and ArrayList (stored in each MyMainItem).
Then you'll need to create two class extenting ArrayAdaptor :
MainAdapter extends ArrayAdaptor and SecondaryAdapter
In each of your ArrayAdapter you'll need to store the list of items and override the constructor like so :
private ArrayList<MyObj> items = new ArrayList<MyObj>();
public ArticlesAdapter(Context context, int textViewResourceId,
ArrayList<MyObj> items) {
super(context, textViewResourceId, items);
this.items = items;
}
In each adaptor, you'll have to override the method
#Override
public View getView(int position, View convertView, ViewGroup parent)
like shown in the tutorial
But don't forget in your MainAdapter to call the setAdaptor() on the subListView.
Be careful with the getView method : you are highly to receive a null object, so make sure to test it to avoid NullPointerException
Hope I helped you. This is worth a Bounty :-)
I think you can set in xml!
enter code here
android:divider="#null"
android:dividerheight="10dp"<-- space between items-->