Ruben Flores Ruben Flores - 3 months ago 32
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() {
@Override
public void OnSuccess(JSONObject response) {

}

@Override
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);
objects.add(categoria);
}
data_listener.onBindData(objects);
}

@Override
public void OnError(String error) {
data_listener.onBindData(objects);
}

});
}


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;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_categorias, container, false);
listView = (ExpandableListView) view.findViewById(R.id.exp_list);
listView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,int groupPosition, int childPosition, long id) {
if(list.size()>0){
if(list.get(groupPosition).getSubCategorias().size()>0){
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);
dataReader.Categorias_fill();

return view;
}

@Override
public void onBindData(ArrayList<Object> objects) {
list = (ArrayList) objects;
adapter = new Categorias_Adapter(getContext(), list);
listView.setAdapter(adapter);
}

Answer

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.