I have very strange problem with displaying item in RecyclerView
. first of all it does not display items (strange D/OpenGLRenderer: endAllStagingAnimators on 0x6dd9c400 (CardView) with handle 0x6861d090
appears - never happened before) and back button does not work (it appears but does not react). Ok lets start. I have some Fragment
(here is that fragment with RecyclerView
:
I also have 1 identical fragment to that but one of them conatins as you can see kitchen types
and second one has meal types
. When I click on one of list items, in both cases action redirects me to new activity
When I click on some of that items f.e first one, here it is what happen inside this RecyclerView
adapter:
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
KitchenTypeItem kitchenItem = kitchenItems.get(position);
holder.kitchenTypeName.setText("Kuchnia " + kitchenItem.getKitchenName());
holder.kitchenTypeImgThumbnail.setImageResource(kitchenItem.getKitchenThumbnail());
holder.kitchenTypeDescription.setText(kitchenItem.getKitchenDescription());
holder.setClickListener(new ItemClickListener() {
@Override
public void onClick(View view, int position) {
KitchenTypeItem kitchenItem = kitchenItems.get(position);
Intent intent = new Intent(context, RecipeActivity.class);
intent.putExtra(TYPE_NAME, kitchenItem.getKitchenName());
intent.putExtra(DISPLAY_TYPE, 0);
context.startActivity(intent);
}
});
}
When I press one of that items I send 2 parameters to new activity which are: TYPE_NAME
- helps me to get know if I will be using newly opened activity to download data from server containing kitchen types
recipes or meal types
recipes and DISPLAY_TYPE
- used to send request to MySQL database. This is some code from 2nd adapter (as you can almost identical):
Intent intent = new Intent(context, RecipeActivity.class);
intent.putExtra(TYPE_NAME, mealItem.getMealName());
intent.putExtra(DISPLAY_TYPE, 1);
context.startActivity(intent);
This is what I get inside newly opened activity:
As you can see nothing is displying despite the fact RecipeAdapter
is made correctly I think:
Here you have it's code:
public class RecipeAdapter extends RecyclerView.Adapter<RecipeAdapter.ViewHolder> {
private static String TAG = RecipeAdapter.class.getSimpleName().toString();
private Context context;
private ArrayList<RecipeItem> recipeItems;
private CoordinatorLayout coordinatorLayout;
public RecipeAdapter(Context context, ArrayList<RecipeItem> recipeItems) {
this.context = context;
this.recipeItems = recipeItems;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recipe_item, parent,
false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
RecipeItem recipeItem = recipeItems.get(position);
Picasso.with(context).load(recipeItem.getRecipeImgThumbnailLink()).into(
holder.recipeItemImgThumbnail);
holder.recipeItemTitle.setText(recipeItem.getRecipeTitle());
holder.recipeItemKitchenMealType.setText("Kuchnia " + recipeItem.getRecipeKitchenType() +
", " + recipeItem.getRecipeMealType());
holder.recipeItemAddDate.setText(recipeItem.getRecipeAddDate());
holder.recipeItemLikeCount.setText(recipeItem.getRecipeLikeCount());
holder.setClickListener(new ItemClickListener2() {
@Override
public void onClick(View view, int position, boolean isLongClick) {
if (!isLongClick) {
// go to recipes site
} else {
RecipeItem recipeItem = recipeItems.get(position);
FragmentActivity fragmentActivity = (FragmentActivity)(context);
FragmentManager fragmentManager = fragmentActivity.getSupportFragmentManager();
RecipeAddDialogFragment recipeDialogFragment = new RecipeAddDialogFragment();
Log.d(TAG, "Ustawiono recipeUniqueId, coordinatorLayout oraz " +
"recipeDialogFragment w klasie RecipeAddDialogFragment");
recipeDialogFragment.setReferences(recipeItem.getRecipeUniqueID(),
coordinatorLayout, recipeDialogFragment);
Log.d(TAG, "Uruchamiam okno dialogowe RecipeAddDialogFragment");
recipeDialogFragment.show(fragmentManager, "recipeDialogFragment");
}
}
});
}
@Override
public int getItemCount() {
return recipeItems.size();
}
// Recipe Item Holder
class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener,
View.OnLongClickListener {
private ImageView recipeItemImgThumbnail;
private TextView recipeItemTitle;
private TextView recipeItemKitchenMealType;
private TextView recipeItemAddDate;
private TextView recipeItemLikeCount;
private ItemClickListener2 clickListener2;
public ViewHolder(View itemView) {
super(itemView);
recipeItemImgThumbnail = (ImageView) itemView.findViewById(
R.id.recipe_item_img_thumbnail);
recipeItemTitle = (TextView) itemView.findViewById(R.id.recipe_item_title);
recipeItemKitchenMealType = (TextView) itemView.findViewById(
R.id.recipe_item_kitchen_meal_type);
recipeItemAddDate = (TextView) itemView.findViewById(R.id.recipe_item_add_date);
recipeItemLikeCount = (TextView) itemView.findViewById(R.id.recipe_item_like_count);
itemView.setOnClickListener(this);
itemView.setOnLongClickListener(this);
}
public void setClickListener(ItemClickListener2 itemClickListener2) {
this.clickListener2 = itemClickListener2;
}
@Override
public void onClick(View view) {
clickListener2.onClick(view, getAdapterPosition(), false);
}
@Override
public boolean onLongClick(View view) {
clickListener2.onClick(view, getAdapterPosition(), true);
return true;
}
}
public void setCoordinatorLayout(CoordinatorLayout coordinatorLayout) {
this.coordinatorLayout = coordinatorLayout;
}
}
Here is XML file of that activity:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/recipe_activity_coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".RecipeActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/log_reg_background"
android:orientation="vertical">
<!-- Toolbar -->
<include
android:id="@+id/toolbar"
layout="@layout/tool_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v7.widget.RecyclerView
android:id="@+id/activity_recipe_recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="8dp"
android:clipToPadding="false"/>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
This is main code of the activity:
public class RecipeActivity extends AppCompatActivity {
private static String TAG = RecipeActivity.class.getSimpleName().toString();
private ArrayList<RecipeItem> recipeItems;
private RecipeAdapter recipeAdapter;
private ProgressDialog pDialog;
private CoordinatorLayout coordinatorLayout;
private BroadcastReceiver broadcastReceiver;
private int offset;
private String typeName;
private int displayType;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recipe);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
offset = 0;
recipeItems = new ArrayList<>();
coordinatorLayout = (CoordinatorLayout) findViewById(
R.id.recipe_activity_coordinator_layout);
RecyclerView recipeRecyclerView = (RecyclerView) findViewById(
R.id.activity_recipe_recyclerview);
recipeAdapter = new RecipeAdapter(getApplicationContext(), recipeItems);
recipeAdapter.setCoordinatorLayout(coordinatorLayout);
recipeRecyclerView.setHasFixedSize(true);
recipeRecyclerView.setAdapter(recipeAdapter);
recipeRecyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
recipeRecyclerView.setItemAnimator(new DefaultItemAnimator());
getBundleExtras(); // Getting Argument Passed By Meal Or Kitchen Type Adapter
setActivityTitle(); // Setting Title Depending On Adapter Type
setBroadcastReceiver();
}
@Override
public void onResume() {
registerReceiver(broadcastReceiver, new IntentFilter(
"android.net.conn.CONNECTIVITY_CHANGE"));
Log.d(TAG, "Zarejestrowano BroadcastReceiver'a");
super.onResume();
}
@Override
public void onPause() {
unregisterReceiver(broadcastReceiver);
Log.d(TAG, "Wyrejestrowano BradcasReceiver'a");
super.onPause();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.home) {
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
private void getBundleExtras() {
Bundle extras = getIntent().getExtras();
// Static Fields Could Bo From MealAdapter Aswell
typeName = extras.getString(KitchenTypeAdapter.TYPE_NAME);
displayType = extras.getInt(KitchenTypeAdapter.DISPLAY_TYPE);
Log.d(TAG, "Trzymano dane z listy fragmentu: typeName=" + typeName + ", displayType=" +
displayType);
}
private void setActivityTitle() {
switch (displayType) {
case 0: {
setTitle(getString(R.string.nav_kitchen_type));
Log.d(TAG, "Ustawiono tytuł: " + displayType);
break;
}
case 1: {
setTitle(getString(R.string.nav_meal_type));
Log.d(TAG, "Ustawiono tytuł: " + displayType);
break;
}
}
}
private void setBroadcastReceiver() {
broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(
Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (activeNetwork == null) {
Log.d(TAG, "Brak połączenia. Pokazuję komuikat.");
showSnackbarInfo(getString(R.string.err_msg_connection_problem),
R.color.snackbar_error_msg);
} else {
if (recipeItems.isEmpty()) {
Log.d(TAG, "Jest połączenie. Listy puste. Pobieram przepisy.");
getRecipes();
} else {
Log.d(TAG, "Jest połączenie. Listy pełne. Nic nie robię.");
}
}
}
};
}
private void getRecipes() {
switch (displayType) {
case 0: {
Log.d(TAG, "Pobieranie przepisów z warunkiem typ_kuchnii");
getKitchenTypeRecipes();
break;
}
case 1: {
Log.d(TAG, "Pobieranie przepisów z warunkiem typ_posiłku");
getMealTypeRecipes();
break;
}
}
}
private void getKitchenTypeRecipes() {
JsonArrayRequest kitchenTypeRecipesReq = new JsonArrayRequest(prepareGetMethodUrl(),
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d("odpowiedz", "Odpowiedź przepisów: " + response.toString());
Log.d("odpowiedz", "Dlugosc response: " + Integer.toString(
response.length()));
if (response.length() == 0) {
showSnackbarInfo("Na serwerze nic nie ma (kitchen type)", R.color.snackbar_info_msg);
} else {
for (int i = 0; i < response.length(); i++) {
try {
JSONObject jObj = response.getJSONObject(i);
RecipeItem recipeItem = new RecipeItem();
recipeItem.setRecipeUniqueID(jObj.getString("unique_id"));
recipeItem.setRecipeTitle(jObj.getString("title"));
recipeItem.setRecipeImgThumbnailLink(jObj.getString(
"img_tumbnail_link"));
recipeItem.setRecipeAddAte(jObj.getString("add_date"));
recipeItem.setRecipeKitchenType(jObj.getString("kitchen_type"));
recipeItem.setRecipeMealType(jObj.getString("meal_type"));
recipeItem.setRecipeLikeCount(jObj.getString("like_count"));
} catch (JSONException e) {
e.printStackTrace();
}
}
recipeAdapter.notifyItemRangeInserted(recipeItems.size(),
response.length());
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("odpowiedz", "Błąd pobierania przepisów (kichen type): " +
Integer.toString(error.networkResponse.statusCode));
showSnackbarInfo(Integer.toString(error.networkResponse.statusCode),
R.color.snackbar_error_msg);
}
});
AppController.getInstance().addToRequestQueue(kitchenTypeRecipesReq);
}
private void getMealTypeRecipes() {
JsonArrayRequest mealTypeRecipesReq = new JsonArrayRequest(prepareGetMethodUrl(),
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d("odpowiedz", "Odpowiedź przepisów: " + response.toString());
Log.d("odpowiedz", "Dlugosc response: " + Integer.toString(
response.length()));
if (response.length() == 0) {
showSnackbarInfo("Na serwerze nic nie ma (meal_type)",
R.color.snackbar_info_msg);
} else {
for (int i = 0; i < response.length(); i++) {
try {
JSONObject jObj = response.getJSONObject(i);
RecipeItem recipeItem = new RecipeItem();
recipeItem.setRecipeUniqueID(jObj.getString("unique_id"));
recipeItem.setRecipeTitle(jObj.getString("title"));
recipeItem.setRecipeImgThumbnailLink(jObj.getString(
"img_tumbnail_link"));
recipeItem.setRecipeAddAte(jObj.getString("add_date"));
recipeItem.setRecipeKitchenType(jObj.getString("kitchen_type"));
recipeItem.setRecipeMealType(jObj.getString("meal_type"));
recipeItem.setRecipeLikeCount(jObj.getString("like_count"));
} catch (JSONException e) {
e.printStackTrace();
}
}
recipeAdapter.notifyItemRangeInserted(recipeItems.size(),
response.length());
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("odpowiedz", "Błąd pobierania przepisów (meal type): " +
Integer.toString(error.networkResponse.statusCode));
showSnackbarInfo(Integer.toString(error.networkResponse.statusCode),
R.color.snackbar_error_msg);
}
});
AppController.getInstance().addToRequestQueue(mealTypeRecipesReq);
}
private String prepareGetMethodUrl() {
switch (displayType) {
case 0: {
return AppConfig.URL_GETRECIPESBYKITCHENTYPE + "?kitchen_type=" + typeName +
"&offset=" + Integer.toString(offset);
}
case 1: {
return AppConfig.URL_GETRECIPESBYMEALTYPE + "?meal_type=" + typeName +
"&offset=" + Integer.toString(offset);
}
default:
return null;
}
}
// Showing Dialog Window
private void showDialog() {
if (!pDialog.isShowing()) {
pDialog.show();
}
}
// Hiding Dialog Window
private void hideDialog() {
if (pDialog.isShowing()) {
pDialog.dismiss();
}
}
// Fucntion Showing Text In Snackbar
private void showSnackbarInfo(String info, int textColor) {
Snackbar snackbar = Snackbar.make(coordinatorLayout, info, Snackbar.LENGTH_LONG);
View sbView = snackbar.getView();
TextView sbText = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text);
sbText.setTextColor(ContextCompat.getColor(getApplicationContext(), textColor));
snackbar.show();
}
}
I can guarantee that everything is fine with downloading and putExtra
works fine so the parameters are send correctly. Just the problem with displaying it and that strange error: D/OpenGLRenderer: endAllStagingAnimators on 0x6dd9c400 (CardView) with handle 0x6861d090
and not working up button
. Can sb help me?
I add
in Manifest and solve my problem.