What does inflator.context() mean in this line of code?
ArrayAdapter<DataType> listAdapter = new ArrayAdapter<DataType>(
inflator.getContext(), android.R.layout.simple_list_item_1, array);
It basically provides the context of the system inflator which inflates the given list item layout.
You can also use getActivity() if it's inside a fragment and getContex() or 'this' if it's inside an activity.
Related
I am curious to know this as I am not able to find it in internet.
I am creating a custom list adapter. In this adapter there is a getView method which I am overriding. In this method I inflate the row layout which I pick from layout folder.
In this case I should not need to pass the same row layout as a constructor to the adapter, which is not allowed.
Please answer.
Regards
Utsav.
you can safely ignore it by overriding the constructor, like this:
public class MyAdapter extends ArrayAdapter {
public MyAdapter(Context context){
super(context, 0);
}
}
edit:
That exists for cases that you're not creating a custom adapter, e.g. new ArrayAdapter(context, R.layout.item);
This adapter would write the value of to string() of its data items to TextView with ID android.R.id.text1 inside that layout.
I'm trying to create listview with custom adapter, everything fine in activity class.
but fragment extended class i can not set list in my custom adapter.
you should pass a Context object as first argument of your CustomListAdaptor's constructor but you are passing a Fragment objecy instead.
try this :
CustomListAdaptor customListAdaptor = new CustomListAdaptor(getActivity(), list);
Your problem is in this line
CustomListAdapter customAdapter = new CustomListAdapter(this, list);
More specifically the keyword this is your problem. You are trying to pass this as Context, which works in an Activity, because Activities have Context. However, Fragments do not.
So you must call the parent Activity's Context like so:
CustomListAdapter customAdapter = new CustomListAdapter(getActivity(), list);
In my OncreateView() set the adapter which is working when i am first loading the page. When i go to another page and make changes then come back to this fragment it is not working adapter.notifyDatasetchanged().
#Override
public void onStart() {
super.onStart();
groupItem.clear();
childItem.clear();
List<String> child_Category;child_Category=new ArrayList<String>();
groupItem = obj_Listdatabase.fetchcategory();
childItem.clear();
ListIterator<String> iterator = groupItem
.listIterator();
while (iterator.hasNext()) {
String categoryname = iterator.next();
child_Category = new ArrayList<String>();
child_Category = obj_Listdatabase
.fetchchildlist(categoryname);
childItem.add(child_Category);
}
adapter.notifyDataSetChanged();
}
Hai I got output for this one:
groupItem.addAll(obj_Listdatabase.fetchcategory());
Instead of This
//groupItem = obj_Listdatabase.fetchcategory();
Because i change the reference in assignment statement(=) for that adapter.So i use addAll() method to store the values only instead of reference.
You should reaaaally indent your code more cleanly, never use several ";" on the same line for example.
And you can definitely use BaseExpandableListAdapter the issu is somewhere else..
You should also notice that onCreate view is more suitable for "creating view" so all the adapter stuff should be somewhere else like onViewCreated, or onActivityCreated as you need (it's just some advices)
I assume your adapter is in a Fragment since you use GetActivity() and in the constructor of your adapter you pass a reference to the activity (context), the group list and the child list.. ok
we ll assume that you are using the fragment onStart. From the official doc :
Called when the Fragment is visible to the user. This is generally tied to Activity.onStart of the containing Activity's lifecycle.
So normally this method is called after onViewCreate, onViewCreated .. etc at least the first time you code is running. So this is ok
Did you try using adapter.notifyDataSetInvalidate() and then do adapter.notidyDataSetChanged ?
One last thing, since you actually pass the data to your adapter by the constructor, when you update your lists how can the adapter be aware of the changes ? Are your lists global (static) ?
If not, before doing adapter.notifyDataSetChanged() you should pass the lists (group and child) to the adapter with some setter..
good luck
I have created a listview that sits inside a larger activity (the list view fills half the screen, with inputs/buttons above it). This listview uses a custom adapter to draw the row views.
Inside the row view is a button, which when clicked/tapped i want the activity to handle, and not the adapter. However i don't know how inside the adapter class, i can tell it to use the activity. Since it's OOP, im assuming i have to pass some sort of reference to the activity when setting up the adapter (rather than hardcoding the parent activity into the adapter).
I had a simlier issue with sharing a dataset between the activity and adapter (i wanted the adapter to use an arraylist from the activity), however i couldn't solve that one either so i ended up passing the arraylist through as a duplicate into the adapter. So i would hope working out the click listener, will also mean i can get rid of that duplicate data having to be created?
All the code is pretty simple, but here's a rough outline:
Setting the list adapter & declaring dataset (this is the activity dataset, not the adapter)
numbersListAdapter = new NumberListAdapter(this);
numbersList.setAdapter(numbersListAdapter);
this.selectedContacts = new HashMap<Long, HashMap<String, String>>();
Adding entry into activity data set and adding to the adapter dataset
HashMap<String, String> tempa = new HashMap<String,String>();
tempa.put("name", name);
tempa.put("number", number);
this.selectedContacts.put(contactID, tempa);
this.numbersListAdapter.addEntry(contactID, tempa);
The adapters add entry
public void addEntry(Long id, HashMap<String, String> entry) {
entry.put("contactID", id.toString());
this.selectedNumbers.add(entry);
this.notifyDataSetChanged();
}
The adapters constructor
public NumberListAdapter(Context context) {
selectedNumbers = new ArrayList<HashMap<String, String>>();
mInflater = LayoutInflater.from(context);
}
Please note: this is my first attempt at this stuff. I've never done android, java and very, very little OO programming. So i already know the code is most likely inefficient and quite terrible. But i've got to learn somehow :)
EDIT
Ok so i realised ive been a little silly and i just needed to use the context passed into the adapter to reference the parent activity. However i still ain't getting it. Heres the code:
The constructor for the adapter
numbersListAdapter = new NumberListAdapter(this);
The variable declaration and constructor method
public class NumberListAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private ArrayList<HashMap<String, String>> selectedNumbers;
private Context parentActivity;
public NumberListAdapter(Context context) {
parentActivity = (Context) context;
selectedNumbers = new ArrayList<HashMap<String, String>>();
mInflater = LayoutInflater.from(context);
}
The listener
Button theBtn = (Button)rowView.findViewById(R.id.actionRowBtn);
theBtn.setOnClickListener(this.parentActivity);
I get two messages from eclipse, the first occurs when i comment out the listener and i get
The value of the field NumberListAdapter.parentActivity is not used
Once i add the listener in i get
The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (Context)
Obviously im doing something wrong. Probably quite a silly mistake again too
If you need to get callback when row of the listview is clicked you can use
numbersListAdapter.setOnitemitemClickLiistener
But if you need a button inside each row to be clicked you will have to override the
getView function of your adapter and then set the onclicklistener of the button individually
Edit:
Typecast the Context to Activity
theBtn.setOnClickListener((YourActivity)this.parentActivity);
I know you can call notifyDataSetChanged(); How would you do that in this context -- is it even possible?
ListView listView = getListView();
listView.setTextFilterEnabled(true);
setListAdapter(new ArrayAdapter<String>(Items.this, R.layout.list, items));
Edit: I have changed to one of the suggestions below. Here is what I am doing:
protected void onPostExecute(Void v) {
adapter = new ArrayAdapter<String>(Items.this, R.layout.list, items);
setListAdapter(adapter);
adapter.notifyDataSetChanged();
}
Here I am refreshing inside an AsyncTask method. This is being called inside a Dialog with a Submit button on top of a ListLiew. The Dialog inserts new data into a database and I want the list refreshed where to reflect this. I have tried this code above AND inside the onClick for the Button in the Dialog. Right before dialog dismissed.
You need to initialize the ArrayAdapter by itself:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(Items.this, R.layout.list, items);
setListAdapter(adapter);
...then call notifyDataSetChanged() on that:
adapter.notifyDataSetChanged();
Note that you only need to call this method if the data actually changes; given this, you probably need to have it stored as a member variable in the class so it can be accessed later.
You should just be able to call notifyDataSetChanged() on your ArrayAdapter, since ArrayAdapter extends BaseAdapter
http://developer.android.com/reference/android/widget/ArrayAdapter.html
In your example you'd do something like this, taking that you meant to call listView.setListAdapter()
(ArrayAdapter)listView.getAdapter()).notifyDataSetChanged();
In your example, you shouldn't be setting the adapter in your AsyncTask. Generally it is a good idea to do that in onCreate() because otherwise you are setting it each time AsyncTask is called. If you make your adapter a member variable, then you should be able to call <adapter>.notifyDataSetChanged() from within the AsyncTask.