-1

I have this problem where my listActivity.this and getApplicationContext get the err message "non-static cant be referenced from a static context"

I want to make a list in my fragment activity

    public static class PlaceholderFragment extends Fragment {

            ListView listView;
            public PlaceholderFragment() {
            }

            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                     Bundle savedInstanceState) {


                View rootView = inflater.inflate(R.layout.fragment_list, container, false);
                return rootView;

                //haal listView van XML
                listView = (ListView) rootView.findViewById(R.id.list);

                //waarden in Array definiëren om lijst te tonen

                String[] values = new String[] {"Park","Café","Nachtclub","Straat","School/Opleiding"};

                // Definieert nieuwe Adapter
                // Eerste paramater = context
                // Tweede parameter = layout voor rijen
                // Derde parameter = ID voor de textView
                // Vierde = data array

                ArrayAdapter<String> adapter = new ArrayAdapter<String>(listActivity.this, //cannot reference non-static from a static
                        android.R.layout.simple_list_item_1,android.R.id.text1, values);

                //Adapter instellen aan lijst
                listView.setAdapter(adapter);


                //onClick event voor lijst
                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                        //listview clicked item index
                        int itemPosition = position;

                        //listview clicked item value
                        String itemValue = (String) listView.getItemAtPosition(position);

                        //show alert
//cannot reference non-static from a static
                        Toast.makeText(getApplicationContext(),
                                "Position: " + itemPosition + " Listitem: " + itemValue, Toast.LENGTH_LONG).show();


                    }
                });

what should I do guys? Any suggestions of changing some methods here? Ii was following a tutorial btw.

TheNoob
  • 19
  • 2
  • 5

4 Answers4

0

It's hard to tell exactly since it seems you haven't provided all of the code, (PlaceholderFragment looks to be a nested class), but my guess is that the getApplicationContext() as defined in the surrounding class is not static and therefore is not accessible inside of PlaceholderFragment. To refer to it you'd need to have an instance of the surrounding class available to invoke it on. Without more context it would be difficult to suggest how you could refactor this to work.

laz
  • 28,320
  • 5
  • 53
  • 50
0

That won't work - try calling getActivity() where you need the Context instead. e.g. something like:

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

and then reference the outer class in your anonymous implementation:

Toast.makeText(PlaceholderFragment.getActivity(), "Position: " + itemPosition + " Listitem: " + itemValue, Toast.LENGTH_LONG).show();

See this SO question for some good discussion around this.

Community
  • 1
  • 1
declension
  • 4,110
  • 22
  • 25
0

If your fragment is attached to an activity you can use:

getActivity()

instead of getApplicationContext and listActivity.this.

Simas
  • 43,548
  • 10
  • 88
  • 116
0

you can use a handler, in onItemClick you just sendMessage, and show toast in handler when you receive this message

H.Bear
  • 1