How Do I Get Extra From an Intent (as a type Long)?

╄→гoц情女王★ 提交于 2019-12-14 02:43:25

问题


I have this problem accessing an extra from an intent. The value i'm parsing is a long type, and I need this value to be stored in a database.

So this is what I have so far:

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;

        }

    }

}

And the other Activity to use the value:

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;

        }

    }

}

The error is on the long dateFecth = fetchDate.getLongExtra(DATE, defaultValue); line and I don't know what to use for the second argument, since everything I think of gives me an error.

Can someone please help me with this?


回答1:


Move fetchDate = getIntent(); inside onCreate method of Activity as:

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



回答2:


First check - Value is associated with the given name. if you don't want to use defaultValue try with bundle as follow

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

}



回答3:


You should check if result of getIntent() is not empty at first (an of course the call should be inside onCreate, not constructor). If it's not empty, then get long variable from it. defaultValue stands for a value returned from a method if a value for specified key is not found.



来源:https://stackoverflow.com/questions/22485072/how-do-i-get-extra-from-an-intent-as-a-type-long

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!