0

I have an application that has a scrollable tab view in which each tab is a fragment. Now i want each tab to have a custom list view. But according to code I came up with there is a problem in the fragment in passing its instance as it wants an Activity's instance. Here is the fragment that has the error at the 11th line where the adapter is asking for an instance to be passed.

public class Department extends Fragment {

        public Department(){}

    private String TAG = Department.class.getSimpleName();
    private static final String url = "http://api.androidhive.info/json/movies.json";
    private List<Socs> socList = new ArrayList<>();
    private ListView listView;
    private CustomListAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.dept,container,false);

        listView = (ListView) rootView.findViewById(R.id.lvDept);
        adapter = new CustomListAdapter(Department.this,socList);
        listView.setAdapter(adapter);
        JsonArrayRequest socRequest = new JsonArrayRequest(url,new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {

                for (int i=0;i<response.length() ; i++){
                    try {
                        JSONObject obj = response.getJSONObject(i);
                        Socs socs = new Socs();
                        socs.setDesc(obj.getString("title"));
                        socs.setName(obj.getString("title"));

                        socList.add(socs);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                adapter.notifyDataSetChanged();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d("ERROR" + error.getMessage());
            }
        });
        AppController.getInstance().addToRequestQueue(socRequest);

        return rootView;


    }

        }

The error that Android Studio is giving is

CustomListAdapter (Android.app.Activity, List<socs>) can not be applied to CustomListAdapter(com..Department, List<socs>)

Here is the adapter for CustomList

public class CustomListAdapter extends BaseAdapter {

    private Activity activity;
    private LayoutInflater inflater;
    private List<Socs> socItems;

    public CustomListAdapter(Activity activity, List<Socs> socItems){
        this.activity = activity;
        this.socItems = socItems;
    }

    @Override
    public int getCount() {
        return socItems.size();
    }

    @Override
    public Object getItem(int position) {
        return socItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {


        if (inflater==null)
            inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView==null)
            convertView = inflater.inflate(R.layout.list_row,null);

        TextView nameL = (TextView) convertView.findViewById(R.id.socName);
        TextView descL = (TextView) convertView.findViewById(R.id.socDesc);

        Socs socs = socItems.get(position);

        nameL.setText(socs.getName());
        descL.setText(socs.getDesc());

        return convertView;
    }
}

And the Scrollable tabs adapter

public class SectionsPagerAdapter extends FragmentStatePagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        Fragment fragment = null;
        switch (position){
            case 0:
                fragment = new Department();
                break;
            case 1:
                fragment  = new Technical();
                break;
            case 2:
                fragment = new Cultural();
                break;
            case 3:
                fragment = new StudentChapters();
        }
        return fragment;
    }

    @Override
    public int getCount() {
        return 4;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        String title = new String();
        switch (position){
            case 0:
                title="Department";
                break;
            case 1:
                title="Technical";
                break;
            case 2:
                title="Cultural";
                break;
            case 3:
                title="Student Chapters";
                break;
            default:
                title=null;
                break;
        }
        return title;
    }
}

I tried changing the department to Activity but then the scrollable tabs adapter gives an error. I tried using getActivity() in CustomListAdapter but it gives an error at runtime that I am giving a null feed to the fragment as I have not yet assigned any value to the fragment. Just for checking, I then gave a temporary value to fragment so it could inflate the default view. It still gave a null error.

Ayush Pahwa
  • 154
  • 1
  • 2
  • 12

1 Answers1

1

The error comes from the fact you are calling CustomListAdapter(); with the fragment as the first parameter and this object's constructor is expecting an activity. Instead you could do something like this:

new CustomListAdapter(Department.this.getActivity(),socList);

For your second problem, could you update your question with the error you get?

Quanturium
  • 5,500
  • 2
  • 27
  • 35