我的应用将事件添加到用户的日历中。但是我目前正在使用第一个有效日历(通常是id 1)。用户从其设备上的可用日历中进行选择的最佳方法是什么
答案 0 :(得分:0)
所以我自己得到了它 在布局中我有一个微调器,它由这个在简历上运行的方法填充和维护
public void getCalendarsForCheckBox(){
String[] projection =
new String[]{
Calendars._ID,
Calendars.NAME,
Calendars.ACCOUNT_NAME,
Calendars.ACCOUNT_TYPE};
Uri calendars = Uri.parse("content://calendar/calendars");
ContentResolver cr = getContentResolver();
Cursor managedCursor =
getContentResolver().
query(Calendars.CONTENT_URI,
projection,
Calendars.VISIBLE + " = 1",
null,
Calendars._ID + " ASC");
int j=0;
if (managedCursor.moveToFirst()) {
String calName;
Integer calId;
int nameColumn = managedCursor.getColumnIndex("name");
int idColumn = managedCursor.getColumnIndex("_id");
Integer i=0;
do {
calName = managedCursor.getString(nameColumn);
if (!(calName == null)) {
j++;
}
} while (managedCursor.moveToNext());
}
//retrieving previously selected calendar (defaults to id 1)
SharedPreferences prefs = getBaseContext().getSharedPreferences("prefs", MODE_PRIVATE);
Integer theID = prefs.getInt("calendarID",1);
Integer indexToSelect=0;
String calTitles[] = new String[j];
if (managedCursor.moveToFirst()) {
String calName;
Integer calId;
int nameColumn = managedCursor.getColumnIndex("name");
int idColumn = managedCursor.getColumnIndex("_id");
Integer i=0;
do {
calName = managedCursor.getString(nameColumn);
calId = Integer.parseInt(managedCursor.getString(idColumn));
if (!(calName == null)) {
if (calId.equals(theID)) indexToSelect = i;
calTitles[i] = calName + "-" + calId;
i++;
}
} while (managedCursor.moveToNext());
}
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, calTitles); //selected item will look like a spinner set from XML
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setSelection(indexToSelect);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
String findTheIdSplit[] = ((TextView)selectedItemView).getText().toString().split("-");
Integer TheID = Integer.parseInt(findTheIdSplit[findTheIdSplit.length-1]);
SharedPreferences prefs = getBaseContext().getSharedPreferences("prefs", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putInt("calendarID",TheID);
editor.commit();
//for committing the selected calendar
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here if needed
}
});
}