2016-08-09 3 views
0

データを取得するために私のカスタムクラスで動くことがあります。 Webサービスからデータを取得するためのカスタムクラスを1つ作成しましたが、シングルトンクラスを使用しようとすると問題が発生します。DataReader.getInstance(getContext(), this)を使用すると、リストビューはアプリ起動時に初めて作成されます。しかし、私はここ..DataReaderクラスの問題点

をいつでもの私のDataReaderの作業罰金の新しいインスタンスを使用している場合、私の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); 
} 

答えて

1

:私はDataReaderを使用する方法

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); 
     } 

    }); 
} 

、ここでgetInstance()コンテキストとData_Listenerが設定されていません。彼らは最初の呼び出しで指していたものを指しています。

これを試すことをおすすめします。getInstance()メソッドの内部にDataReader.contextとDataReader.data_listenerを明示的に設定することをお勧めします。