使用CustomListAdapter和Volley片段化ViewPager?

时间:2015-03-30 06:59:50

标签: android android-fragments android-volley fragmentpageradapter listview-adapter

我需要扩展public class FragmentB的{​​{1}}。它没有错误,但是当我尝试在适配器中调用它时。

FragmentPagerAdapter代码:

ActionBarActivity

它说我需要一个在public class FragmentPagerAdapter extends android.support.v4.app.FragmentPagerAdapter{ private static int NUM_ITEMS = 3; public FragmentPagerAdapter(FragmentManager fragmentManager) { super(fragmentManager); } // Returns total number of pages @Override public int getCount() { return NUM_ITEMS; } @Override public Fragment getItem(int position) { switch (position) { case 0: FragmentA a = new FragmentA(); return a; case 1: FragmentB b = new FragmentB(); return b; case 2: FragmentC c = new FragmentC(); return c; default: return null; } } // Returns the page title for the top indicator @Override public CharSequence getPageTitle(int position) { return "Page " + position; } } 延伸android.support.v4.app.Fragment的课程。在片段A和C中没有return b的实现,这就是为什么没有错误,而在ActionBarActivity中有。

基本上我需要在片段B上显示所有事件。如果我测试它没有片段它可以工作,但是对它们来说它会成为一个小问题。我需要片段,因为return b可以滑动。

片段B代码:

ViewPager

CustomListAdapter代码:     公共类CustomListAdapter扩展BaseAdapter {         私人活动活动;         private LayoutInflater inflater;         private List eventItems;         ImageLoader imageLoader = AppController.getInstance()。getImageLoader();         public CustomListAdapter(Activity activity,List eventItems){             this.activity =活动;             this.eventItems = eventItems;         }

private static final String TAG = FragmentB.class.getSimpleName();

private static final String url = "http://server.si/events.php";
private ProgressDialog pDialog;
private List<Event> eventList = new ArrayList<Event>();
private ListView listView;
private CustomListAdapter adapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    adapter = new CustomListAdapter(getActivity(), eventList);
}

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

    ListView listView = (ListView) rootView.findViewById(R.id.list);
    listView.setAdapter(adapter);

    pDialog.setMessage("Loading Events...");
    pDialog.show();

    JsonArrayRequest eventReq = new JsonArrayRequest(url,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    Log.d(TAG, response.toString());
                    hidePDialog();

                    for (int i = 0; i < response.length(); i++) {
                        try {
                            JSONObject obj = response.getJSONObject(i);
                            Event event = new Event();
                            event.setTitle(obj.getString("name"));
                            event.setPicture_url(obj.getString("picture_url"));
                            event.setCity(obj.getString("city"));
                            event.setDate(obj.getString("date"));

                            eventList.add(event);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    // notifying list adapter about data changes
                    // so that it renders the list view with updated data
                    adapter.notifyDataSetChanged();
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d(TAG, "Error: " + error.getMessage());
                    hidePDialog();

                }
    });

    AppController.getInstance().addToRequestQueue(eventReq);

    return rootView;
}

@Override
public void onDestroy() {
    super.onDestroy();
    hidePDialog();
}

private void hidePDialog() {
    if (pDialog != null) {
        pDialog.dismiss();
        pDialog = null;
    }
}

这是运行中的错误:

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

    @Override
    public Object getItem(int location) {
        return eventItems.get(location);
    }

    @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);

        if (imageLoader == null)
            imageLoader = AppController.getInstance().getImageLoader();
        NetworkImageView thumbNail = (NetworkImageView) convertView
                .findViewById(R.id.picture);
        TextView name = (TextView) convertView.findViewById(R.id.name);
        TextView city = (TextView) convertView.findViewById(R.id.city);
        TextView date = (TextView) convertView.findViewById(R.id.date);

        Event m = eventItems.get(position);

        thumbNail.setImageUrl(m.getPicture_url(), imageLoader);

        name.setText(m.getTitle());

        city.setText("City: " + String.valueOf(m.getCity()));

        date.setText(String.valueOf(m.getDate()));

        return convertView;
    }

看起来,我在OnCreate方法上做错了。我不知道我应该把代码放在哪里.OnCreate是在所有东西膨胀或创建活动和片段之前,而OnCreateView设置片段布局。我不知道该怎么做。

1 个答案:

答案 0 :(得分:3)

将此行添加到application中的AndroidManifest.xml代码中  MyApplication项目中的文件:

android:name="primoz.test.com.volleytesting.AppController"

如果您在应用程序清单中遗漏了这一行,那么每次调用它时AppController.getInstance()都将为空。