从listView项目上的自定义对象中提取字符串

时间:2017-11-17 14:36:12

标签: android listview

我创建了自己的类(Course)来保存多个字符串并传递对象以使用ArrayAdapter和AsyncTaskLoader填充列表(字符串是从JSON API填充的。)

让我说我班上有这3个字符串:

private String mCourseTitle;
private String mTime;
private String mTrainer;

public String getCourseTitle() {
    return mCourseTitle;
}

public String getTime() {
    return mTime;
}

public String getTrainer() {
    return mTrainer;
}

我想在我的片段中设置一个项目点击监听器,当点击时,显示只有课程标题字符串的吐司信息,我有点卡住...

如何从点击的项目中提取标题以在Toast中显示它?

 // Create a new adapter that takes an empty list of days as input
        mAdapter = new DayAdapter(getActivity(), new ArrayList<Day>());

        // Set the adapter on the {@link ListView}
        // so the list can be populated in the user interface
        listView.setAdapter(mAdapter);

        //on item click Listener
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                // Show the title
                Toast.makeText(getContext(), Title, Toast.LENGTH_SHORT).show();
            }
        });

1 个答案:

答案 0 :(得分:1)

以下是答案

 //on item click Listener
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
             Course yourCourse = yourList.get(position);
              String yourCourseTitle = yourCourse.getCourseTitle();
            // Show the title
            Toast.makeText(getContext(), Title, Toast.LENGTH_SHORT).show();
        }
    });