Search code examples
androidandroid-recyclerviewadapterpicasso

RecyclerView doesn't load XML


My app loads some items into the recycler view and sets an onClickListeners for each of them. Two hours ago I had the problem where Picasso, Glide or asyncronous task, all of them loaded fine the first 2 or 4 images, then it started loading it randomly (only the images, the textview and the item itself was fine) and flickering sometimes beetween different images.

I've started changing my code to try and fix it and now, the recycler view doesn't load anything.

Can someone take o look at my code? I only touched these 2 classes.

Edit: This is how it look now https://i.sstatic.net/28xHA.jpg

Edit2: using debug, the program never enters the onBindViewHolder or onCreateViewHolder methods

Main Activity

public class Principal extends AppCompatActivity implements AppBarLayout.OnOffsetChangedListener{
    public static LugaresBD lugares;
    private RecyclerView recyclerView;
    public static AdaptadorLugares adaptador;
    private android.support.v7.widget.RecyclerView.LayoutManager layoutManager;
    static final int RESULTADO_PREFERENCIAS = 0;
    static public ArrayList<Lugar> arrayLugares = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        lugares = new LugaresBD(this);
        setContentView(R.layout.activity_main);

        recyclerView = (RecyclerView)
                findViewById(R.id.recycler_view);
        adaptador = new AdaptadorLugares(this, lugares, lugares.extraeCursor());
        recyclerView.setAdapter(adaptador);
        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);


        adaptador.setOnItemClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(Principal.this,
                        VistaLugarActivity.class);
                i.putExtra("id", arrayLugares.get(recyclerView.getChildAdapterPosition(v)).get_id());
                startActivityForResult(i, 1433);
            }
        });
    }
}

Adapter

public class AdaptadorLugares extends RecyclerView.Adapter<AdaptadorLugares.ViewHolder> {
        protected Cursor cursor;
        protected Lugares lugares; //Lugares a mostrar
        protected LayoutInflater inflador; //Crea Layouts con el XML
        protected Context contexto; //Lo necesitamos para el inflador
        protected View.OnClickListener onClickListener;
        public static ImageView foto;
        AsyncTask<String,Void,Bitmap> task = null;
        Bitmap mBitmap;

    public AdaptadorLugares(Context contexto, Lugares lugares, Cursor cursor){
        this.cursor = cursor;
        this.contexto = contexto;
        this.lugares = lugares;
        inflador = (LayoutInflater) contexto.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }



        public static class ViewHolder extends RecyclerView.ViewHolder {
            public TextView nombre;

            public ViewHolder(View itemView) {
                super(itemView);
                nombre = (TextView) itemView.findViewById(R.id.nombrecito);
                foto = (ImageView) itemView.findViewById(R.id.foto);

            }
        }


        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            // Inflamos la vista desde el xml
            View v = inflador.inflate(R.layout.elemento_lista, parent,
                    false);
            v.setOnClickListener(onClickListener);
            return new ViewHolder(v);
        }


        @Override
        public void onBindViewHolder(ViewHolder holder, int posicion) {
            Lugar lugar = lugares.elemento(posicion);

            holder.nombre.setText(lugar.get_id() + lugar.getNombre());
            Picasso.with(contexto).load(lugar.getImage()).into(foto);



        }



        @Override
        public int getItemCount() {
            return lugares.tamanyo();
        }

        public void setOnItemClickListener(View.OnClickListener onClickListener) {
            this.onClickListener = onClickListener;
        }

        public Cursor getCursor(){
            return cursor;
        }

        public void setCursor(Cursor cursor){
            this.cursor = cursor;
        }

        public Lugar lugarPosicion(int posicion){
            cursor.moveToPosition(posicion);
            return LugaresBD.extraeLugar(cursor);
        }

        public int idPosicion(int posicion){
            cursor.moveToPosition((posicion));
            return cursor.getInt(0);
        }
}

Thank you.

Edit3:

Activity Main xml

