Java 获取从日历视图到其他活动的日期

Java 获取从日历视图到其他活动的日期,java,android,sqlite,android-activity,calendarview,Java,Android,Sqlite,Android Activity,Calendarview,我需要将日期信息从日历视图带入另一个活动,以便将信息正确存储在数据库中 我需要使用日期的活动是: package com.example.calendar; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.CalendarView; import

我需要将日期信息从
日历视图
带入另一个活动,以便将信息正确存储在数据库中

我需要使用日期的活动是:

    package com.example.calendar;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CalendarView;
import android.widget.EditText;
import android.app.Activity;
import android.content.Intent;
import static android.provider.BaseColumns._ID;
import static com.example.calendar.Constants.TABLE_NAME;
import static com.example.calendar.Constants.TIME;
import static com.example.calendar.Constants.TITLE;
import static com.example.calendar.Constants.DETAILS;
import static com.example.calendar.Constants.DATE;
import static com.example.calendar.Constants.CONTENT_URI;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;


public class CreateAppointment extends Activity implements OnClickListener{

    private static String[] FROM = { _ID, DATE, TIME, TITLE, DETAILS};
    private static String ORDER_BY = TIME + " ASC";
    AppointmentsData appointments;
    CalendarView calendar;
    String string;
    EditText nameTextBox;
    EditText timeTextBox;
    EditText detailsTextBox;
    Button createButton;
    SQLiteDatabase db;


    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.create);

        createButton = (Button) findViewById(R.id.apptSave);
        nameTextBox = (EditText)findViewById(R.id.apptName);//Assign the global name box
        timeTextBox = (EditText)findViewById(R.id.apptTime);//Assign the global time box
        detailsTextBox = (EditText)findViewById(R.id.apptDetails);//Assign the global details box
        calendar = (CalendarView)findViewById(R.id.calendar);
        createButton.setOnClickListener(this);
        appointments = new AppointmentsData(this);
        string = "row";

    }

    private void addAppointment(String string) {
        /* Insert a new record into the Events data
        source. You would do something similar
        for delete and update. */
        String getTitle = nameTextBox.getText().toString();
        String getTime = timeTextBox.getText().toString();
        String getDetails = detailsTextBox.getText().toString();

        db = appointments.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(DATE, calendar.getDate());
        values.put(TIME, getTime);
        values.put(TITLE, getTitle);
        values.put(DETAILS, getDetails);
        getContentResolver().insert(CONTENT_URI, values);
        }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch(v.getId()){

        case R.id.apptSave:
            addAppointment(string);
            finish();
            break;

        }

    }

}
我需要从主活动中提取日期,即:

    package com.example.calendar;

import java.util.Calendar;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import static android.provider.BaseColumns._ID;
import static com.example.calendar.Constants.TABLE_NAME;
import static com.example.calendar.Constants.TIME;
import static com.example.calendar.Constants.TITLE;
import static com.example.calendar.Constants.DETAILS;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class MainActivity extends Activity implements OnClickListener {

    private AppointmentsData appointments;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        View createButton = findViewById(R.id.btn_create);
        View editButton = findViewById(R.id.btn_viewEdit);
        View deleteButton = findViewById(R.id.btn_delete);
        View moveButton = findViewById(R.id.btn_move);
        View searchButton = findViewById(R.id.btn_search);
        View translateButton = findViewById(R.id.btn_translate);
        View exitButton = findViewById(R.id.exit);

        createButton.setOnClickListener(this);
        editButton.setOnClickListener(this);
        deleteButton.setOnClickListener(this);
        moveButton.setOnClickListener(this);
        searchButton.setOnClickListener(this);
        translateButton.setOnClickListener(this);
        exitButton.setOnClickListener(this);

        appointments = new AppointmentsData(this);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Intent i;
        switch (v.getId()) {
        case R.id.btn_create:
        i = new Intent(this, CreateAppointment.class);
        startActivity(i);
        break;
        case R.id.btn_viewEdit:
            i = new Intent(this, EditViewAppointment.class);
            startActivity(i);
        break;
        case R.id.btn_move:
            i = new Intent(this, MoveAppointment.class);
            startActivity(i);
        break;
        case R.id.btn_delete:
            i = new Intent(this, DeleteAppointment.class);
            startActivity(i);
        break;
        case R.id.btn_search:
            i = new Intent(this, SearchAppointment.class);
            startActivity(i);
        break;
        case R.id.btn_translate:
            i = new Intent(this, TranslateAppointment.class);
            startActivity(i);
        break;
        case R.id.exit:
            finish();
        break;

        }

    }

}
编辑:

此外,以下是我的主要活动布局:

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:background="@color/background"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="225dp"
            android:orientation="vertical"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin" >

            <CalendarView
                android:id="@+id/calendar"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </LinearLayout>

        <TableLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:layout_marginBottom="2dip"
            android:layout_marginTop="2dip"
            android:stretchColumns="*" >

            <TableRow>

                <Button
                    android:id="@+id/btn_create"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/create" />

                <Button
                    android:id="@+id/btn_viewEdit"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/viewEdit" />
            </TableRow>

            <TableRow>

                <Button
                    android:id="@+id/btn_delete"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/delete" />

                <Button
                    android:id="@+id/btn_move"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/move" />
            </TableRow>

            <TableRow>

                <Button
                    android:id="@+id/btn_search"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/search" />

                <Button
                    android:id="@+id/btn_translate"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/translate" />
            </TableRow>
        </TableLayout>

        <Button
            android:id="@+id/exit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/exit"/>

</LinearLayout>


如何解决此问题?

要在活动之间传输数据,可以使用篮子

生成数据的活动

     i = new Intent(this, DeleteAppointment.class);
     i.putExtra("name", strFirstName);
     startActivity(i);
正在检索数据的活动

@Override
protected void onHandleIntent(Intent intent)
{
    String action = intent.getAction();

    String name= intent.getStringExtra("name");



}

您不能使用intent将值从一个活动传递到另一个活动我在启动另一个活动(主活动中的切换方法)时是否这样做?是,如果您的日历视图属于某个活动,则在该活动中初始化它本身。您不能在另一个activity中初始化其他activity的视图使用您的ManInActivity xml来确保Calendant视图属于mainactivity CML日历视图确实属于mainactivity xml。所以它应该从其他活动中移除。好的观点。我试图解释他如何在活动之间传递数据对于这个案例,calendarview给了我很长的时间。这是否意味着我必须在OnHandleContent中使用long而不是字符串?getLongExtra(“name”)@是的,您必须使用预期接收的类型