Reputation: 2782
I am trying to populate a ListView in a fragment named DrivingLog. I made a custom adapter that converts strings of a specific format to a view called 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>
When I switch to the DrivingLog fragment from MainActivity, nothing shows up.
Where am I going wrong?
Upvotes: 1
Views: 54
Reputation: 37404
You need to call super(context, layoutId,values)
because super(context, layoutId,)
will generate an empty list inside adapter and the count will be zero so to represent the original count of data use the suggested constructor so use
public DrivingLogAdapter(Context context, int layoutId, String[] values)
{
super(context, layoutId,values);
this.context = context;
this.values = values;
}
Upvotes: 1
Reputation: 75
You are missing this
@Override
public int getCount() {
return super.getCount();
}
in DrivingLogAdapter
class.
plus try super(context, layoutId,values);
in DrivingLogAdapter
constructor
Upvotes: 1