如何正确获取列表视图标题并通过Toast显示?

时间:2017-08-04 16:01:16

标签: java android

我正在开发一个具有列表视图和适配器的笔记应用程序。我在每个列表视图项上都有一个上下文菜单实现,弹出删除选项。单击删除选项后,将显示警告对话框。我无法连接每个选定列表视图项的标题,并以吐司形式显示它。如何正确获取每个列表视图标题的标题并将其转换为吐司?我现在的逻辑给了我一个空指针异常。

我的主要活动

private Note mLoadedNote = null;
    public boolean onContextItemSelected(MenuItem item) {
            switch (item.getItemId()) {
                case R.id.edit:
                    break;
                case R.id.delete:
                    AlertDialog.Builder alertDialog = new AlertDialog.Builder(this)
                            .setTitle("delete note")
                            .setMessage("really delete the note?")
                    .setPositiveButton("YES", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if(mLoadedNote != null) {
                            Toast.makeText(MainActivity.this,  mLoadedNote.getTitle() + " is deleted", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(MainActivity.this,  "can not delete the note '" + mLoadedNote.getTitle() + "'", Toast.LENGTH_SHORT).show();
                        }

                    }
                    })
                        .setNegativeButton("NO", null); //do nothing on clicking NO button :P
                    alertDialog.show();
            }
            return super.onContextItemSelected(item);
        }

注意可序列化

public class Note implements Serializable {
    private long mDateTime; //creation time of the note
    private String mTitle; //title of the note
    private String mContent; //content of the note

    public Note(long dateInMillis, String title, String content) {
        mDateTime = dateInMillis;
        mTitle = title;
        mContent = content;
    }

    public void setDateTime(long dateTime) {
        mDateTime = dateTime;
    }

    public void setTitle(String title) {
        mTitle = title;
    }

    public void setContent(String content) {
        mContent = content;
    }

    public long getDateTime() {
        return mDateTime;
    }

    public String getDateTimeFormatted(Context context) {
        SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"
                , context.getResources().getConfiguration().locale);
        formatter.setTimeZone(TimeZone.getDefault());
        return formatter.format(new Date(mDateTime));
    }

    public String getTitle() {
        return mTitle;
    }

    public String getContent() {
        return mContent;
    }
}

注意适配器类

public class NoteAdapter extends ArrayAdapter<Note> {

    public static final int WRAP_CONTENT_LENGTH = 50;
    public NoteAdapter(Context context, int resource, List<Note> objects) {
        super(context, resource, objects);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if(convertView == null) {
            convertView = LayoutInflater.from(getContext())
                    .inflate(R.layout.view_note_item, null);
        }

        Note note = getItem(position);

        if(note != null) {
            TextView title = (TextView) convertView.findViewById(R.id.list_note_title);
            TextView date = (TextView) convertView.findViewById(R.id.list_note_date);
            TextView content = (TextView) convertView.findViewById(R.id.list_note_content_preview);

            title.setText(note.getTitle());
            date.setText(note.getDateTimeFormatted(getContext()));

            //correctly show preview of the content (not more than 50 char or more than one line!)
            int toWrap = WRAP_CONTENT_LENGTH;
            int lineBreakIndex = note.getContent().indexOf('\n');
            //not an elegant series of if statements...needs to be cleaned up!
            if(note.getContent().length() > WRAP_CONTENT_LENGTH || lineBreakIndex < WRAP_CONTENT_LENGTH) {
                if(lineBreakIndex < WRAP_CONTENT_LENGTH) {
                    toWrap = lineBreakIndex;
                }
                if(toWrap > 0) {
                    content.setText(note.getContent().substring(0, toWrap) + "...");
                } else {
                    content.setText(note.getContent());
                }
            } else { //if less than 50 chars...leave it as is :P
                content.setText(note.getContent());
            }
        }

        return convertView;
    }

}

1 个答案:

答案 0 :(得分:0)

你做得很好,只是缺少获取列表的选定对象Note。 为此,请尝试:

AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
Note mLoadedNote = (Note) getListAdapter().getItem(info.position);
mLoadedNote.getTitle()

如果没有该代码,您的对象mLoadedNote将始终为null,而Toast将永远不会显示。