user1060251
user1060251

Reputation: 45

how to pass clicked item data to another activity

I have todolist app and it works fine.I can add and edit item to list view successfully. Here is my problem.When i click item,item's data have to pass edit activity but now it is empty like adding new item.What can ı do?

editItem.java

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

    editText1 = (EditText) findViewById(R.id.editText1);
    spinner = (Spinner) findViewById(R.id.prioritySpinner);
    datePicker = (DatePicker) findViewById(R.id.datePicker);
    toggleButton = (ToggleButton) findViewById(R.id.statusbutton);
    editButton = (Button) findViewById(R.id.editButton);
    cancelButton = (Button) findViewById(R.id.cancelButton);

    editButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // ArrayList<String> b = new ArrayList<String>();

            task = editText1.getText().toString();
            priorityLevel = spinner.getSelectedItem().toString();
            status = toggleButton.getText().toString();

            int day = datePicker.getDayOfMonth();
            int month = datePicker.getMonth() + 1;
            int year = datePicker.getYear();

            date = day + "/" + month + "/" + year;

            itemList.setName(task);
            itemList.setPriorityLevel(priorityLevel);
            itemList.setStatus(status);
            itemList.setDueDate(date);

            Intent okIntent = new Intent();
            okIntent.putExtra("editItem", new DataWrapper(itemList));

            setResult(Activity.RESULT_OK, okIntent);
            finish();
        }
    });

Upvotes: 0

Views: 1366

Answers (2)

yurezcv
yurezcv

Reputation: 1033

You should implement your itemList class from Serializable interface,

public class ItemList implements Serializable {
// your class
}

and then use in first activity:

okIntent.putExtra("editItem", itemList);

in second activity, to get intent data:

ListItem listItem = (ListItem) getIntent().getSerializableExtra("editItem");

Upvotes: 1

Rolf ツ
Rolf ツ

Reputation: 8781

When starting your edit Activity you should pass in some extra edit data.

Assuming your DataWrapper class can be used as 'extra', so it should extend Parcelable or Serializable. (Preferably use Parcelable because its faster and designed for Android, of course Serializable can be used but i don't recommend it)

Here is an example of adding some arguments to the Activity start Intent:

Intent i = new Intent(getApplicationContext(), edititem.class);
i.putExtra("editdata",new DataWrapper(clickeditem));
startActivityForResult(i);

Then in the edititem Activity get the data using:

Bundle extras = getIntent().getExtras();
if (extras != null) {
    //Edit mode
    DataWrapper value = extras.getParcelable("editdata"); //Or extras.getSerializable("editdata");
} else {
    //New mode
}

Rolf

Upvotes: 0

Related Questions