按下按钮

时间:2016-06-19 10:25:54

标签: android

我正在使用共享偏好设置记笔记。但是,当我单击listItem进行编辑,然后再次单击它时,它不会被更新。

我的MainActivity列表视图:

ListView notesListView;
static ArrayList<String> notesArrayList;
static ArrayAdapter<String> notesAdapter;

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

    notesArrayList = new ArrayList<String>();

    notesListView = (ListView) findViewById(R.id.listView);

    notesArrayList.add("Example");

    notesAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, notesArrayList);

    notesListView.setAdapter(notesAdapter);

    notesListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent = new Intent(getApplicationContext(), EditNote.class);
            intent.putExtra("position", position);
            startActivity(intent);
        }
    });

我的编辑笔记活动:

EditText userNote;
int noteId;

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

    userNote = (EditText) findViewById(R.id.editTextNote);

    Intent intent = getIntent();
    noteId = intent.getIntExtra("position", -1);
    if(noteId != -1){

        userNote.setText(MainActivity.notesArrayList.get(noteId));

    }
    userNote.addTextChangedListener(this);

}

@Override
public void onBackPressed() {
    Intent goBack = new Intent(EditNote.this, MainActivity.class);
    startActivity(goBack);
    finish();
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
        MainActivity.notesArrayList.set(noteId, String.valueOf(s));
        MainActivity.notesAdapter.notifyDataSetChanged();
}

@Override
public void afterTextChanged(Editable s) {

}
}

我认为问题在于编辑注释中的onBackPressed。但是我想要一个解决方案,因为我找不到一个。

1 个答案:

答案 0 :(得分:1)

首先在你的

@Override
public void onBackPressed() {
    Intent goBack = new Intent(EditNote.this, MainActivity.class);
    startActivity(goBack);
    finish();
}

将其更改为此

  @Override
    public void onBackPressed() {  
        super.onBackPressed();
        this.finish();
    }

因为当您已经使用MainActivity.java方法进行MainActivity.java时,为什么还要再次启动onBackPressed()