2013-10-31 40 views
2

我想在listView中僅顯示一週的事件,但是我得到了所有事件,並且它在列表視圖中顯示。如何從android的日曆中獲取特定星期的事件?

我使用此代碼來獲取特定日期的事件,但沒有從日曆中獲取任何事件。事件計數爲0.我是這個問題的結構!如何獲得特定日期或星期的活動?

public class Meeting extends Activity { 
    public ConferenceAdapter adapter; 
    ListView meetingListView; 
    static Cursor cursor; 
    private String description = null; 
    private String where = null; 
    public String[] number; 
    static List<GoogleCalendar> gCalendar = null; 
    private static String startString; 
    private static String endString; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.ec_meeting); 
     adapter = new ConferenceAdapter(this); 
     readCalendar(this); 
     meetingListView = (ListView) findViewById(R.id.meetinglistView); 
     meetingListView.setAdapter(new MeetingListViewAdapter(Meeting.this, adapter, gCalendar));  
     meetingListView.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> arg0, View view, int arg2, long arg3) { 
       TextView descriptionId1 = ((TextView) view.findViewById(R.id.descriptionmeeting)); 
       TextView where=((TextView) view.findViewById(R.id.wheretextView)); 
       TextView tittle = ((TextView) view.findViewById(R.id.titlemeeting));     
       Meeting.this.where=where.getText().toString(); 
       description = descriptionId1.getText().toString(); 



       StringBuffer numbers =new StringBuffer();    
       String a=Meeting.this.where.replaceAll("-","").replaceAll(" ", "").replaceAll("\\(","").replaceAll("\\)","")+description.replaceAll("-","").replaceAll(" ", "").replaceAll("\\(","").replaceAll("\\)","");      
       if(a.isEmpty()){ 
        Toast.makeText(getApplicationContext(),"Sorry no conference numbers found", Toast.LENGTH_LONG).show(); 
       }else{    
       Pattern p = Pattern.compile("[0-9]+[0-9]"); 
         Matcher m = p.matcher(a);      
         while (m.find()) {       
           Meeting.this.addComma(numbers,m.group().toString());      
         } 
         number = numbers.toString().split(",");      
         Intent intent = new Intent(Meeting.this,EcConferenceNumber.class);      
         intent.putExtra("strings", number);      
         startActivity(intent);      
         finish(); 
            } 
      } 
     }); 
    } 

