0

I want to load a listview in my fragment and the problem with the adapter is the error - Cannot resolve constructor ArrayAdapter.

This is the onCreateView method in my class :

 public class AndroidGUIModifier implements IMyComponentGUIModifier, IFragmentEvents {

    @Override
    public void onCreateView() {
        tv1 = (TextView) fragment.findViewById("xtv1");
        tv2 = (TextView) fragment.findViewById("xtv2");
        lv=(ListView) fragment.findViewById("xdevices") ;

        mydevices = new ArrayList<String>();

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(AndroidGUIModifier.this,android.R.layout.simple_list_item_1,mydevices);// the problem here : Cannot resolve constructor ArrayAdapter

        lv.setAdapter(adapter);
    }

}
0

2 Answers 2

4

Obtain the Context from the fragment and just pass it to the ArrayAdapter :

ArrayAdapter<String> adapter = new ArrayAdapter<String>(fragment.getContext(), android.R.layout.simple_list_item_1, mydevices);
Sign up to request clarification or add additional context in comments.

3 Comments

I got this : Cannot resolve constructor 'ArrayAdapter(java.security.AccessControlContext, int, java.util.List<java.lang.String>)
Are you extending Fragment?
public class AndroidGUIModifier implements IMyComponentGUIModifier, IFragmentEvents {
1

If it is inside a fragment, you need to pass context for the first argument usually like this I think?

ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, mydevices);

If you are using AndroidGUIModifier as your fragment, can you try extending the Fragment class?

public class AndroidGUIModifier extends Fragment implements IMyComponentGUIModifier, IFragmentEvents {

2 Comments

Are you using the AndroidGUIModifier as your fragment?
the problem was like said Nika about fragment.getContext() , thank you .

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.