简体   繁体   English

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

[英]I can't update an item on my list view using item id.

I have a list view with data base and when i click on an item on the list i want to move to edit page activity in order to update the item. 我有一个带有数据库的列表视图,当我单击列表上的一个项目时,我想移动以编辑页面活动以更新该项目。 What am i doing wrong? 我究竟做错了什么?

There is my code: 有我的代码:

//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();
    }
}

When you want to pass so much data through an activity, it is best to bind that data into a bundle. 当您要通过活动传递大量数据时,最好将这些数据绑定到捆绑中。 You should store all your data into a bundle and then bind it to the intent and then pass in the activity. 您应该将所有数据存储到捆绑中,然后将其绑定到意图,然后传递活动。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我不能 select 列表视图中的项目 - I can't select an item in list view 无法使用房间数据库中的视图 model 更新我的 recyclerView,每个项目都会编辑前一个,而不是它在 recyclerView 中的项目 ID - Can't update my recyclerView with view model in room database, every item edits the previous one, not its item id in recyclerView 如何为列表中的每个项目添加文本视图? - How can I add a text view for every item in my list? 无法在我的列表视图中访问项目元素 - Item element can not be accessed on my list view 无法更新列表视图中的项目视图 - Cannot update item view in a list view 如何在Android的列表视图中更新列表项 - how to update list item in list view in android 如何从我的列表视图项上的警报对话框(单选)中设置选定的值? - how can i set selected value from alert dialog (single selection) on my list view item? 无法更新我的列表视图的数据 - Can't update data for my list view 无法从另一个片段将项目添加到我的回收站视图 - can't add item to my recycler view from another fragment 无法成功从数据库中检索微调框选择的项目文本以使用光标填充我的应用程序中的联系人列表项目 - Can't successfully retrieve Spinner selected item text from database to fill contact list item in my app using cursor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM