java - 尝试使用自定义适配器填充 ListView

标签 java android xml listview android-fragments

我正在尝试在名为 DrivingLog 的 fragment 中填充 ListView。我制作了一个自定义适配器,可将特定格式的字符串转换为名为 drivinglog_list_item.xml 的 View 。

驾驶日志.java

package com.example.drivinglessona3;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;



public class DrivingLog extends Fragment{
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        String[] sampleData = {"02/27/2017,1.2,Residential,Clear",
                "02/25/2017,0.5,Residential,Raining",
                "02/20/2017,0.3,Highway,Snow/Ice"};
        List<String> sampleDataList = new ArrayList<String>(Arrays.asList(sampleData));
        View rootView = inflater.inflate(R.layout.fragment_driving_log, container, false);
        DrivingLogAdapter adapter = new DrivingLogAdapter(getContext(), R.layout.drivinglog_list_item, sampleData);
        ListView listview = (ListView) rootView.findViewById(R.id.listView);
        listview.setAdapter(adapter);
        return rootView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getActivity().setTitle("Driving Log");
    }

    public class DrivingLogAdapter extends ArrayAdapter<String[]> {
        private final Context context;
        private final String[] values;

        public DrivingLogAdapter(Context context, int layoutId, String[] values)
        {
            super(context, layoutId);
            this.context = context;
            this.values = values;
        }

        @NonNull
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View rowView = inflater.inflate(R.layout.drivinglog_list_item, parent, false);
            TextView tv_datehours = (TextView) rowView.findViewById(R.id.tv_dateHours);
            ImageView iv_lessonType = (ImageView) rowView.findViewById(R.id.iv_lessonType);
            ImageView iv_weatherConditions = (ImageView) rowView.findViewById(R.id.iv_weatherConditions);
            String css = values[position]; //css = comma separated string
            String[] css_split = css.split(",");
            String date = css_split[0];
            String hours = css_split[1];
            String lessonType = css_split[2];
            String weatherConditions = css_split[3];
            tv_datehours.setText(String.format("%1s - %2s hours", date, hours));
            if (lessonType.equals("Residential")){
                iv_lessonType.setImageResource(R.drawable.ic_residential);
            }else if (lessonType.equals("Commercial")){
                iv_lessonType.setImageResource(R.drawable.ic_commercial);
            }else if (lessonType.equals("Highway")) {
                iv_lessonType.setImageResource(R.drawable.ic_highway);
            }
            if (weatherConditions.equals("Clear")){
                iv_weatherConditions.setImageResource(R.drawable.ic_sunny);
            }else if (weatherConditions.equals("Raining")){
                iv_weatherConditions.setImageResource(R.drawable.ic_rainy);
            }else if (weatherConditions.equals("Snow/Ice")) {
                iv_weatherConditions.setImageResource(R.drawable.ic_snowy);
            }

            return rowView;
        }
    }
 }

fragment_driving_log.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="DrivingLog">

    <!-- TODO: Update blank fragment layout -->

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listView"
        />


</LinearLayout>

drivinglog_list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent">


    <TextView
        android:text="TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv_dateHours"
        android:layout_weight="1" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@mipmap/ic_launcher"
        android:id="@+id/iv_lessonType"
        android:layout_weight="1" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@mipmap/ic_launcher"
        android:id="@+id/iv_weatherConditions"
        android:layout_weight="0.18" />
</LinearLayout>

当我从 MainActivity 切换到 DrivingLog fragment 时,没有任何显示。

enter image description here

我哪里错了?

最佳答案

你需要调用 super(context, layoutId,values) 因为 super(context, layoutId,)将在适配器内生成一个空列表,计数将为零,因此使用建议的构造函数表示原始数据计数,因此使用

public DrivingLogAdapter(Context context, int layoutId, String[] values)
   {
       super(context, layoutId,values);
       this.context = context;
       this.values = values;
   }

关于java - 尝试使用自定义适配器填充 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43075822/

相关文章:

java - "Flip all"(Light Out) 游戏的任何算法?

android - 在同一 Activity 中创建 ActionBar 和选项菜单

java - 随机数生成器崩溃

android - 适用于 Win32/Android 的基本 Indy TCP 客户端/服务器

xml - HiveQL-提取同级节点的值

css - 将样式应用于 xml 属性值

xml - 删除 XML 标记之间的某些文本

java - 无穷级数之和与 Math.exp 结果不一致

java - 将无状态注入(inject) ManagedBean

java - Kotlin Koans 与 EduTools 插件 : "Failed to launch checking"