Home > Back-end >  My onBindViewHolder Cannot see my imageView with id
My onBindViewHolder Cannot see my imageView with id

Time:12-25

My Adapter in my onBindView holder cannot find the id of imageView i want to write code like viewHolder.imageView.... so when i am write code, my code saying to me Cannot Resolve This symbol, so What is the main problem ?

Note : I am trying to make AutoSliderImage

package com.ilkin.androidimageslider;

import android.content.ClipData;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.smarteist.autoimageslider.SliderViewAdapter;

public class SliderAdapter extends SliderViewAdapter<SliderViewAdapter.ViewHolder> {

    int[] images;

    public SliderAdapter(int[] images){

        this.images = images;

    }

    @Override
    public SliderViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.slider_item,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(SliderViewAdapter.ViewHolder viewHolder, int position) {



    }

    @Override
    public int getCount() {
        return 0;
    }

    public class ViewHolder extends SliderViewAdapter.ViewHolder {

       public ImageView imageView;


        public ViewHolder(View itemView){
            super(itemView);

            imageView = (ImageView) itemView.findViewById(R.id.image_view);



        }

    }
}

Main Activity

package com.ilkin.androidimageslider;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import com.smarteist.autoimageslider.SliderView;

public class MainActivity extends AppCompatActivity {

    SliderView sliderView;
    int[] images = {R.drawable.coleseum,R.drawable.eifel,R.drawable.pisa,R.drawable.london};

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

        sliderView = findViewById(R.id.image_slider);

    }
}

Slider Item XML

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@ id/image_view"
        android:scaleType="centerCrop"
        android:adjustViewBounds="true">

    </ImageView>
</androidx.constraintlayout.widget.ConstraintLayout>

Activity Main XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <com.smarteist.autoimageslider.SliderView
        android:id="@ id/image_slider"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        app:sliderAnimationDuration="600"
        app:sliderAutoCycleDirection="back_and_forth"
        app:sliderAutoCycleEnabled="true"
        app:sliderIndicatorAnimationDuration="600"
        app:sliderIndicatorGravity="center_horizontal"
        app:sliderIndicatorMargin="15dp"
        app:sliderIndicatorOrientation="horizontal"
        app:sliderIndicatorPadding="3dp"
        app:sliderIndicatorRadius="2dp"
        app:sliderIndicatorSelectedColor="#5A5A5A"
        app:sliderIndicatorUnselectedColor="#FFF"
        app:sliderScrollTimeInSec="2"
        app:sliderStartAutoCycle="true">

    </com.smarteist.autoimageslider.SliderView>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_below="@ id/image_slider"
        android:textAlignment="center"
        android:text="Auto Image Slider"
        android:textColor="@color/black"
        android:textSize="40dp"
        android:layout_marginTop="100dp"
        >

    </TextView>


</RelativeLayout>

CodePudding user response:

To use databinding, you should put below code to Slide Item Xml file, outside of your ConstraintLayout.

<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">

</layout>

CodePudding user response:

change your Main Activity class like this

public class MainActivity extends AppCompatActivity {
SliderView sliderView;
int[] images = {R.drawable.images,R.drawable.images,R.drawable.images,R.drawable.images};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    sliderView = findViewById(R.id.image_slider);
    SliderAdapter adapter=new SliderAdapter(images);
    sliderView.setSliderAdapter(adapter);

}}

and change SliderAdapter class like this

public class SliderAdapter extends SliderViewAdapter<SliderViewAdapter.ViewHolder> {

int[] images;
public SliderAdapter(int[] images){

    this.images = images;
}
@Override
public SliderViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.slider_item,parent,false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(SliderViewAdapter.ViewHolder viewHolder, int position) {
    ViewHolder holder= (ViewHolder) viewHolder;
    holder.imageView.setImageResource(images[position]);
}

@Override
public int getCount() {
    return images.length;
}

public class ViewHolder extends SliderViewAdapter.ViewHolder {

    public ImageView imageView;


    public ViewHolder(View itemView){
        super(itemView);

        imageView = (ImageView) itemView.findViewById(R.id.image_view);



    }

}}
  • Related