ListView无法加载数据

时间:2012-08-09 16:00:05

标签: android android-listview

我真的坚持这个并且不能继续前进我试图将谷歌日历事件填充到列表视图中并且没有看到任何结果。我很抱歉发布这么冗长的代码,但我真的被卡住了!! 我的活动

public class EventListActivity extends Activity {

 private List<Events> events = new ArrayList<Events>();
 private Handler handler = new Handler();
  private ProgressDialog progressBar;
  private SelectableEventsAdapter eventsAdapter;

@Override
public void onCreate(Bundle savedInstance) {
     super.onCreate(savedInstance);
     getAccount();
    setContentView(R.layout.layout_main);
    setEventsListView();
}

private void setEventsListView() {
    Log.i("ReachedsetEvents", "Reached set Events view 1");
     final ListView eventsListView = (ListView) findViewById(R.id.list_main);
     eventsAdapter = new SelectableEventsAdapter(this, events);
     //eventsAdapter.getView(0, eventsListView, eventsListView);
    eventsAdapter.setAdapter(eventsAdapter);
    Log.i("ReachedsetEvents", "Reached set Events view 2");
    eventsAdapter.sort();

     private void retrieveEvents() {
    // Retrieves the attendees on a separate thread.
    new Thread(new Runnable() {

      public void run() {

        EventsRetriever eventsRetriever =
            new EventsRetriever(EventListActivity.this, OAuthManager.getInstance()
                .getAccount());
        final List<Events> newevents = eventsRetriever.getEvents();
        Log.i("reachedRetrieve", "reached retrieve");
        // Update the progress bar
        handler.post(new Runnable() {

          public void run() {
            if (newevents != null) {
              events.clear();
              events.addAll(newevents);

             eventsAdapter.sort();
             eventsAdapter.notifyDataSetChanged();
            }

            Log.d(Constants.TAG, "Got attendees, dismissing progress bar");
            if (progressBar != null) {
              progressBar.dismiss();
              Log.d(Constants.TAG, "Progress bar should have been dismissed");
            }
          }
        });
      }
    }).start();
    // Show a progress bar while the attendees are retrieved from the phone's
    // database.
    progressBar =
        ProgressDialog.show(this, null, getString(R.string.retrieve_contacts_wait_text), true);
  }
           @Override
  protected void onResume() {
    super.onResume();
   // getAccount();
  }

  /**
   * Prompt user to choose an account and retrieve attendees from phone's
   * database.
   */
  private void getAccount() {
    OAuthManager.getInstance().doLogin(false, this, new OAuthManager.AuthHandler() {
      public void handleAuth(Account account, String authToken) {
        if (account != null) {
          retrieveEvents();
        }
      }
    });
  }}

我的自定义适配器

public SelectableEventsAdapter(Context context, List<Events> items) {
      super(context, R.layout.layout_main,items);
      inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
   Events item = getItem(position);
    LinearLayout eventsView = getView(convertView);

    setViews(item, eventsView);
    return eventsView;
  }
  private LinearLayout getView(View convertView) {
      Log.i("Reached", "Reached getView long");
      LinearLayout eventsView;
    if (convertView == null) {
         eventsView = new LinearLayout(getContext());
         inflater.inflate(R.layout.layout_main,eventsView,true);

    } else {
        eventsView = (LinearLayout) convertView;
    }
    return eventsView;
  }
  private void setViews(Events item, LinearLayout eventsView) {
        setTitleView(item, eventsView);

      }
     private void setTitleView(Events item, LinearLayout eventsView) {
   ListView titleView = (ListView) eventsView.findViewById(R.id.list_main);
       titleView.setEnabled(true);
    //titleView.setText(item.title);

}

以及获取事件的检索器:

  public EventsRetriever(Activity activity, Account account) {
    this.activity = activity;
    this.account = account;
  }
  public static final String[] FIELDS = {
      CalendarContract.Events.TITLE,
      CalendarContract.Events.DESCRIPTION,
      CalendarContract.Events.ORGANIZER,
      CalendarContract.Events.STATUS,
      CalendarContract.Events.DTSTART,
      CalendarContract.Events.EVENT_LOCATION,

      };


      public static final Uri CALENDAR_URI = Uri.parse("content://com.android.calendar/calendars");

      String[] selectionArgs = new String[] {CalendarServiceBuilder.ACCOUNT_NAME, Constants.ACCOUNT_TYPE,
                CalendarServiceBuilder.ACCOUNT_NAME};


  /**
   * Get the list of user's contacts.
   * 
   * @return The list of contacts.
   */
  public List<Events> getEvents() {
    List<Events> result = new ArrayList<Events>();
    ContentResolver cr = activity.getContentResolver();
    Cursor cursor =
        cr.query(CalendarContract.Events.CONTENT_URI, FIELDS,null,null, null);

    try {
          if(cursor.getCount() > 0) {
            while (cursor.moveToNext()) {
              String title =cursor.getString(cursor.getColumnIndex(
                      CalendarContract.Events.TITLE));
              String description = cursor.getString(cursor.getColumnIndex(
                      CalendarContract.Events.DESCRIPTION));
              String organizer = cursor.getString(cursor.getColumnIndex(
                      CalendarContract.Events.ORGANIZER));
              String status = cursor.getString(cursor.getColumnIndex(
                      CalendarContract.Events.STATUS));
              String location = cursor.getString(cursor.getColumnIndex(
                      CalendarContract.Events.EVENT_LOCATION));
              result.add(new Events(title,description,organizer,status,location));
              Log.i("Reachedretriever", "Reached retriever");
             }

          }}
            finally {
                  cursor.close();
                }
        return result;

        }}

我无法在屏幕上看到任何填充内容!也只是为了让你知道我正在使用未经授权的Oauth进行api访问。 layout_main.xml只包含linearlayout中的ListView。 如果您还需要更多信息,请与我们联系。

1 个答案:

答案 0 :(得分:0)

我认为适配器中的事件列表与您活动中的列表不同。

因此,当已经填充事件列表时,最后调用setAdapter(),或者在适配器中创建一个setter

setItems(List<Events> events){
     this.events = events
 } 

并在致电notifyDataSetChanged()

之前调用它

你可能还想用AsyncTask替换你的线程/处理程序实现,它更易于阅读和使用更快(恕我直言)。