3

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?

2
  • 1
    try this: StableArrayAdapter adapter = new StableArrayAdapter(getActivity(), R.layout.row , list); Commented Jun 14, 2013 at 8:20
  • FragmentActivity.this instead of this Commented Jun 14, 2013 at 8:35

2 Answers 2

6

You are setting the adapter inside a fragment. Use

StableArrayAdapter adapter = new StableArrayAdapter(this.getActivity(), R.layout.row , list);
Sign up to request clarification or add additional context in comments.

Comments

1
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().

2 Comments

I cannot see it popups in intellisense, that means it cannot reach them
this is not working, did you try passing yourActivity.this or getActivity(), let me know if this works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.