如何通过将对象的UUID传递给新活动来检索对象的信息(标题,desc)

时间:2016-08-27 20:25:38

标签: android android-intent

MainActivity.java

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private EditText title;
    private EditText desc;
    private Button button;
    private String mTitle;
    private String mDesc;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        title = (EditText) findViewById(R.id.note_title);
        desc = (EditText) findViewById(R.id.note_desc);
        button = (Button) findViewById(R.id.add_new_item_button);
        button.setOnClickListener(this);

        mTitle = title.getText().toString();
        mDesc = desc.getText().toString();
    }

    @Override
    public void onClick(View view) {
        Note note = new Note();
        note.setTitle(mTitle);
        note.setDesc(mDesc);
        Intent intent = new Intent(this, ReceiveActivity.class);
        Log.d("Test", "From Main " + note.getId());
        intent.putExtra("ID", note.getId());
        startActivity(intent);
    }
}

ReceiveActivity

public class ReceiveActivity extends AppCompatActivity {
    private TextView titleTextView;
    private TextView descTextView;
    private Note mNote;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_receive);

        titleTextView = (TextView) findViewById(R.id.receiving_title);
        descTextView = (TextView) findViewById(R.id.receiving_desc);

        Intent intent = getIntent();
        UUID mId = (UUID) intent.getSerializableExtra("ID");
        Log.d("Test", "From Receive" + mId);
        mNote = new Note(mId);
        Log.d("Test", "ID " + mNote.getId());
        Log.d("Test", "TITLE " + mNote.getTitle());
        titleTextView.setText(mNote.getTitle());
        descTextView.setText(mNote.getDesc());
    }
}

Note.java

public class Note {

    private UUID mId;
    private String Title;
    private String Desc;

    public Note(){
        mId = UUID.randomUUID();
    }

    public Note(UUID id){
        mId = id;
    }

    public UUID getId(){
        return mId;
    }

    public void setId(UUID id){
        mId = id;
    }

    public String getTitle() {
        return Title;
    }

    public void setTitle(String title) {
        Title = title;
    }

    public String getDesc() {
        return Desc;
    }

    public void setDesc(String desc) {
        Desc = desc;
    }
}

Logcat

mynotes D/Test: From Receive 2947b104-8489-4f92-aaff-71608180cfe2
mynotes D/Test: ID 2947b104-8489-4f92-aaff-71608180cfe2
mynotes D/Test: TITLE null
mynotes D/Test: From Main bc13e67c-ae16-4041-b3d5-54b3208fc983
mynotes D/Test: From Receive bc13e67c-ae16-4041-b3d5-54b3208fc983
mynotes D/Test: ID bc13e67c-ae16-4041-b3d5-54b3208fc983
mynotes D/Test: TITLE null

我正在尝试将UUID从对象传递到另一个活动。当我得到Intent时,我收到了UUID,但没有收到对象的属性。如何检索对象的标题和描述?

1 个答案:

答案 0 :(得分:0)

可以在接收活动中作为捆绑接收数据。在RecieveActivity中执行以下操作:

@Override 
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_receive);

    titleTextView = (TextView) findViewById(R.id.receiving_title);
    descTextView = (TextView) findViewById(R.id.receiving_desc);

    Bundle bundle = getIntent().getExtras();
    UUID mId = (UUID) bundle.getSerializableExtra("ID");
    Log.d("Test", "From Receive" + mId);
    mNote = new Note(mId);
    Log.d("Test", "ID " + mNote.getId());
    Log.d("Test", "TITLE " + mNote.getTitle());
    titleTextView.setText(mNote.getTitle());
    descTextView.setText(mNote.getDesc());
}