Search code examples
androidandroid-listviewadapter

No setAdapter method on List to connect it to Listview


I wrote a program that takes data from the server using the retrofit library and displays it inside a fragment that contains a Listview. The problem is that I don't have the setadapter option to connect the data to the listview please guide me

public class get_data extends Fragment {



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

        View view=inflater.inflate(R.layout.fragment_get_data,container,false);
    List list=view.findViewById(R.id.listview);


    Button btn_get_data=view.findViewById(R.id.btn_get_data);




    btn_get_data.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
                getdata();
        }

        private void getdata() {
            Call<List<models>> call=ApiClient.getInstance().getApi().getdata();
            call.enqueue(new Callback<List<models>>() {
                @Override
                public void onResponse(Call<List<models>> call, Response<List<models>> response)                                           {
                    List<models> modelsList=response.body();
                    String[] name=new String[modelsList.size()];
                    for (int i=0;i<modelsList.size();i++){
                            name[i]=modelsList.get(i).getName();
                    }
               ArrayAdapter<String> adapter=new ArrayAdapter<String>(getContext(),
                       androidx.appcompat.R.layout.support_simple_spinner_dropdown_item,
                       name);
                    //-----this section not work----\\\
               //     list.setadapter(adapter);

                }

                @Override
                public void onFailure(Call<List<models>> call, Throwable t) {
                    Toast.makeText(getContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
                    Log.e("erorr", t.getMessage() );
                }
            });

        }
    });

    return view;
  }


  }

please help me about this problem


Solution

  • The type object of variable list is List , which is not even a View , Hence you do not see the set adapter option! Make it either RecyclerView or ListView depending on what view element you've used in your layout XML for ID listview.