<android.support.design.widget.CoordinatorLayout
    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:ignore="RtlHardcoded"
    >

    <android.support.design.widget.AppBarLayout
        android:id="@+id/main.appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        >

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/main.collapsing"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
            >

            <ImageView
                android:id="@+id/main.imageview.placeholder"
                android:layout_width="match_parent"
                android:layout_height="300dp"
                android:scaleType="centerCrop"
                android:src="@drawable/street_view"
                android:tint="#11000000"
                app:layout_collapseMode="parallax"
                app:layout_collapseParallaxMultiplier="0.9"
                />

            <FrameLayout
                android:id="@+id/main.framelayout.title"
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:layout_gravity="bottom|center_horizontal"
                android:background="@color/primary"
                android:orientation="vertical"
                app:layout_collapseMode="parallax"
                app:layout_collapseParallaxMultiplier="0.3"
                >

                <LinearLayout
                    android:id="@+id/main.linearlayout.title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:orientation="vertical"
                    >

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:gravity="bottom|center"
                        android:text="ITEMS"
                        android:textColor="@android:color/white"
                        android:textSize="30sp"
                        />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:layout_marginTop="4dp"
                        android:text="My store STORE"
                        android:textColor="@android:color/white"
                        />

                </LinearLayout>
            </FrameLayout>
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="none"
        android:layout_marginTop="30dp"
        android:fitsSystemWindows="true"
        app:behavior_overlapTop="30dp"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"

        >

        <ImageView
            android:id="@+id/main.backdrop"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop"
            android:fitsSystemWindows="true"
            app:layout_collapseMode="parallax"
            />
        <include layout="@layout/content_principal" />


    </RelativeLayout>

    <android.support.v7.widget.Toolbar
        android:id="@+id/main.toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/primary"
        app:layout_anchor="@id/main.framelayout.title"
        app:theme="@style/ThemeOverlay.AppCompat.Dark"
        app:title=""
        >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            >

            <Space
                android:layout_width="@dimen/image_final_width"
                android:layout_height="@dimen/image_final_width"
                />

            <TextView
                android:id="@+id/main.textview.title"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginLeft="8dp"
                android:gravity="center_vertical"
                android:text="My Store"
                android:textColor="@android:color/white"
                android:textSize="20sp"
                />

        </LinearLayout>
    </android.support.v7.widget.Toolbar>

    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/circleView"
        android:layout_width="@dimen/image_width"
        android:layout_height="@dimen/image_width"
        android:layout_gravity="center_horizontal"
        android:src="@drawable/logo"
        app:border_color="@android:color/white"
        app:border_width="2dp"
        app:finalHeight="@dimen/image_final_width"
        app:finalYPosition="2dp"
        app:layout_behavior="com.example.dam202.mislugares.AvatarImageBehavior"
        app:startHeight="2dp"
        app:startToolbarPosition="2dp"
        app:startXPosition="2dp"
        />
</android.support.design.widget.CoordinatorLayout>

content_principal.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="10"
    android:theme="@style/ThemeOverlay.AppCompat.Light">
<android.support.v7.widget.RecyclerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".Principal"
    tools:showIn="@layout/activity_main"
    />


</LinearLayout>

Solution

  • Try to flip the order code when you set the adapter and set the linear layout of recyclerview. Because RecyclerView won't work if it doesn't have layout manager. So we have to set the linear layout first and adapter after it. It will be like this :

    layoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);
    adaptador = new AdaptadorLugares(this, lugares, lugares.extraeCursor());
    recyclerView.setAdapter(adaptador);
    

    Then try to put public ImageView foto; to your ViewHolder class. So it will be like this:

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView foto;
        public TextView nombre;
    
        public ViewHolder(View itemView) {
            super(itemView);
            nombre = (TextView) itemView.findViewById(R.id.nombrecito);
            foto = (ImageView) itemView.findViewById(R.id.foto);
         }
    }
    

    And also use the holder inside onBindViewHolder(). So it will be like this :

        @Override
        public void onBindViewHolder(ViewHolder holder, int posicion) {
            Lugar lugar = lugares.elemento(posicion);
    
            holder.nombre.setText(lugar.get_id() + lugar.getNombre());
            Picasso.with(contexto).load(lugar.getImage()).into(holder.foto);
        }
    

    And make sure getItemCount() is return more than zero