-2

I have got an app with a tabLayout of 2 fragments with listView and I want to load the content from a URLConnection with Json but in the Fragment.java file I geht the Error: can't resolve method 'findViewByID(int)'.

   public class Fragment1 extends Fragment implements URLConnectionResponse {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        CanteenProvider canProv = new CanteenProvider();
        canProv.delegate = this;
        canProv.getDishes(getActivity().getApplicationContext());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.activity_canteen_tab_mensa, container, false);
    }

    @Override
    public void processFinish(List<?> output) {
        ListView canteenList = (ListView) findViewById(R.id.mensalistView);
        ArrayList<Dish> canteenItemContent = new ArrayList<Dish>();
        canteenItemContent = (ArrayList<Dish>)output;

        CanteenListAdapter canteenAdapter = new CanteenListAdapter(canteenItemContent, this);
        canteenList.setAdapter(canteenAdapter);
    }


} 
nolags
  • 633
  • 1
  • 11
  • 30

3 Answers3

1

you should use findViewById of View class. so you can create a global variable for your ListView :

ListView canteenList;

then in your onViewCreated initialize your canteenList

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        canteenList = view.findViewById(R.id.mensalistView);
    }
Mohammad Rahchamani
  • 5,002
  • 1
  • 26
  • 36
1

follow this answer on stack overflow

 private ListView canteenList=null;
  private View view = null;
 @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.activity_canteen_tab_mensa, container, false);
        canteenList = (ListView)view.findViewById(R.id.mensalistView);
        return view;
    }

    @Override
    public void processFinish(List<?> output) {

        ArrayList<Dish> canteenItemContent = new ArrayList<Dish>();
        canteenItemContent = (ArrayList<Dish>)output;

        CanteenListAdapter canteenAdapter = new CanteenListAdapter(canteenItemContent, view.getContext());
        canteenList.setAdapter(canteenAdapter);
    }
Community
  • 1
  • 1
Muhammad Waleed
  • 2,517
  • 4
  • 27
  • 75
0

You have to create one Global variable

View mainContent;

After that at the time of inflating in onCreateView like this

mainContent = inflater.inflate(R.layout.activity_canteen_tab_mensa, container, false);
return mainContent;

and at the end with the reference of the mainContent you can findViewById like this

ListView canteenList = (ListView) mainContent.findViewById(R.id.mensalistView);

Keyur Lakhani
  • 4,321
  • 1
  • 24
  • 35