I have 2 activities, one of them has a ListView and the other one has a Multiline Text in it. Every time that I change something in Multiline Text, an Item will be added to ListView and when I click on that item, it takes me to the second activity to edit my text. the problem is that the whole text adds to the ListView item. I can create a short version of my text (beginning 10-20 characters) but when I do that, the Multiline Text value also changes. how can I fix it?
My main activity code:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
static ArrayList<String> notelist = new ArrayList<String>();
static ArrayAdapter notelistAdapter;
ListView noteslistView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
noteslistView = (ListView) findViewById(R.id.noteslist);
notelist.add("My Note");
notelistAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, notelist);
noteslistView.setAdapter(notelistAdapter);
noteslistView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent addedit = new Intent(getApplicationContext(), EditNote.class);
addedit.putExtra("noteID", position);
startActivity(addedit);
}
});
}
}
My EditNote activity code:
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
public class EditNote extends AppCompatActivity implements TextWatcher {
int noteID;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_note);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
EditText editText = (EditText) findViewById(R.id.edittext);
editText.setGravity(Gravity.TOP);
Intent addedit = getIntent();
noteID = addedit.getIntExtra("noteID", 0);
if (noteID != -1)
{
editText.setText(MainActivity.notelist.get(noteID));
}
editText.addTextChangedListener(this);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
MainActivity.notelist.set(noteID, String.valueOf(s));
MainActivity.notelistAdapter.notifyDataSetChanged();
}
@Override
public void afterTextChanged(Editable s) {
}
}