繁体   English   中英

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

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

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

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

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

这是我的 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;
}
}

我使用它的活动是:

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

我搜索了它,但找不到。 任何帮助表示赞赏。

科特林代码:

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

爪哇代码:

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