2

我想在 listView 中仅显示一周的事件,但我得到了所有事件并且它显示在 listview 中。

我正在使用此代码获取特定日期的事件,但没有从日历中获取任何事件。事件计数为 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

1 回答 1

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");
于 2013-11-13T10:16:17.417 回答