I'm a beginning android developer. I have made an app with CardView, but when I scrolling RecyclerView, it's changing its content. Please, help me)
Video: http://sendvid.com/60ui8cay
My code:
RecyclerViewActivity.java:
import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.CardView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class RecyclerViewActivity extends AppCompatActivity {
private List<Ingredient> ingredients = new ArrayList();
private RecyclerView rv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recyclerview_activity);
rv=(RecyclerView)findViewById(R.id.rv);
LinearLayoutManager llm = new LinearLayoutManager(this);
rv.setLayoutManager(llm);
rv.setHasFixedSize(true);
initializeAdapter();
initializeData();
}
private void initializeData(){
ingredients.add(new Ingredient("Арахис очищенный", "8 грамм в чайной ложке", "23,3 грамма в столовой ложке", "175 грамм в стакане (250 мл)", R.drawable.peanut));
ingredients.add(new Ingredient("Брусника", "", "", "140 грамм в стакане (250 мл)", R.drawable.cranberry));
ingredients.add(new Ingredient("Варенье", "19 грамм в чайной ложке", "46,7 грамм в столовой ложке", "330 грамм в стакане (250 мл)", R.drawable.jam));
}
private void initializeAdapter(){
RVAdapter adapter = new RVAdapter(ingredients);
rv.setAdapter(adapter);
}
}
RVAdapter.java:
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
public class RVAdapter extends RecyclerView.Adapter<RVAdapter.IngredientViewHolder> {
public static class IngredientViewHolder extends RecyclerView.ViewHolder {
CardView cv;
public static TextView ingredientName;
public static TextView ingredientTeaspoon;
public static TextView ingredientTablespoon;
public static TextView ingredientGlass;
public static ImageView ingredientPhoto;
IngredientViewHolder(View itemView) {
super(itemView);
cv = (CardView)itemView.findViewById(R.id.cv);
ingredientName = (TextView)itemView.findViewById(R.id.ingredient_name);
ingredientTeaspoon = (TextView)itemView.findViewById(R.id.ingredient_teaspoon);
ingredientTablespoon = (TextView)itemView.findViewById(R.id.ingredient_tablespoon);
ingredientGlass = (TextView)itemView.findViewById(R.id.ingredient_glass);
ingredientPhoto = (ImageView)itemView.findViewById(R.id.ingredient_photo);
}
}
List<Ingredient> ingredients;
RVAdapter(List<Ingredient> ingredients){
this.ingredients = ingredients;
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
@Override
public IngredientViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false);
IngredientViewHolder ivh = new IngredientViewHolder(v);
return ivh;
}
@Override
public void onBindViewHolder(IngredientViewHolder ingredientViewHolder, int i) {
IngredientViewHolder.ingredientName.setText(ingredients.get(i).name);
IngredientViewHolder.ingredientTablespoon.setText(ingredients.get(i).tablespoon);
IngredientViewHolder.ingredientTeaspoon.setText(ingredients.get(i).teaspoon);
IngredientViewHolder.ingredientGlass.setText(ingredients.get(i).glass);
IngredientViewHolder.ingredientPhoto.setImageResource(ingredients.get(i).photoId);
}
@Override
public int getItemCount() {
return ingredients.size();
}
}
CardViewActivity.java:
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
public class CardViewActivity extends Activity {
TextView ingredientName;
TextView ingredientTeaspoon;
TextView ingredientTablespoon;
TextView ingredientGlass;
ImageView ingredientPhoto;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cardview_activity);
ingredientName = (TextView)findViewById(R.id.ingredient_name);
ingredientTeaspoon = (TextView)findViewById(R.id.ingredient_teaspoon);
ingredientTablespoon = (TextView)findViewById(R.id.ingredient_tablespoon);
ingredientGlass = (TextView)findViewById(R.id.ingredient_glass);
ingredientPhoto = (ImageView)findViewById(R.id.ingredient_photo);
ingredientPhoto.setImageResource(R.drawable.salt);
}
}
remove
static
inIngredientViewHolder
definition and its fieldsremove all
static
from this block of code:and during
onBindViewHolder
change to:and try to have a read about the difference of static and non-static fields. I Googled and found that: http://www.tutorial4us.com/java/java-static-and-non-static-variable
remove all
static
from this block of code: