I'm writing a MEMO app, but there have some problum When I want to edit MEMO, whatever item I choose , it always only change the first item, It only got the item number in front of the string Like this I want the result that I can change any item which I click
This is my code:
public class d_Game_message extends AppCompatActivity
implements AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener {
private static final int REQUEST_CODE_EDIT = 1;
String[] aMemo ={
"1.按一下可以編輯備忘",
"2.長按可以清除備忘","3.","4.","5.","6."};
ListView lv;
ArrayAdapter<String> aa;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dgame_message);
lv = (ListView)findViewById(R.id.listView);
aa = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, aMemo);
lv.setAdapter(aa);
lv.setOnItemClickListener(this);
lv.setOnItemLongClickListener(this);
@Override
public void onItemClick(AdapterView<?> a, View v, int pos, long id) {
Intent it = new Intent(this, d_Game_message_edit.class);
it.putExtra("requestCode",pos); //附加編號
it.putExtra("memo", aMemo[pos]); //附加備忘項目的內容
activityResultLaunch.launch(it);
}
@Override
public boolean onItemLongClick(AdapterView<?> a, View v, int pos, long id) {
aMemo[pos] = (pos 1) "."; //將內容清除 (只剩編號)
aa.notifyDataSetChanged(); //通知 Adapter 要更新陣列內容
return true; //傳回 true 表示此事件已處理
}
ActivityResultLauncher<Intent> activityResultLaunch = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
if (result.getResultCode() == RESULT_OK) {
Intent data = result.getData();
int requestCode = data.getIntExtra("requestCode", 0);
aMemo[requestCode] = data.getStringExtra("memo");
aa.notifyDataSetChanged();
}
}
});
This is edit activity
public class d_Game_message_edit extends AppCompatActivity {
TextView txv;
EditText edt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dgame_message_edit);
Intent it = getIntent();
String s = it.getStringExtra("memo");
txv = (TextView)findViewById(R.id.textView);
txv.setText(s.substring(0, 2));
edt = (EditText)findViewById(R.id.editText);
if(s.length() > 3)
edt.setText(s.substring(2));
}
public void onCancel(View v) {
setResult(RESULT_CANCELED);
finish();
}
public void onSave(View v) {
Intent it2 = new Intent();
it2.putExtra("memo", txv.getText() " " edt.getText());
setResult(RESULT_OK, it2);
finish();
}
}
I am a novice, thanks a lot for the help!!
CodePudding user response:
In Activity
in onSave
you are setting "memo"
text, but you are not returning your "requestCode"
, thus int requestCode = data.getIntExtra("requestCode", 0);
is always 0
(as 0
set as default param when lack of "requestCode"
key), so edited item will always be this one on position = 0.
Store passed "requestCode"
in d_Game_message_edit
Activity
(read it in onCreate
)
...
private int position = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dgame_message_edit);
Intent it = getIntent();
String s = it.getStringExtra("memo");
position = it.getIntExtra("requestCode");
...
and return it in same Intent
next to "memo"
text when onSave
runs
public void onSave(View v) {
Intent it2 = new Intent();
it2.putExtra("memo", txv.getText() " " edt.getText());
it2.putExtra("requestCode", position);
setResult(RESULT_OK, it2);
finish();
}
And change "requestCode"
naming, it may be confusing...