public void addComma(StringBuffer updatedString,String value){ 
    if(updatedString.length() >0 && updatedString != null){ 
     updatedString.append(","); 
    } 
    updatedString.append(value); 
} 
    public static void readCalendar(Context context) { 

     ContentResolver contentResolver = context.getContentResolver(); 

     Calendar c_start = Calendar.getInstance(); 
     c_start.set(2014,2,4,0,0); //Note that months start from 0 (January) 
     Calendar c_end = Calendar.getInstance(); 
     c_end.set(2013,2,11,0,0); //Note that months start from 0 (January) 


     String selection = "((dtstart >= "+c_start.getTimeInMillis()+") AND (dtend <= "+c_end.getTimeInMillis()+"))"; 

     String[] selectionArgs = new String[] {startString, endString}; 
     cursor = contentResolver.query(Uri.parse("content://com.android.calendar/events"), 
       (new String[] { "calendar_id", "title", "description", "dtstart", "dtend", "eventLocation"}) 
       ,null,selectionArgs,selection); 




     gCalendar = new ArrayList<GoogleCalendar>(); 
     try { 
      System.out.println("Count=" + cursor.getCount()); 
      if (cursor.getCount() > 0) { 
       System.out.println("the control is just inside of the cursor.count loop"); 
       while (cursor.moveToNext()) { 
        GoogleCalendar googleCalendar = new GoogleCalendar(); 
        gCalendar.add(googleCalendar); 
        int calendar_id = cursor.getInt(0); 
        googleCalendar.setCalendar_id(calendar_id); 
        String title = cursor.getString(1); 
        googleCalendar.setTitle(title); 
        String description = cursor.getString(2); 
        googleCalendar.setDescription(description); 
        String dtstart = cursor.getString(3); 
        googleCalendar.setDtstart(dtstart); 
        String dtend = cursor.getString(4); 
        googleCalendar.setDtend(dtend); 
        String eventlocation = cursor.getString(5); 
        googleCalendar.setEventlocation(eventlocation); 

    } 
      } 
     } catch (AssertionError ex) { 
      ex.printStackTrace(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 

// inner class for setting the home screen value to the list view 
class MeetingListViewAdapter extends BaseAdapter { 
    private List<GoogleCalendar> calendars = null; 
    LayoutInflater inflater = null; 
    public MeetingListViewAdapter(Activity activity, ConferenceAdapter adapter, List<GoogleCalendar> gCalendar) { 
     this.calendars = gCalendar; 
     inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 
    @Override 
    public int getCount() { 
     return calendars.size(); 
    } 
    @Override 
    public Object getItem(int position) { 
     return position; 
    } 
    @Override 
    public long getItemId(int position) { 
     return position; 
    } 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

     View vi = convertView; 
     if (convertView == null) 
      vi = inflater.inflate(R.layout.calendar_events, null);  
     TextView titleNameTV = (TextView) vi.findViewById(R.id.titlemeeting); 
     TextView timeTV = (TextView) vi.findViewById(R.id.profileStepCountTV); 
     TextView whereTv=(TextView) vi.findViewById(R.id.wheretextView); 
     TextView descriptionTV = (TextView) vi.findViewById(R.id.descriptionmeeting);  
     GoogleCalendar calendar = calendars.get(position); 
     titleNameTV.setText(calendar.getTitle()); 
     descriptionTV.setText(calendar.getDescription()); 
     whereTv.setText(calendar.getEventlocation()); 
     return vi; 
    } 


} 

任何答案對我來說都是有幫助的。

回答

4

替換此代碼:

ContentResolver contentResolver = context.getContentResolver(); 

     Calendar c_start = Calendar.getInstance(); 
     c_start.set(2014,2,4,0,0); //Note that months start from 0 (January) 
     Calendar c_end = Calendar.getInstance(); 
     c_end.set(2013,2,11,0,0); //Note that months start from 0 (January) 


     String selection = "((dtstart >= "+c_start.getTimeInMillis()+") AND (dtend <= "+c_end.getTimeInMillis()+"))"; 

     String[] selectionArgs = new String[] {startString, endString}; 
     cursor = contentResolver.query(Uri.parse("content://com.android.calendar/events"), 
       (new String[] { "calendar_id", "title", "description", "dtstart", "dtend", "eventLocation"}) 
       ,null,selectionArgs,selection); 

有了這個:

Uri l_eventUri; 
    Calendar calendar = Calendar.getInstance(); 
    if (Build.VERSION.SDK_INT >= 8) { 
     l_eventUri = Uri.parse("content://com.android.calendar/events"); 
    } else { 
     l_eventUri = Uri.parse("content://calendar/events"); 
    } 
    ContentResolver contentResolver = context.getContentResolver(); 

    String dtstart = "dtstart"; 
    String dtend = "dtend"; 

    String[] l_projection = new String[] { "title", "dtstart", "dtend" }; 

    SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yy"); 
    // Date dateCC = formatter.parse("04/27/2013"); 
    Date dateCC = formatter.parse("11/13/2013"); 
    calendar.setTime(dateCC); 

    long after = calendar.getTimeInMillis(); 

    SimpleDateFormat formatterr = new SimpleDateFormat("MM/dd/yy hh:mm:ss"); 

    Calendar endOfDay = Calendar.getInstance(); 
    Date dateCCC = formatterr.parse("17/13/2013 23:59:59"); 
    // Date dateCCC = formatterr.parse(startDate + " 23:59:59"); 
    endOfDay.setTime(dateCCC); 

    cursor= contentResolver.query(l_eventUri, new String[] { "title", 
      "dtstart", "dtend" }, "(" + dtstart + ">" + after + " and " 
      + dtend + "<" + endOfDay.getTimeInMillis() + ")", null, 
      "dtstart ASC"); 
+0

是什麼在這裏爲dtstart ASC?我不明白srikanth – Deepak

+0

我得到了答案srikanth謝謝 – Deepak

+0

我得到了感謝' – user2841300

相關問題