我已经构建了一个用于执行应用程序的应用程序,该应用程序允许我添加以将项目添加到listView中,然后在长时间单击中删除这些项目并编辑它们。但是,我不能编辑超过1个项目。我的第一个编辑作品和该项目已更新,但是之后,我尝试编辑的任何其他项目保持不变。
这是我的班级负责编辑:
public class EditItemActivity extends AppCompatActivity {
EditText editToDo;
String itemPos;
Button saveChanges;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_item);
editToDo = (EditText) findViewById(R.id.edit_todo_item);
saveChanges = (Button)findViewById(R.id.save_changes);
String item = getIntent().getStringExtra("item");
editToDo.setText(item);
itemPos = getIntent().getStringExtra("itemPos");
saveChanges.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onSubmit();
}
});
}
public void onSubmit() {
Intent item = new Intent();
item.putExtra("item", editToDo.getText().toString());
item.putExtra("itemPos", itemPos);
setResult(RESULT_OK, item);
finish();
}
}
startActivityForResult
及其相应的方法在我的主要活性中调用:
public class MainActivity extends AppCompatActivity {
private final int REQUEST_CODE = 10;
ArrayList <String> toDoItems = new ArrayList<>();
ArrayAdapter<String> adapter;
ListView listItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listItems = (ListView) findViewById(R.id.listViewItems);
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, toDoItems);
listItems.setAdapter(adapter);
listItems.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapter, View item, int position, long id) {
toDoItems.remove(position);
MainActivity.this.adapter.notifyDataSetChanged();
return true;
}
});
listItems.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View item, int pos, long id) {
Intent intent = new Intent(MainActivity.this, EditItemActivity.class);
intent.putExtra("item", toDoItems.get(pos));
intent.putExtra("itemPos", String.valueOf(pos));
startActivityForResult(intent, REQUEST_CODE);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
String item = data.getStringExtra("item");
int itemPosition = Integer.parseInt(data.getStringExtra("itemPos"));
toDoItems.set(itemPosition, item);
adapter.notifyDataSetChanged();
}
}
public void addItem(View v){
EditText newItem = (EditText) findViewById(R.id.itemInputEditText);
String item = newItem.getText().toString();
adapter.add(item);
newItem.setText("");
}
}
基本上我所做的就是请oğuzhanDöngül的建议并加入其中。因此,在我的onActivityResult
方法中,这就是我所做的:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
String item = data.getStringExtra("item");
int itemPosition = Integer.parseInt(data.getStringExtra("itemPos"));
toDoItems.add(itemPosition, item);
toDoItems.remove(itemPosition + 1);
adapter.notifyDataSetChanged();
}
}