添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

如何限制在Android自定义列表视图中显示的字符数,该列表视图用于多个活动?

0 人关注

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

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

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

这里是我的自定义ListView适配器是。

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;

我搜索了一下,但没有找到。希望得到任何帮助。

java
android
android-studio
Badri Paudel
Badri Paudel
发布于 2020-08-25
1 个回答
Petrus Nguyễn Thái Học
Petrus Nguyễn Thái Học
发布于 2020-08-25
已采纳
0 人赞同

Kotlin code:

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