处理从Singleton类添加,编辑和删除操作

时间:2014-01-29 08:32:53

标签: android singleton android-edittext

我有一个Singleton类,看起来像;

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

现在,我想要点击特定项目,它应该执行它的特定任务添加新的,编辑信息和从数据库删除。

添加;

   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

用于编辑;

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

现在我想要的是当行动是ACTION_EDIT时,将currentTrainee的值自动传递给另一个活动中的edittext字段。

0 个答案:

没有答案