Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to call ArrayAdapter constructer from Fragment in Android

I am trying to add a two-column ListView to my android app. When I created the project, I selected the option Fragment which creates that beautiful navigation by sliding to left and right. So my MainActivity extends FragmentActivity.

my problem is when I am trying to add AddayAdapter to ListView, the construstor of ArrayAdapter looks for a context object and I dont know to pass that.

here is the code where I get error

StableArrayAdapter adapter = new StableArrayAdapter(this, R.layout.row , list);
listview.setAdapter(adapter);

When I pass "this", it passes FragmentActivity because the activity extends FragmentActivity. but constructor need a "Context" object.

it gives this error message

"The constructor MainActivity.StableArrayAdapter(MainActivity.DummySectionFragment, int, ArrayList<String>) is undefined"

How can I pass Context object?

like image 984
Arif YILMAZ Avatar asked Nov 24 '25 01:11

Arif YILMAZ


2 Answers

You are setting the adapter inside a fragment. Use

StableArrayAdapter adapter = new StableArrayAdapter(this.getActivity(), R.layout.row , list);
like image 136
Neoh Avatar answered Nov 26 '25 14:11

Neoh


StableArrayAdapter adapter = new StableArrayAdapter(getContext(), R.layout.row , list);
listview.setAdapter(adapter);

Did you try passing something like this from your Fragment. Try either getContext() or getApplicationContext().

like image 26
Thalaivar Avatar answered Nov 26 '25 16:11

Thalaivar