[英]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.