简体   繁体   中英

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.

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 :

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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