0

I have a Singleton class, that looks like;

public class SharedMemory {

private Training currentTraining;
private Trainee currentTrainee;
private String currentAction;

public static final String ACTION_ADD= "add";
public static final String ACTION_EDIT = "edit";
public static final String ACTION_DELETE = "delete";


private static SharedMemory instance;

private SharedMemory() {

}

public static SharedMemory getInstance() {
    if (instance == null)
        instance = new SharedMemory();
    return instance;

}

public Training getCurrentTraining() {
    return currentTraining;
}

public void setCurrentTraining(Training currentTraining) {
    this.currentTraining = currentTraining;
}

public Trainee getCurrentTrainee() {
    return currentTrainee;
}

public void setCurrentTrainee(Trainee currentTrainee) {
    this.currentTrainee = currentTrainee;
}

public String getCurrentAction() {
    return this.currentAction;
}

public void setCurrentAction(String currentAction) {
    this.currentAction = currentAction;
}}

Now, I want On click to the particular items, it should perform it's specific tasks add new, edit information and delete to and from database.

For Add;

   Button addnew = (Button) findViewById(R.id.add_btn);
    addnew.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // Add new Trainee
            SharedMemory.getInstance().setCurrentAction(
                    SharedMemory.ACTION_ADD);
            SharedMemory.getInstance().setCurrentTrainee(new Trainee());

            Intent intent = new Intent(TraineeActivity.this,
                    FormActivity.class);

            TraineeActivity.this.startActivity(intent);
        }
    });

    traineesList

For edit;

traineesListView
            .setOnItemLongClickListener(new OnItemLongClickListener() {

                @Override
                public boolean onItemLongClick(AdapterView<?> arg0,
                        View arg1, int arg2, long arg3) {
                    currentTrainee = (Trainee) arg0.getAdapter().getItem(
                            arg2);

                    String items[] = { "Edit your information",
                            "Unregister from training" };
                    Builder alert = new AlertDialog.Builder(
                            TraineeActivity.this);
                    alert.setTitle("Dear!" + " "
                            + currentTrainee.getFormatedName() + "," + " "
                            + "Choose your action!!");
                    alert.setItems(items, new OnClickListener() {

                        @Override
                        public void onClick(DialogInterface arg0,
                                int selectedItem) {

                            if (selectedItem == 0) {

                                SharedMemory.getInstance()
                                        .setCurrentAction(
                                                SharedMemory.ACTION_EDIT);
                                SharedMemory.getInstance()
                                        .setCurrentTrainee(currentTrainee = new Trainee());

                                Intent intent = new Intent(
                                        TraineeActivity.this,
                                        FormActivity.class);
                                TraineeActivity.this.startActivity(intent);

                            } else if (selectedItem == 1) {
                                // delete trainee information
                            }

                        }

                    });
                    alert.show();
                    return true;
                }

Now what I want is to pass the values of currentTrainee to the edittext fields automatically in another activity, when the action is ACTION_EDIT.

bShah
  • 201
  • 1
  • 6
  • 17
  • What isn't working? What steps did you take to pass that information? What happened? No one will be able to help you until you post more details regarding exactly what you did, what you expected to happen, and what actually happened. – PaF Jan 29 '14 at 09:56
  • Those strings "add", "edit", "delete" in the sharedmemory class does not make any effect. I want to make some specific action. Like if i click the button, it should open another activity and should know that it is time to add new trainee, not edit or not delete. – bShah Jan 29 '14 at 10:25
  • You defined those strings, but have done nothing else with them. If you want to learn about passing information to a new activity, I recommend you read [this](http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android) question, or [this](http://stackoverflow.com/questions/8090747/how-to-send-data-to-new-activity-from-current-activity-via-intent) question. – PaF Jan 29 '14 at 11:25
  • @PaF Please check my edited question. I was implementing wrong way. Now I solved half of it. But still I have some problem. – bShah Jan 29 '14 at 12:00
  • Why are you assigning to currentTrainee in `SharedMemory.getInstance().setCurrentTrainee(currentTrainee = new Trainee());` ? Didn't you mean to just pass the trainee that you retrieved in the beginning of the function, like `SharedMemory.getInstance().setCurrentTrainee(currentTrainee);` ? – PaF Jan 29 '14 at 12:24
  • @PaF Yeah I did that. But still I am having nullPointer Exception. I another class; I did; currentTrainee = SharedMemory.getInstance.getCurrentTrainee; And pass values like; LastNameEditText.setText(currentTrainee.getLastname()); But still cannot get value. – bShah Jan 29 '14 at 12:26
  • Also, where is `currentTrainee` defined (the one referenced in `onItemLongClick`)? If this whole definition is within the implementation of `SharedMemory`, then by doing `currentTrainee = (Trainee) arg0.getAdapter().getItem(arg2);` in the beginning of the function, you're already doing the same as calling `setCurrentTrainee()`. – PaF Jan 29 '14 at 12:26
  • Yeah, you are right!! – bShah Jan 29 '14 at 12:28
  • If the problem is a `NullPointerException`, please paste all relevant code, and the exception details from logcat. – PaF Jan 29 '14 at 12:34
  • @PaF Thanks for the support. I did it!! – bShah Jan 29 '14 at 12:51
  • No problem, happy to assist. – PaF Jan 29 '14 at 13:04

0 Answers0