1

The problem I am having is getting a listview to work inside my fragment. I have tried creating a custom adapter but then I don't have anything appear on my screen, perhaps I am not doing it right.

My main xml for fragment looks like this:

    <ListView
        android:id="@+id/pieChartView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentStart="true"
        />

The adapter layout looks like this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">


    <com.github.mikephil.charting.charts.PieChart
        android:id="@+id/chart1"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_marginBottom="2dp"
        />
 <com.github.mikephil.charting.charts.PieChart
        android:id="@+id/chart2"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_marginBottom="2dp"
        />
 <com.github.mikephil.charting.charts.PieChart
        android:id="@+id/chart3"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_marginBottom="2dp"
        />
 <com.github.mikephil.charting.charts.PieChart
        android:id="@+id/chart4"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_marginBottom="2dp"
        />
 <com.github.mikephil.charting.charts.PieChart
        android:id="@+id/chart5"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_marginBottom="2dp"
        />

The fragment code :

  public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {

        View root = inflater.inflate(R.layout.fragment_piechart, container, false);

        Log.i(ACTIVITY_NAME, "In onCreate()");
        chart1 = root.findViewById(R.id.chart1);
        chart2 = root.findViewById(R.id.chart2);
 chart3 = root.findViewById(R.id.chart3);
        chart4 = root.findViewById(R.id.chart4);
 chart5 = root.findViewById(R.id.chart5);

        ChartOne(); 
        ChartTwo();

        return root;


    }

The problem I am having is creating a custom adapter inside the fragment. I tried one and then with my code above I got an error with where I create the charts(ChartOne,ChartTwo).

  • 1
    If there is an error it's always good to share as much details as possible: Logcat with crash log, at which line of your code did it happen... ? – Bö macht Blau Dec 06 '19 at 20:28
  • it was a null error – newtoMobileDev Dec 06 '19 at 20:53
  • Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Bö macht Blau Dec 06 '19 at 21:14
  • I understand what the error means, I just didn't think my custom adapter was right and was needing help with writing a custom adapter for the code I have – newtoMobileDev Dec 06 '19 at 21:26
  • Since those `PieChart`s are in your `ListView`'s item layout, they need to be handled in the `Adapter`. They are not going to found in the `Fragment`'s layout in `onCreateView()`, as they are created dynamically when the `ListView` builds its children, so those `findViewById()` calls there are all going to return null. It wouldn't make sense to look for them like that anyway, since a `ListView` will have multiples of each `PieChart` with a given ID. Are you sure you want a `ListView` with 5 `PieChart`s in each item? Is that really what you're going for? – Mike M. Dec 06 '19 at 21:39
  • No, what I meant is I would like to have a listview to show a piechart in each item, not have 5 piecharts in each item. 1st item will show 1st chart, 2nd item will show 2nd chart and so on. – newtoMobileDev Dec 06 '19 at 22:04
  • Then you wouldn't define an item layout with 5 `PieChart`s in it. It would have only one, which would be inflated dynamically for each `Adapter` item. However, if you're only going to have those 5, then a `ListView` might be more trouble than it's worth, and not really gain you anything. It might be preferable to just stick that `` in a ``, and make that your `Fragment` layout. You could then do your `PieChart`s' setups in `onCreateView()`, where you have it now. – Mike M. Dec 06 '19 at 22:18
  • I would like to do it that way, but it's for a project and the project requires a listview – newtoMobileDev Dec 06 '19 at 23:44
  • OK, then the item layout should have only one `PieChart` in it, and you would need to handle them in the `Adapter`. The item `View`s don't exist until the `ListView` asks the `Adapter` for them, which won't happen until after the `ListView` is laid out. – Mike M. Dec 07 '19 at 00:04
  • Oh ok, would you be able to provide a template for a solution? I understand that each chart will need a layout, but not sure of how to do that in the adapter – newtoMobileDev Dec 07 '19 at 00:32

0 Answers0