【问题标题】:Android fragment having problems in passing instance in CustomListAdapterAndroid 片段在 CustomListAdapter 中传递实例时出现问题
【发布时间】:2015-02-11 07:34:15
【问题描述】:

我有一个应用程序,它有一个可滚动的选项卡视图,其中每个选项卡都是一个片段。现在我希望每个选项卡都有一个自定义列表视图。但是根据我想出的代码,在传递它的实例时片段中存在问题,因为它需要一个 Activity 的实例。 这是适配器要求传递实例的第 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>)

这是自定义列表的适配器

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() ,但它在运行时给出了一个错误,因为我还没有为片段分配任何值,所以我给片段提供了一个空提要。只是为了检查,然后我给片段一个临时值,以便它可以膨胀默认视图。它仍然给出了一个空错误。

【问题讨论】:

    标签: android android-fragments android-listview android-scrollable-tabs


    【解决方案1】:

    错误来自您使用片段作为第一个参数调用CustomListAdapter(); 并且该对象的构造函数期待一个活动。相反,您可以这样做:

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

    对于你的第二个问题,你能用你得到的错误更新你的问题吗?

    【讨论】:

    • 一切都解决了吗,还是您需要帮助解决第二个问题?
    • 不,已解决。我现在了解错误并已将其删除。
    猜你喜欢
    • 1970-01-01
    • 2023-03-28
    • 2016-05-10
    • 1970-01-01
    • 2014-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多