Compilation error creating an adapter in a fragment - android

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.

Related

ArrayAdapter initialization error (Android)

I have written the following code snippet in my App:
public Fragment getItem(int position) {
switch(position){
case 0:
LP_Events_Tab mEventsTab= new LP_Events_Tab();
mList= (ListView) findViewById(R.id.event_list);
String[] temp= {"1","2","3"};
ArrayAdapter<String> arrayAdapter= new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, temp);
return mEventsTab;
/*Other cases*/
}
return null;
}
For case 0, I am using an array of strings named temp to initialize the ArrayAdapter. However, Android Studio redlines the ArrayAdapter initialization arguments: (this,android.R.layout.simple_list_item_1, temp) and thus, I cannot proceed. I do not see any error with these arguments but Android Studio does and hence, I am stuck.
Replace the following line of code inside your Fragment -
ArrayAdapter<String> arrayAdapter= new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1, temp);
Hope this helps, let me know if you need any more help.
The problem is with your Constructor of Adapter
ArrayAdapter(Context context, int resource, List<T> objects)
So make sure you are passing right parameters, in your case your first parameter is this which will not work other then in Activity.
So pass either getContext or getActivity.
Try to replace "this" with "MainActivity.this"(or whatever class name you have)

Constructor cannot be resolved

I am trying to create an array adapter to populate my list view.
Below is the code I have however the Array Adapter gets the error "Cannot resolve constructor".
Can anybody please help me with why this might be happening?
Thanks.
private void populateListView(){
//Create list of items'
String[] myItems = {"Blue", "red", "green", "purple"};
//Build adapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.deal_items, myItems);
//Configure the list view
ListView list = (ListView) findViewById(R.id.listViewMain);
list.setAdapter(adapter);
}
The first parameter to the ArrayAdapter constructor needs to be a Context. You appear to have implemented this method in a Fragment, which is not a Context. If so, then replace this in the ArrayAdapter constructor with getActivity().

Cant set list to custom adapter in fragment

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);

android: Refresh ListView using ListAdapter and SimpleCursorAdapter

I'm trying to refresh a ListView that uses a ListAdapter created as a SimpleCursorAdapter.
Here is my code for creating the Cursor and ListAdapter in onCreate which populates the ListView.
tCursor = db.getAllEntries();
ListAdapter adapter=new SimpleCursorAdapter(this,
R.layout.row, tCursor,
new String[] columns,
new int[] {R.id.rowid, R.id.date});
setListAdapter(adapter);
Then, I add some data to the db in another method, but I can't figure out how to refresh the ListView. Similar questions on stackoverflow and other places mention using notifyDataSetChanged() and requery(), but neither are methods of ListAdapter or SimpleCursorAdapter.
I'm able to get the ListView to refresh by creating a new adapter and calling setListAdapter again.
I named it adapter2 in the other method.
tCursor = db.updateQuery();
ListAdapter adapter2=new SimpleCursorAdapter(this,
R.layout.row, tCursor,
columns,
new int[] {R.id.rowid, R.id.date});
setListAdapter(adapter2);
I'm not sure why this is necessary, but it works for now. If anyone has a better solution, I'm willing to try it.
The method notifyDataSetChanged comes from the SimpleCursorAdapter parent class BaseAdapter. The parent implements ListAdapter and you should be able to pass it to your ListView.
Try:
tCursor = db.getAllEntries();
BaseAdapter adapter=new SimpleCursorAdapter(this,
R.layout.row, tCursor,
new String[] columns,
new int[] {R.id.rowid, R.id.date});
setListAdapter(adapter);
Then you should be able to use notifyDataSetChanged.
In that case I recommend to go with custom Adapter, by extending the BaseAdapter class.
You can define the adapter as a class variable if it needs to be accessed from other methods in the same class. Then you can call changeCursor() to refresh the ListView.
public class mainActivity extends AppCompatActivity {
// Define the Cursor variable here so it can be accessed from the entire class.
private SimpleCursorAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_coordinator_layout)
// Get the initial cursor
Cursor tCursor = db.getAllEntries();
// Setup the SimpleCursorAdapter.
adapter = new SimpleCursorAdapter(this,
R.layout.row,
tCursor,
new String[] { "column1", "column2" },
new int[] { R.id.rowid, R.id.date },
CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
// Populate the ListAdapter.
setListAdapter(adapter);
}
protected void updateListView() {
// Get an updated cursor with any changes to the database.
Cursor updatedCursor = db.getAllEntries();
// Update the ListAdapter.
adapter.changeCursor(updatedCursor);
}
}
If the list view needs to be updated from methods in another class, the adapter variable should be declared public static instead
public static SimpleCursorAdapter adapter;

OnFinishInflate inside ListActivity

I have a main class filled with a cursor adapter:
public class MainMenu extends ListActivity{
...
private void updateData(){
...
SimpleAdapter notes = new SimpleAdapter(this, array, R.layout.row_task, from, to); setListAdapter(notes);
}
}
I need to know when this list has finnished to inflate it, so I've discovered this methods:
ListView.onFinishInflate()...
But I have no idea how to override it inside my code.
Any idea?
You'll need your own View that will extend ListView. And than you can override whatever you want.
Note: Method is not specific to the ListView it's inherited from View. But since you'll need functionality of the ListView you'll need to extend ListView.

Categories

Resources