如何限制在多个活动中使用的Android自定义ListView中显示的字符数



我正在从SQLite数据库中获取数据,并将其添加到我的自定义列表视图中,该视图在多个活动中使用

在用户看到的第一个屏幕中,它有完整标题和显示的完整描述,但我希望限制标题[或仅一行]和描述[或最多两行]中显示的字符数。

我知道如果我只使用过一次自定义列表视图,我就可以做一些事情,比如只显示标题或描述的子字符串。但问题是,我在多个地方使用列表视图,我不想在其他活动中看到这种行为。相反,对于这个活动,我想要的是在点击特定列表项时获得完整的标题和描述,我已经这样做了。

这是我的customListView适配器是:

public class MyCustomNotesAdapter extends BaseAdapter {
Context context;
ArrayList<Note> noteList;
public MyCustomNotesAdapter(Context context, ArrayList<Note> noteList) {
this.context = context;
this.noteList = noteList;
}
@Override
public int getCount() {
return this.noteList.size();
}
@Override
public Object getItem(int position) {
return noteList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View view, ViewGroup viewGroup) {
//inflate our custom listview
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.custom_notes_listview, null);
TextView title_text =  view.findViewById(R.id.note_title);
TextView desc_text =    view.findViewById(R.id.note_desc);

//Button update_btn =  view.findViewById(R.id.update_note_button);
Note note = noteList.get(position);

title_text.setText(note.getTitle()); //note.getTitle().substring(beginIndex, endIndex) doesn't work for my case.
desc_text.setText(note.getDescription());
return view;
}
}

我使用这个的活动是:

.................. other codes ......        
//display notes of the logged in user
listView = findViewById(R.id.listView);
myNotesDatabaseHelper = new MyNotesDatabaseHelper(AllNotesScreenActivity.this);
final List<Note> allNotes = 
myNotesDatabaseHelper.getAllNotes(myNotesDatabaseHelper.getIdFromUsername(username));
if (allNotes.size() <= 0)
Toast.makeText(this, "You have no notes , please create note.", Toast.LENGTH_SHORT).show();
//array adapter
myCustomNotesAdapter = new MyCustomNotesAdapter(AllNotesScreenActivity.this, (ArrayList<Note>) allNotes);
listView.setAdapter(myCustomNotesAdapter);
//handle delete on long click listener
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
//logic  to delete item
final Note clickedNote = (Note) adapterView.getItemAtPosition(i);
//alert dialog for deleting your note on tapping
AlertDialog.Builder deleteNoteAlertDialog = new AlertDialog.Builder(
AllNotesScreenActivity.this);
//initializng  alert dialog
alertDialog = new Alert("Delete Note !", "Do you want to delete this note permanently ? [ can't be undo ]");
// Setting Dialog Title
deleteNoteAlertDialog.setTitle(alertDialog.getAlertTitle());
// Setting Dialog Message
deleteNoteAlertDialog.setMessage(alertDialog.getAlertMessage());
// Setting Icon to Dialog
deleteNoteAlertDialog.setIcon(R.drawable.delete);
// Setting Positive "Yes" Btn
deleteNoteAlertDialog.setPositiveButton("YES",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
boolean success = myNotesDatabaseHelper.deleteOneNote(clickedNote);
if (!success) {
Toast.makeText(AllNotesScreenActivity.this, "Couldn't be deleted your note. ", Toast.LENGTH_SHORT).show();
return;
}
Toast.makeText(AllNotesScreenActivity.this, "Note Deleted Successfully ", Toast.LENGTH_LONG).show();
Intent intent = new Intent(getApplicationContext(), AllNotesScreenActivity.class);
intent.putExtra("username", username);
startActivity(intent);
}
});
// Setting Negative "NO" Btn
deleteNoteAlertDialog.setNegativeButton("NO",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// Showing Alert Dialog
deleteNoteAlertDialog.show();
return true;
}
});

我找过了,但找不到。如有帮助,不胜感激。

Kotlin代码:

textView.ellipsize = TextUtils.TruncateAt.END
textView.maxLines = 2 // or = 1

Java代码:

textView.setEllipsize(TextUtils.TruncateAt.END);
textView.setMaxLines(2); // or setMaxLines(1)

最新更新