0

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

    }
}
Community
  • 1
  • 1
Amir Amini
  • 51
  • 1
  • 1
  • 7

1 Answers1

0

In your simple_list_item_1.xml, where you defined TextView for your ListView put something like:

    android:maxLines="1"
    android:singleLine="true"
    android:ellipsize="end"

Oops, I see, I was not precise enough. The layout mentioned is a reference to an built-in XML layout document that is part of the Android OS, rather than one of your own XML layouts. You can see the code of the layout at https://github.com/android/platform_frameworks_base/tree/master/core/res/res/layout

I suggest you to make layout of your own, include lines I suggested, put it in layout folder, and reference it in your code.

bobjan
  • 353
  • 1
  • 4
  • 11
  • where is it? I can't find it. it's not in "layout" directory – Amir Amini Feb 14 '17 at 11:02
  • Thanks for the answer, I created an xml file in my layout folder but my java file couldn't find it. if I'm doing something wrong, please tell me. – Amir Amini Feb 14 '17 at 19:07
  • Oops, I shouldn't have used "android" in "android.R.layout.simple_list_item_1"(I created this xml file as you said). and it worked. Thank you. – Amir Amini Feb 14 '17 at 19:13