我如何获得额外从意向(作为一种长期)?(How Do I Get Extra From an Int

2019-10-19 15:11发布

我有这个问题访问额外的从意图。 我解析的值是一个long型的,我需要被存储在数据库中该值。

所以这是我到目前为止有:

MainActivity:

package com.example.calendar;

import java.sql.Date;
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 android.widget.CalendarView;
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 android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class MainActivity extends Activity implements OnClickListener {

    private AppointmentsData appointments;
    CalendarView calendar;
    Date date = null;

    @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);



        calendar.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
            public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
                date = new Date(year, month, dayOfMonth);
            }
        });

    }

    @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);
        i.putExtra(DATE, date.getTime());
        startActivity(i);
        break;
        case R.id.btn_viewEdit:
            i = new Intent(this, EditViewAppointment.class);
            i.putExtra(DATE, date.getTime());
            startActivity(i);
        break;
        case R.id.btn_move:
            i = new Intent(this, MoveAppointment.class);
            i.putExtra(DATE, date.getTime());
            startActivity(i);
        break;
        case R.id.btn_delete:
            i = new Intent(this, DeleteAppointment.class);
            i.putExtra(DATE, date.getTime());
            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);
            i.putExtra(DATE, date.getTime());
            startActivity(i);
        break;
        case R.id.exit:
            finish();
        break;

        }

    }

}

而其他活动使用的值:

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;
    Intent fetchDate = getIntent();


    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";
        long dateFecth = fetchDate.getLongExtra(DATE, defaultValue);
    }

    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;

        }

    }

}

该错误是在long dateFecth = fetchDate.getLongExtra(DATE, defaultValue); 行,我不知道是干什么用的第二个参数,因为一切我想给我一个错误。

是否有人可以帮助我吗?

Answer 1:

移动fetchDate = getIntent(); 内部onCreate活动作为方法:

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.create);
     //.your code here..
    fetchDate = getIntent();  // get intent here from previous Activity
    long dateFecth = fetchDate.getLongExtra(DATE, defaultValue);
}


Answer 2:

首先检查 - 值与给定名称关联。 如果你不希望使用默认值尝试捆绑如下

if (getchDate.hasExtra("Date"))  {
Bundle bundle = new Bundle();
bundle =  getchDate.getExtras();
Object object = bunlde.get(Date);
Now parse this object in your desire type.

}


Answer 3:

你应该检查的结果getIntent()是不是第一个空(当然一个的电话应该是里面onCreate ,没有构造函数)。 如果不是空的,那么从它那里得到长变量。 defaultValue表示从一个方法返回一个值,如果未找到指定键的值。



文章来源: How Do I Get Extra From an Intent (as a type Long)?