0

各タブがフラグメントであるスクロール可能なタブ ビューを持つアプリケーションがあります。ここで、各タブにカスタム リスト ビューを表示したいと考えています。しかし、私が思いついたコードによると、アクティビティのインスタンスが必要なため、インスタンスを渡す際にフラグメントに問題があります。これは、アダプターがインスタンスの受け渡しを要求している 11 行目にエラーがあるフラグメントです。

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;


    }

        }

Android Studioが出しているエラーは

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

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;
    }
}

スクロール可能なタブアダプター

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;
    }
}

部門をアクティビティに変更しようとしましたが、スクロール可能なタブ アダプターでエラーが発生します。CustomListAdapter で getActivity() を使用しようとしましたが、フラグメントにまだ値を割り当てていないため、実行時にフラグメントに null フィードを与えているというエラーが発生します。確認のために、fragment に一時的な値を指定して、デフォルト ビューを拡張できるようにしました。それでもnullエラーが発生しました。

4

1 に答える 1