-2

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) {

    }
}
Amir Amini
  • 51
  • 1
  • 1
  • 7
  • 2
    We need to see some code for exactly how you're saving the notes and populating the ListView. However, you can just get the first `n` characters from the EditText doing something like this: `String shortNote = editText.getText().toString().substring(0, Math.min(editText.getText().length(), n));` – Andrew Brooke Feb 13 '17 at 19:05
  • 1
    Search for String.substring() method! and please read these: [page1](http://stackoverflow.com/help/how-to-ask) [page2](http://stackoverflow.com/help/mcve) [page3](http://stackoverflow.com/help/on-topic) – Atef Hares Feb 13 '17 at 19:06

1 Answers1

0

Use the String#substring() method where you display the string in your Adapter.


Example for returning the first 14 characters of your string and appending "..." to it:

String titleSubstring = "Really long title to shorten".substring(0, 14) + "...";

This will return Really long ti....

Niklas Wenzel
  • 888
  • 7
  • 6