jjoe64 / GraphView

Android Graph Library for creating zoomable and scrollable line and bar graphs.
https://github.com/jjoe64/GraphView/wiki
Other
2.76k stars 811 forks source link

Crash: In a recyclerView that has 4 different layouts with textviews, how can i add a graph in a layout. #685

Closed ptzivaras closed 2 years ago

ptzivaras commented 2 years ago

Project with Recycler View that has multiple layouts and in one layout it has a simple line graph. So Screen has buttons and when i click at them it displays the layout that i want in the list.

FATAL EXCEPTION: main Process: com.example.rv_multipllayouts, PID: 16703 java.lang.NullPointerException: Attempt to invoke virtual method 'void com.jjoe64.graphview.GraphView.addSeries(com.jjoe64.graphview.series.Series)' on a null object reference at com.example.rv_multipllayouts.MainActivity$1.onClick(MainActivity.java:60) `package com.example.rvmultipllayouts;

import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

public class ExampleAdapter extends RecyclerView.Adapter{ private ArrayList mExampleList; private static final int TYPE_ONE = 1; private static final int TYPE_TWO = 2; private static final int TYPE_THREE=3; private static final int TYPE_FOUR =4;

// public static class ExampleViewHolder extends RecyclerView.ViewHolder { // public TextView mTextView1; // public TextView mTextView2; // // public ExampleViewHolder(View itemView) { // super(itemView); // mTextView1 = itemView.findViewById(R.id.textView01); // mTextView2 = itemView.findViewById(R.id.textView02); // } // }

public ExampleAdapter(ArrayList<ExampleItem> exampleList) {
    mExampleList = exampleList;
}

// determine which layout to use for the row
@Override
public int getItemViewType(int position) {
    ExampleItem exampleItem = mExampleList.get(position);
    if (exampleItem.getType() == ExampleItem.ItemType.ONE_ITEM) {
        return TYPE_ONE;
    } else if (exampleItem.getType() == ExampleItem.ItemType.TWO_ITEM) {
        return TYPE_TWO;
    } else if (exampleItem.getType() == ExampleItem.ItemType.THREE_ITEM) {
        return TYPE_THREE;
    } else if (exampleItem.getType() == ExampleItem.ItemType.FOUR_ITEM) {
        return TYPE_FOUR;
    } else {
        return -1;
    }
}

// @Override // public ExampleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { // View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.example_item, parent, false); // ExampleViewHolder evh = new ExampleViewHolder(v); // return evh; // }

// specify the row layout file and click for each row
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    if (viewType == TYPE_ONE) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.example_item, parent, false);
        return new ViewHolderOne(view);
    } else if (viewType == TYPE_TWO) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.example_item2, parent, false);
        return new ViewHolderTwo(view);
    } else if (viewType == TYPE_THREE) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.example_item3, parent, false);
        return new ViewHolderThree(view);
    } else if (viewType == TYPE_FOUR) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.example_item4, parent, false);
        return new ViewHolderFour(view);
    } else {
        throw new RuntimeException("The type has to be ONE or TWO or Three");
    }
}

// @Override // public void onBindViewHolder(ExampleViewHolder holder, int position) { // ExampleItem currentItem = mExampleList.get(position); // // holder.mTextView1.setText(currentItem.getText1()); // holder.mTextView2.setText(currentItem.getText2()); // }

// load data in each row element
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    switch (holder.getItemViewType()) {
        case TYPE_ONE:
            initLayoutOne((ViewHolderOne)holder, position);
            break;
        case TYPE_TWO:
            initLayoutTwo((ViewHolderTwo) holder, position);
            break;
        case TYPE_THREE:
            initLayoutThree((ViewHolderThree) holder, position);
            break;
        case TYPE_FOUR:
            initLayoutFour((ViewHolderFour) holder, position);
            break;
        default:
            break;
    }
}

private void initLayoutOne(ViewHolderOne holder, int pos) {
    holder.ittem.setText(mExampleList.get(pos).getText1());
    holder.ittem2.setText(mExampleList.get(pos).getText2());
}

private void initLayoutTwo(ViewHolderTwo holder, int pos) {
    holder.mitem1.setText(mExampleList.get(pos).getText1());
    holder.mitem2.setText(mExampleList.get(pos).getText2());
    holder.mitem3.setText(mExampleList.get(pos).getText3());
}

private void initLayoutThree(ViewHolderThree holder, int pos) {
    holder.Xmitem1.setText(mExampleList.get(pos).getText1());
    holder.Xmitem2.setText(mExampleList.get(pos).getText2());
    holder.Xmitem3.setText(mExampleList.get(pos).getText3());
}

private void initLayoutFour(ViewHolderFour holder, int pos) {
    holder.Smitem1.setText(mExampleList.get(pos).getText1());
}
// Static inner class to initialize the views of rows
static class ViewHolderOne extends RecyclerView.ViewHolder {
    public TextView ittem, ittem2;
    public ViewHolderOne(View itemView) {
        super(itemView);
        ittem = (TextView) itemView.findViewById(R.id.textView01);
        ittem2 = (TextView) itemView.findViewById(R.id.textView02);
    }
}
//potato
static class ViewHolderTwo extends RecyclerView.ViewHolder {
    public TextView mitem1, mitem2, mitem3;
    public ViewHolderTwo(View itemView) {
        super(itemView);
        mitem1 = (TextView) itemView.findViewById(R.id.textView01);
        mitem2 = (TextView) itemView.findViewById(R.id.textView02);
        mitem3 = (TextView) itemView.findViewById(R.id.textView03);
    }
}

static class ViewHolderThree extends RecyclerView.ViewHolder {
    public TextView Xmitem1, Xmitem2, Xmitem3;
    public ViewHolderThree(View itemView) {
        super(itemView);
        Xmitem1 = (TextView) itemView.findViewById(R.id.textView01);
        Xmitem2 = (TextView) itemView.findViewById(R.id.textView02);
        Xmitem3 = (TextView) itemView.findViewById(R.id.textView03);
    }
}

static class ViewHolderFour extends RecyclerView.ViewHolder {
    public TextView Smitem1;
    public ViewHolderFour(View itemView) {
        super(itemView);
        Smitem1 = (TextView) itemView.findViewById(R.id.textView01);
    }
}

@Override
public int getItemCount() {
    return mExampleList.size();
}

} package com.example.rv_multipllayouts;

public class ExampleItem {

public enum ItemType {
    ONE_ITEM, TWO_ITEM, THREE_ITEM, FOUR_ITEM;
}

private String mText1;
private String mText2;
private String mText3;
private ItemType type;

public ExampleItem( String text1, String text2, String text3, ItemType type) {
    mText1 = text1;
    mText2 = text2;
    mText3 = text3;
    this.type = type;
}

public ExampleItem( String text1, String text2, ItemType type) {
    mText1 = text1;
    mText2 = text2;
    this.type = type;
}

public ExampleItem( String text1,  ItemType type) {
    mText1 = text1;
    this.type = type;
}

public String getText1() {
    return mText1;
}

public String getText2() {
    return mText2;
}

public String getText3() {
    return mText3;
}

public ItemType getType() {
    return type;
}

} package com.example.rv_multipllayouts;

import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle; import android.view.View; import android.widget.Button;

import com.jjoe64.graphview.GraphView; import com.jjoe64.graphview.series.DataPoint; import com.jjoe64.graphview.series.LineGraphSeries;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity { private RecyclerView mRecyclerView; private RecyclerView.Adapter mAdapter;//Bridge between data and image/ arrayList and RecyclerView private RecyclerView.LayoutManager mLayoutManager;//allign single items in our list private Button button1, button2, button3, button4;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ArrayList<ExampleItem> exampleList = new ArrayList<>();

    button1 = findViewById(R.id.button1);
    button2 = findViewById(R.id.button2);
    button3 = findViewById(R.id.button3);
    button4 = findViewById(R.id.button4);

    button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            exampleList.clear();
            //mAdapter.notifyDataSetChanged();
            for(int i=0;i<10;i++){
                exampleList.add(new ExampleItem("Text1.", "Num-" + i, ExampleItem.ItemType.ONE_ITEM));
            }
            mRecyclerView = findViewById(R.id.recyclerView);
            mRecyclerView.setHasFixedSize(true);
            mLayoutManager = new LinearLayoutManager(getApplicationContext());
            mAdapter = new ExampleAdapter(exampleList);

            mRecyclerView.setLayoutManager(mLayoutManager);
            mRecyclerView.setAdapter(mAdapter);

            //GraphCode
            GraphView graph = (GraphView) findViewById(R.id.graph);
            LineGraphSeries<DataPoint> series = new LineGraphSeries<DataPoint>(new DataPoint[] {
                    new DataPoint(0, 1),
                    new DataPoint(1, 5),
                    new DataPoint(2, 3),
                    new DataPoint(3, 2),
                    new DataPoint(4, 6)
            });
            graph.addSeries(series);

            //exampleList.clear();
            //mAdapter.notifyDataSetChanged();
        }
    });

    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            exampleList.clear();
            //mAdapter.notifyDataSetChanged();
            for(int i=0;i<2;i++){
                exampleList.add(new ExampleItem("Text1.", "Text2.", "Num-" + i, ExampleItem.ItemType.TWO_ITEM));
            }
            mRecyclerView = findViewById(R.id.recyclerView);
            mRecyclerView.setHasFixedSize(true);
            mLayoutManager = new LinearLayoutManager(getApplicationContext());
            mAdapter = new ExampleAdapter(exampleList);

            mRecyclerView.setLayoutManager(mLayoutManager);
            mRecyclerView.setAdapter(mAdapter);
        }
    });

    button3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            exampleList.clear();
            //mAdapter.notifyDataSetChanged();
            for(int i=0;i<2;i++){
                exampleList.add(new ExampleItem("Text1.", "Text2.", "Num-" + i, ExampleItem.ItemType.THREE_ITEM));
            }
            mRecyclerView = findViewById(R.id.recyclerView);
            mRecyclerView.setHasFixedSize(true);
            mLayoutManager = new LinearLayoutManager(getApplicationContext());
            mAdapter = new ExampleAdapter(exampleList);
            mRecyclerView.setLayoutManager(mLayoutManager);
            mRecyclerView.setAdapter(mAdapter);
        }
    });

    button4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            exampleList.clear();
            //mAdapter.notifyDataSetChanged();

            exampleList.add(new ExampleItem("Text1", ExampleItem.ItemType.FOUR_ITEM));

            mRecyclerView = findViewById(R.id.recyclerView);
            mRecyclerView.setHasFixedSize(true);
            mLayoutManager = new LinearLayoutManager(getApplicationContext());
            mAdapter = new ExampleAdapter(exampleList);
            mRecyclerView.setLayoutManager(mLayoutManager);
            mRecyclerView.setAdapter(mAdapter);
        }
    });

    //GraphCode
    GraphView graph = (GraphView) findViewById(R.id.graph2);
    LineGraphSeries<DataPoint> series = new LineGraphSeries<DataPoint>(new DataPoint[] {
            new DataPoint(0, 1),
            new DataPoint(1, 5),
            new DataPoint(2, 3),
            new DataPoint(3, 2),
            new DataPoint(4, 6)
    });
    graph.addSeries(series);

}

}`