0

私はアンドロイドが初めてで、カレンダーイベントを表示するListViewを作成しています。以下のコードを使用して、カレンダーからすべてのイベントを取得しています。しかし、現在の週のイベントのみをリストビューに表示したいので、アイデアや回答私がアンドロイドで進むのに本当に役に立ちます。

   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;
    }


}

すべてのカレンダーイベントをデータベースに挿入し、クエリを使用して現在の週のイベントを取得する必要があるか、カレンダーイベントをデータベースに挿入せずに実行できるかどうか疑問に思っています。初心者として、どんなアイデアでも役に立ちます。

4

1 に答える 1

0

私はすでにそのことについて答えていますので、2つの日付を渡すだけでうまくいくと思います。

コード:

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");
于 2013-10-28T06:10:15.033 に答える