我正在尝试在名为DrivingLog的片段中填充ListView。我创建了一个自定义适配器,将特定格式的字符串转换为名为drivinglog_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片段时,没有任何显示。
我哪里错了?
答案 0 :(得分:1)
你错过了这个
@Override
public int getCount() {
return super.getCount();
}
在DrivingLogAdapter
课程中。
在super(context, layoutId,values);
DrivingLogAdapter
constructor
答案 1 :(得分:0)
你需要调用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;
}