MainActivityに2つのフラグメントがあります。なぜこのエラーが起きるのですか?コードは同じですがフラグメントが異なる、Recyclerview、Retrofit2
まず、YellowFragment
public class YellowFragment extends Fragment implements FlowerAdapter.FlowerClickListener{
private SwipeRefreshLayout swipeContainer_yellow;
private RecyclerView mRecyclerView;
private RestManager mRestManager;
private FlowerAdapter mFlowerAdapter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_yellow, container, false);
swipeContainer_yellow = (SwipeRefreshLayout) view.findViewById(R.id.swipeContainer_yellow);
swipeContainer_yellow.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
mRestManager = new RestManager();
Call<List<Flower>> listCall = mRestManager.getmFlowerApiService(getActivity()).getAllFlowers();
listCall.enqueue(new Callback<List<Flower>>() {
@Override
public void onResponse(Call<List<Flower>> call, Response<List<Flower>> response) {
if (response.isSuccessful()) {
mFlowerAdapter.clear();
List<Flower> flowerList = response.body();
for(int i =0; i<flowerList.size(); i++) {
Flower flower = flowerList.get(i);
mFlowerAdapter.addFlower(flower);
}
swipeContainer_yellow.setRefreshing(false);
}
}
@Override
public void onFailure(Call<List<Flower>> call, Throwable t) {
}
});
}
});
configViews(view);
return view;
}
private void configViews(View view) {
mRecyclerView = (RecyclerView) view.findViewById(R.id.rv_flower_yellow);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setRecycledViewPool(new RecyclerView.RecycledViewPool());
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity().getApplicationContext(), LinearLayoutManager.VERTICAL, false));
mFlowerAdapter = new FlowerAdapter(this);
mRecyclerView.setAdapter(mFlowerAdapter);
}
}
第二に、RedFragment (それはYellowFragmentとすべて同じだが、swipeContainer_yellow, rv_flower_yellow
、swipeContainer_red, rv_flower_red
に変更LinearLayoutManager
、GridLayoutManager
)
public class RedFragment extends Fragment {
private SwipeRefreshLayout swipeContainer_red;
private RecyclerView mRecyclerView;
private RestManager mRestManager;
private FlowerAdapter mFlowerAdapter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_red, null);
swipeContainer_red = (SwipeRefreshLayout) view.findViewById(R.id.swipeContainer_red);
swipeContainer_red.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
mRestManager = new RestManager();
Call<List<Flower>> listCall = mRestManager.getmFlowerApiService(getActivity()).getAllFlowers();
listCall.enqueue(new Callback<List<Flower>>() {
@Override
public void onResponse(Call<List<Flower>> call, Response<List<Flower>> response) {
if (response.isSuccessful()) {
mFlowerAdapter.clear();
List<Flower> flowerList = response.body();
for(int i =0; i<flowerList.size(); i++) {
Flower flower = flowerList.get(i);
mFlowerAdapter.addFlower(flower);
}
swipeContainer_red.setRefreshing(false);
}
}
@Override
public void onFailure(Call<List<Flower>> call, Throwable t) {
}
});
}
});
configViews(view);
return view;
}
private void configViews(View view) {
mRecyclerView = (RecyclerView) view.findViewById(R.id.rv_flower_red);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setRecycledViewPool(new RecyclerView.RecycledViewPool());
mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity().getApplicationContext(), 3));
mFlowerAdapter = new FlowerAdapter(this); #Here is problem
mRecyclerView.setAdapter(mFlowerAdapter);
}
}
しかしRedFragmentで、
なぜこれが起こったのですか?
FlowerAdpaterの一部が
public class FlowerAdapter extends RecyclerView.Adapter<FlowerAdapter.Holder> {
public FlowerAdapter(FlowerClickListener listener) {
mFlowers = new ArrayList<>();
mListener = listener;
}
public interface FlowerClickListener {
void onClick(int position);
}
}
私は最初だけで、別のビュー(のGridLayoutとのLinearLayout)上(retrofit2を使用して、私のRESTapiサーバーから来ている)同じデータを有する二つのフラグメントを作ってみました。
しかし、私はそれを断念しました。それは私にとってはあまりにも難しいようでした。
私の最初の試みに依存する良い方法があれば、私は非常に感謝するでしょう。
とにかく、そのエラーはどうして起こりましたか?
ヒントを教えてください。
ありがとうU Rahul Sharma –