简体   繁体   English

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

[英]How To Limit The Number of Characters displayed In Android Custom ListView which is used in Multiple activities?

I'm fetching the data from SQLite database and adding it to my custom listview which is used in multiple activities.我正在从 SQLite 数据库中获取数据并将其添加到我的自定义列表视图中,该列表视图用于多个活动。

In the first screen that user sees it has full title and its full description displayed , but what I want is to limit the number of characters displayed in the title [ or just one line ] and description [ or maximum two lines ].在用户看到的第一个屏幕中,它显示了完整的标题和完整的描述,但我想要的是限制标题[或仅一行]和描述[或最多两行]中显示的字符数。

I know if I just had used that custom listview only once I could have done something like just display the substring of the title or description.我知道如果我只使用过一次自定义列表视图,我可以做一些事情,比如只显示标题或描述的子字符串。 But the problem is I'm using that listview in multiple places and I don't want to see that behaviour in other activities.但问题是我在多个地方使用该列表视图,我不想在其他活动中看到这种行为。 Instead, for this activity what I want is to get the full title and description when clicked on that particular list item and I have already done this.相反,对于此活动,我想要的是在单击该特定列表项时获得完整的标题和描述,而我已经完成了此操作。

Here is my customListView Adapter is :这是我的 customListView Adapter 是:

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;
}
}

And the activity where Im using this is :我使用它的活动是:

 .................. 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;
        }
    });

I searched for it but I couldn't find it.我搜索了它,但找不到。 Any help is appreciated.任何帮助表示赞赏。

Kotlin code:科特林代码:

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

Java code:爪哇代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM