I'm creating a notes app and I have a listView in my MainActivity for notes list. Also, I'm not going to add a section for the note subject so it'll save automatically when you go back. but the problem is that it saves the whole text in the listView. how can I change it so it only shows the 10-20 starting characters?
EDIT: I may have to change some parts of my code because when I change a listView item, the text changes too. my codes:
MainActivity:
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);
}
});
}
}
EditNote Activity:
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) {
}
}