在listview中显示edittext值的开头字符

时间:2017-02-13 19:02:31

标签: android

我正在创建一个笔记应用程序,我在MainActivity中有一个listView用于笔记列表。另外,我不打算为音符主题添加一个部分,以便在您返回时自动保存。但问题是它将整个文本保存在listView中。如何更改它以便它只显示10-20个起始字符?

编辑:我可能需要更改代码的某些部分,因为当我更改listView项时,文本也会更改。我的代码:

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活动:

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

    }
}

1 个答案:

答案 0 :(得分:0)

使用适配器中显示字符串的String#substring()方法。

返回字符串的前14个字符并在其中附加“...”的示例:

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

这将返回Really long ti...