在由ArrayList填充的ListView中,如何限制每个列表项的字符数或将每个列表项限制为仅一行文本?这是一个笔记应用程序。我的所有代码都在下面。
的ListView:
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:ellipsize="end"
android:id="@+id/listView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:singleLine="true"/>
MainActivity.java:
public class MainActivity extends AppCompatActivity {
static ArrayList<String> note;
ListView listView;
static ArrayList<String> notes = new ArrayList<>();
static ArrayAdapter arrayAdapter;
static Set<String> set;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
setTitle("Notely");
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
notes.add("");
SharedPreferences sharedPreferences = getSharedPreferences("mypackage", Context.MODE_PRIVATE);
if (set == null) {
set = new HashSet<String>();
} else {
set.clear();
}
set.addAll(notes);
arrayAdapter.notifyDataSetChanged();
sharedPreferences.edit().remove("notes").apply();
sharedPreferences.edit().putStringSet("notes", set).apply();
Intent i = new Intent(getApplicationContext(), EditNote.class);
i.putExtra("noteId", notes.size() - 1);
startActivity(i);
}
});
ListView listView = (ListView) findViewById(R.id.listView);
SharedPreferences sharedPreferences = this.getSharedPreferences("mypackage", Context.MODE_PRIVATE);
set = sharedPreferences.getStringSet("notes", null);
notes.clear();
if (set != null) {
notes.addAll(set);
} else {
notes.add("Example note");
set = new HashSet<String>();
set.addAll(notes);
sharedPreferences.edit().putStringSet("notes", set).apply();
}
arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, notes);
listView.setAdapter(arrayAdapter);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i = new Intent(getApplicationContext(), EditNote.class);
i.putExtra("noteId", position);
startActivity(i);
}
});
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
new AlertDialog.Builder(MainActivity.this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Are you sure?")
.setMessage("Do you want to delete this note?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
notes.remove(position);
SharedPreferences sharedPreferences = MainActivity.this.getSharedPreferences("mypackage", Context.MODE_PRIVATE);
if (set == null) {
set = new HashSet<String>();
} else {
set.clear();
}
set.addAll(notes);
sharedPreferences.edit().remove("notes").apply();
sharedPreferences.edit().putStringSet("notes", set).apply();
arrayAdapter.notifyDataSetChanged();
}
})
.setNegativeButton("No", null)
.show();
return true;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
return super.onOptionsItemSelected(item);
}
}
EditNote.java
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);
setTitle("Write a Note");
EditText editText = (EditText) findViewById(R.id.editText);
Intent i = getIntent();
noteId = i.getIntExtra("noteId", -1);
if (noteId != -1) {
editText.setText(MainActivity.notes.get(noteId));
}
editText.addTextChangedListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
return super.onOptionsItemSelected(item);
}
@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.notes.set(noteId, String.valueOf(s));
MainActivity.arrayAdapter.notifyDataSetChanged();
SharedPreferences sharedPreferences = getSharedPreferences("mypackage", Context.MODE_PRIVATE);
if (MainActivity.set == null) {
MainActivity.set = new HashSet<String>();
} else {
MainActivity.set.clear();
}
MainActivity.set.addAll(MainActivity.notes);
sharedPreferences.edit().remove("notes").apply();
sharedPreferences.edit().putStringSet("notes", MainActivity.set).apply();
}
@Override
public void afterTextChanged(Editable s) {
}
}
答案 0 :(得分:1)
您目前正在为android.R.layout.simple_list_item_1
中的每个项目使用内置布局(List View
)。听起来你想要自定义它。因此,您需要使用具有适当属性的<TextView>
创建自己的布局,包括
android:maxLines="1"
android:ellipsize="end"
答案 1 :(得分:0)
将这些设置应用于ListView本身时,将maxLines设置为1并将ellipsize设置为end不起作用。相反,您有一个为ListView项目充气的布局。我假设它包含一个TextView,因为你想要一个带省略号的行而不是一个内容垂直增长的TextView。将这些行添加到List项目布局中的TextView。同样,此布局是您为每个ListView项目充气的布局。
android:maxLines="1"
android:ellipsize="end"
然后,您将使用此布局来创建适配器。另请注意,您拨打setAdapter()
两次,这是不必要的。