我是Android的新手并创建一个显示日历事件的ListView。通过使用下面的代码,它可以从calendar获取所有事件。但是我只想在列表视图中显示当前周事件,任何想法或者答案对我来说非常有帮助。继续安卓。
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;
@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) {
ConferenceAdapter cAdapter = null;
ContentResolver contentResolver = context.getContentResolver();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String currentDateandTime = sdf.format(new Date());
String presentDate="dtstart.after("+currentDateandTime+")";
cursor = contentResolver.query(Uri.parse("content://com.android.calendar/events"),
(new String[] { "calendar_id", "title", "description", "dtstart", "dtend", "eventLocation"})
,null, null,null);
HashSet<String> calendarIds = new HashSet<String>();
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;
}
}
我怀疑,我必须在数据库中插入所有日历事件并使用查询获取当前周事件,或者可以在不将日历事件插入数据库的情况下完成。作为初学者,任何想法都将是有用的对我来说。
答案 0 :(得分:0)
我已经回答了SO我觉得它可能只能通过两个日期。
代码:
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");
}
String dtstart = "dtstart";
String dtend = "dtend";
String[] l_projection = new String[] { "title", "dtstart", "dtend" };
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yy");
//StartDate is today 10/28/2013
Date dateCC = formatter.parse("10/28/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("11/04/2013 23:59:59");
endOfDay.setTime(dateCCC);
Cursor eventCursorr = cr.query(l_eventUri, new String[] { "title",
"dtstart", "dtend" }, "(" + dtstart + ">" + after + " and "
+ dtend + "<" + endOfDay.getTimeInMillis() + ")", null,
"dtstart ASC");