我无法使用商品ID更新列表视图中的商品。

时间:2014-03-19 09:15:52

标签: android

我有一个包含数据库的列表视图,当我点击列表中的某个项目时,我想移动到编辑页面活动以更新项目。 我做错了什么?

有我的代码:

//by pressing a short press on any item the user moved to the edit page in order to edit his note
lv.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        intent=new Intent(MainActivity.this,Edit_Note.class);

        intent.putExtra("item_id", list.get(position).getId());
        intent.putExtra("position", position);
        intent.putExtra("item_title", list.get(position).getTitle().toString());
        intent.putExtra("item_content", list.get(position).getContent().toString());

        startActivityForResult(intent, FLAG_FOR_EDITING);
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode==ADD_FLAG && resultCode==RESULT_OK ){
        //i am getting the information (title and content) from the edit note activity
        String the_title=data.getStringExtra("user_title");
        String the_content=data.getStringExtra("user_content");
        //the information is added to the database
        database.addNote(the_title,the_content);
    }
    else if(requestCode==ADD_FLAG && resultCode==RESULT_CANCELED){
        Toast.makeText(MainActivity.this, "no changes have been made", Toast.LENGTH_LONG).show();
    }
    else if
    (requestCode==FLAG_FOR_EDITING && resultCode==RESULT_OK){

        int position_from_edit=data.getIntExtra("position", -2);
        String title_editing=data.getStringExtra("user_title");
        String content_editing=data.getStringExtra("user_content");
        database.editNote("position_from_edit", title_editing, content_editing);

    }

    edit_title=(EditText)findViewById(R.id.edit_title);
    edit_content=(EditText)findViewById(R.id.edit_content);
    ImageButton save=(ImageButton)findViewById(R.id.save);
    ImageButton clear=(ImageButton)findViewById(R.id.clear);

    String received_title=intent.getStringExtra("item_title");
    String received_content=intent.getStringExtra("item_content");
    item_id=String.valueOf(intent.getIntExtra("item_id", -1));
    edit_title.setText(received_title);
    edit_content.setText(received_content);


    //by pressing the save button, the information is sending to the main activity page
    save.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            //i am receiving the text that the user entered to the title and to the content fields
            entered_title=edit_title.getText().toString();
            entered_content=edit_content.getText().toString();
            intent.putExtra("position", item_id);
            intent.putExtra("user_title", entered_title);
            intent.putExtra("user_content", entered_content);
            setResult(RESULT_OK, intent);
            finish();
    }
}

1 个答案:

答案 0 :(得分:0)

如果要通过活动传递如此多的数据,最好将该数据绑定到一个包中。您应该将所有数据存储到一个包中,然后将其绑定到intent,然后传入活动。

Bundle value=new Bundle;
value.putString("item_title", list.get(position).getTitle().toString());
value.putString("item_content", list.get(position).getContent().toString());
value.putInt("item_id", list.get(position).getId());
value.putInt("position", position);
intent.putExtras(value);

startActivityForResult(intent,0);