Android:尝试从服务器获取对象列表并在listview中显示

时间:2014-07-23 15:59:22

标签: android listview android-listview calendar adapter

公平地说,IM a n00by ..

我试图从服务器获取一个对象列表并在listview中显示。我有一个适配器向服务器发送请求(现在为jar)并返回它。我有一个片段连接视图和适配器但我做错了。

我觉得它有什么可做的如何我在getDayItems()中调用onSelectedDayChange(...)或/和因为可能在不同的地方混合了List / ArrayList。但我不是法官。

关于选择日期,服务器此刻不接受任何变量,如果点击某个日期,它应该只给我一个列表。

这是片段..

    public class FragmentA extends Fragment {

      private ArrayList<CalanderMeal> calMeals = new ArrayList<CalanderMeal>();
      private ListView listView;
      private CalendarView calendarView;
      private CalendarAdapter adapter;
      private View parentView;
      private int yearSelected; //year selected by user
      private int monthSelected; //month selected by user
      private int dayOfMonthSelected; //day of month selected by user

      public static FragmentA newInstance(String text){
          FragmentA f = new FragmentA();

          return f;
      }

      @TargetApi(Build.VERSION_CODES.HONEYCOMB)
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        parentView = inflater.inflate(R.layout.fragment_a, container, false);
        calendarView = (CalendarView) parentView.findViewById(R.id.calendarView1);
        listView = (ListView) parentView.findViewById(R.id.dailyView1);

        /*
        adapter = new CalendarAdapter(getActivity(), R.layout.fragment_a, calMeals);
        ListView listview = (ListView)parentView.findViewById(R.id.dailyView1);
        listview.setAdapter(adapter);
        */

        calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener(){

            @Override
            public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
                yearSelected = year;
                monthSelected = month;
                dayOfMonthSelected = dayOfMonth;

                calMeals = (ArrayList<CalanderMeal>) adapter.getDayItems();

                adapter = new CalendarAdapter(getActivity(), R.layout.fragment_a, calMeals);
                ListView listview = (ListView)parentView.findViewById(R.id.dailyView1);
                listview.setAdapter(adapter);
            }
        });

        return parentView;
     }
    }

这是适配器..

public class CalendarAdapter extends ArrayAdapter<CalanderMeal> {


    private ArrayList<CalanderMeal> mCalList;
    private CalendarService calService;

    public CalendarAdapter(Context context, int layoutResourceID,     
                   ArrayList<CalanderMeal> calList){
        super(context, layoutResourceID, calList);
        this.mCalList = calList;
}

public List<CalanderMeal> getDayItems(){

    List<CalanderMeal> calList;
    calService = CalendarServiceFactory.getCalanderService();
    calList = calService.getMealsByDay();

    return calList;
}

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    return null;
}

}

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

您的getView方法返回null - 您希望如何直观地看到任何结果?