0

私は本当にこれで立ち往生していて、先に進むことができません.Googleカレンダーのイベントをリストビューに入力しようとしていますが、結果が表示されません. このような長いコードを投稿して申し訳ありませんが、本当に立ち往生しています!! 私の活動

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;

        }}

画面に何も表示されません。また、API アクセスに無許可の Oauth を使用していることをご承知おきください。layout_main.xml には、linearlayout 内に ListView のみが含まれています。さらに何か必要な場合はお知らせください。

4

1 に答える 1

0

アダプターのイベントのリストは、アクティビティのリストと同じではないと思います。

したがって、イベントのリストがすでに入力されているときに最後に setAdapter() を呼び出すか、アダプターでセッターを作成します

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

呼び出す前に呼び出しますnotifyDataSetChanged()

また、スレッド/ハンドラーの実装を、はるかに読みやすく、より高速に使用できる AsyncTask に置き換えることもできます (IMHO)。

于 2012-08-09T17:02:05.113 に答える