0

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

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 只包含一个线性布局内的 ListView。如果您需要更多信息,请告诉我。

4

1 回答 1

0

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

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

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

并在调用之前调用它notifyDataSetChanged()

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

于 2012-08-09T17:02:05.113 回答