Home > Software design >  cardView app, For some reason my code isn't working
cardView app, For some reason my code isn't working

Time:12-02

I have an exam and we can use stack so I'm making the best of it here I tried to insert in list but it doesn't work. This is my code

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ArrayList<Treemodel> drvece = getData();

        RecyclerViewAdapter adapter = new RecyclerViewAdapter(this, drvece);

        RecyclerView recyclerView = findViewById(R.id.recycler);
        recyclerView.setAdapter(adapter);

        recyclerView.setLayoutManager(new GridLayoutManager(this, 2));

    }

    private ArrayList<Treemodel> getData() {
        ArrayList<Treemodel> d = new ArrayList<>();
        d.add(new Treemodel("Pine","Pinus sylvestris","https://yopadoc.com/trees/bor1.jfif","https://yopadoc.com/trees/bor2.jfif"));
        d.add(new Treemodel("Linden","Tilia cordata","https://yopadoc.com/trees/lipa1.jfif","https://yopadoc.com/trees/lipa2.jfif"));
        d.add(new Treemodel("Willow","Salix babylonica","https://yopadoc.com/trees/vrba1.jfif","https://yopadoc.com/trees/vrba2.jfif"));
        d.add(new Treemodel("Maple","Acer platanoides","https://yopadoc.com/trees/javor1.jfif","https://yopadoc.com/trees/javor2.jfif"));
        d.add(new Treemodel("Poplar","Populus nigra","https://yopadoc.com/trees/jablan1.jfif","https://yopadoc.com/trees/jablan2.jfif"));
        d.add(new Treemodel("Locust","Robinia pseudoacacia","https://yopadoc.com/trees/bagrem1.jfif","https://yopadoc.com/trees/bagrem2.jfif")); here i tried to insert in list but it doesn't work 
        return d;
    }

CodePudding user response:

Guys i figured it our :)), pls ignore

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {

public RecyclerViewAdapter(Context kontekst, ArrayList<Treemodel> drvece) {
    this.drvece = drvece;
    this.kontekst = kontekst;
}

private ArrayList<Treemodel> drvece; //kako bi se mogli koristiti pravimo konstruktor
private Context kontekst;

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    //pravi ViewHolder potreban za ubacivanje iz baze u lokalne variable
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    View viewCard = inflater.inflate(R.layout.card_layout, parent, false);
    ViewHolder holder = new ViewHolder(viewCard);
    return holder;
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    // koristeci holder unosimo iz baze tekts
    holder.imeDrveta.setText(drvece.get(position).ime);
    holder.latinskiNaziv.setText(drvece.get(position).latinskiNaziv);
    //za sliku
    RequestManager glideManager = Glide.with(kontekst); //pokrecemo glider
    glideManager.load(drvece.get(position).slika1).into(holder.slikaDrveta);//loadujemo u slikaDrveta iz baze
    holder.card.setTag(position); //omogucava da position bude dostupan u bezimenoj klasi
    holder.card.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            LayoutInflater inflater = LayoutInflater.from(kontekst);
            View viewDialog = inflater.inflate(R.layout.dialog_image, null, true);
            Integer position = (Integer)v.getTag(); //vuce position iz parenta
            ImageView slikaLisca = viewDialog.findViewById(R.id.slika2);
            Glide.with(kontekst)
                    .load(drvece.get(position.intValue()).slika2) //intValue zato sto je position objekat Integer
                    .into(slikaLisca);
            AlertDialog.Builder builder = new AlertDialog.Builder(kontekst);
            builder.setView(viewDialog);
            builder.show();
        }
    });


}

@Override
public int getItemCount() {
    return drvece.size();
}

public class ViewHolder extends RecyclerView.ViewHolder{

    TextView imeDrveta;
    TextView latinskiNaziv;
    ImageView slikaDrveta;
    CardView card;

    public ViewHolder(@NonNull View itemView) {
        super(itemView);
        imeDrveta = itemView.findViewById(R.id.ime);
        latinskiNaziv = itemView.findViewById(R.id.latinski);
        slikaDrveta = itemView.findViewById(R.id.slika1);
        card = itemView.findViewById(R.id.card);
    }
}

}

CodePudding user response:

Please try this ArrayList drvece = new Arraylist(); drvece = getData();

  • Related