簡體   English   中英

嘗試使用自定義適配器填充ListView

[英]Attempting to Populate ListView With Custom Adapter

我正在嘗試在名為DrivingLog的片段中填充ListView。 我制作了一個自定義適配器,該適配器將特定格式的字符串轉換為名為drivelog_list_item.xml的視圖。

DrivingLog.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片段時,什么都沒有顯示。

在此處輸入圖片說明

我要去哪里錯了?

你錯過了這個

@Override
public int getCount() {
return super.getCount();
}

DrivingLogAdapter類中。

加上嘗試super(context, layoutId,values); DrivingLogAdapter constructor

您需要調用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;
   }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM