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);
Related
I'm making an activity with a listView fragment in it. I am having a button click on the MainActivity load items for an rss feed that populate the listview fragment.
My question is, how should I go about setting it up? I already have the code for it.
My plan now is to have the button click on MainActivity point to my method in my RSSFragment which extends ListFragment. I'm confused about some things like which activity to pass here:
#Override
protected void onPostExecute(Exception s) {
super.onPostExecute(s);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, titles);
lvRss.setAdapter(adapter);
progressDialog.dismiss();
}
}
Should I use getActivity()? or Pass MainActivity or RSSFragment.this?
Also, how do I set list adapter to a listview fragment? Imagine lvRss isn't there.
I know it's a long question.
Thanks.
Should I use getActivity()? or Pass MainActivity or RSSFragment.this?
If you are in Activity the use this or Activity.this . IF you are in Fragment the use getActivity().
Also, how do I set list adapter to a listview fragment? Imagine lvRss isn't there.
Use setListAdapter method setting Adapter to ListFragment
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.
So far, a common pattern that I noticed in implementing an Array Adapter is to pass the layout twice to the adapter (in the constructor and in the getView) like this:
class MyAdapter extends ArrayAdapter{
MyAdapter(){
super(cxt, R.layout.adapter_layout);
}
getView(...){
convertView = inflate(R.layout.adapter_layout, ...);
}
}
But is it really necessary?
I have a fragment which contains several views including a ListView. In order to set the adpater for the listview, I thought I'd recycle some old (working) code I'd previously written in an activity. The code in the activity looked something like this:
adapter = new myadapter(
this,
list_of_things,
R.layout.custom_row_view,
new String[] {"label","day","time"},
new int[] {R.id.text1,R.id.text3, R.id.text2}
);
Where myadapter was a method within my activity's class, defined like so...
public class myadapter extends SimpleAdapter {
Now I tried to put the same myadapter method inside my fragment class and called
adapter = new myadapter(
this,
list_of_things,
R.layout.custom_row_view,
new String[] {"label","day","time"},
new int[] {R.id.text1,R.id.text3, R.id.text2}
);
but now I get a compile time error:
The constructor MyTestFragment.myadapter(MyTestFragment, ArrayList<HashMap<String,String>>, int, String[], int[]) is undefined
I don't know whether my bug could be some minor typo - or whether its something more fundamental, like not being allowed adapters within fragments.
use getActivity() instead of this in myadapter
adapter = new myadapter(
getActivity(),
list_of_things,
R.layout.custom_row_view,
new String[] {"label","day","time"},
new int[] {R.id.text1,R.id.text3, R.id.text2}
);
because the this in myadapter is referencing your Fragment and not your Activity.
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.