Ruben Flores Ruben Flores - 1 year ago 125
Java Question

Problems with intance of my DataReader Class

I have some trouwble with my custom class to get Data. I've created one custom class for get data from my web service, the issue comes when I try to use my singleton class, If I use

DataReader.getInstance(getContext(), this)
my listview is populated only the first time when app starts. But if I use a new instance of my DataReader work fine in anytime..

Here my DataReader Class:

public class DataReader {
private static DataReader singleton;
private Context context;
private Data_Listener data_listener;

public DataReader(Context context, Data_Listener data_listener) {
this.context = context;
this.data_listener = data_listener;

public static synchronized DataReader getInstance(Context context, Data_Listener data_listener) {
if (singleton == null) {
singleton = new DataReader(context, data_listener);

return singleton;

public void Categorias_fill(){
final ArrayList<Object> objects = new ArrayList<>();
API.getInstance(context).unAuthenticateArrayRequest(Request.Method.GET, context.getString(R.string.endpoint_categorias), null, new API_Listener() {
public void OnSuccess(JSONObject response) {


public void OnSuccess(JSONArray response) throws JSONException {
Gson gson = new Gson();
Categoria categoria;
for(int i = 0;i < response.length(); i++) {
categoria = gson.fromJson(response.getJSONObject(i).toString(), Categoria.class);

public void OnError(String error) {


and here how I used DataReader:

public class CategoriasFragment extends Fragment implements Data_Listener{
private Categorias_Adapter adapter;
private ArrayList<Categoria> list = new ArrayList<>();
private ExpandableListView listView;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_categorias, container, false);
listView = (ExpandableListView) view.findViewById(;
listView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
public boolean onChildClick(ExpandableListView parent, View v,int groupPosition, int childPosition, long id) {
SubCategoria subCategoria = list.get(groupPosition).getSubCategorias().get(childPosition);
Log.d(General.appname, subCategoria.getNombre());

return true;

//This work only the first time
//DataReader.getInstance(getContext(), this).Categorias_fill();

//This work everytime
DataReader dataReader = new DataReader(getContext(), this);

return view;

public void onBindData(ArrayList<Object> objects) {
list = (ArrayList) objects;
adapter = new Categorias_Adapter(getContext(), list);

Answer Source

In subsequent calls to getInstance() your context and Data_Listener are not getting set. They'll still point to whatever they pointed to with the first call.

I suggest trying this: explicitly set DataReader.context and DataReader.data_listener inside your getInstance() method.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download