-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) {
}
}
我們需要查看一些代碼,以確定如何保存筆記和填充ListView。然而,你可以從EditText得到第一個'n'字符,就像這樣:'String shortNote = editText.getText()。toString()。substring(0,Math.min(editText.getText()。length ),n));' –
搜索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) –