2017-02-13 68 views
-2

我正在创建一个笔记应用程序,我的MainActivity中有一个listView列表视图。另外,我不会为笔记主题添加部分,所以当您返回时它会自动保存。但问题是它将整个文本保存在listView中。我怎样才能改变它,所以它只显示10-20个首字符?在列表视图中显示edittext值的开始字符

编辑:我可能必须更改我的代码的某些部分,因为当我更改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) { 

    } 
} 
+2

我们需要查看一些代码,以确定如何保存笔记和填充ListView。然而,你可以从EditText得到第一个'n'字符,就像这样:'String shortNote = editText.getText()。toString()。substring(0,Math.min(editText.getText()。length ),n));' –

+1

搜索String.substring()方法! (http://stackoverflow.com/help/how-to-ask)[page2](http://stackoverflow.com/help/mcve)[page3](http://stackoverflow.com/help/how-to-ask)[page2] stackoverflow.com/help/on-topic) –

回答

0

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

返回您的字符串的前14个字符并追加 “...”,将

例子:

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

这将返回Really long ti...