Ekenne Chilex
Ekenne Chilex

Reputation: 41

Image not visible on RecyclerView

Am trying to use a sugar orm database to store image and text, but nothing is been displayed on the recylerview although am getting a confirmation that data is been stored in my database. This is the bit of code from my MainActivity.class that is saving value to the database, as well as to display the saved data to the recyclerview. But my problem is that its not showing anything when I run my code. Please help!

    recyclerView =(RecyclerView) findViewById(R.id.recyclerView);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    imagepojo = new ImageSugarPojo();
    int image = imagepojo.setImage(R.drawable.atama);
    String text = imagepojo.setText("Soup");
    imageSugar = new ArrayList<ImageSugarPojo>();
    imageSugar.add(new ImageSugarPojo( image,text));
    imagepojo.save();
    long count = ImageSugarPojo.count(ImageSugarPojo.class);
    Log.i("Count: ",String.valueOf(count));


    imageSugar = new ArrayList<ImageSugarPojo>();
    imageSugar = ImageSugarPojo.listAll(ImageSugarPojo.class);
    recyclerAdapter = new RecyclerAdapter(this,imageSugar);
    recyclerView.setAdapter(recyclerAdapter);
    recyclerAdapter.notifyDataSetChanged();

Upvotes: 0

Views: 2768

Answers (1)

Inducesmile
Inducesmile

Reputation: 2493

I replicated your code since you said my first solution did not work for you.

This is the ImageSugarPojo class it is the same with yours

import com.orm.SugarRecord;

public class ImageSugarPojo extends SugarRecord{

private String image;
private String text;

public ImageSugarPojo(){}

public ImageSugarPojo(String image, String text){
    this.image =image;
    this.text = text;
}

public String getImage() {
    return image;
}

public void setImage(String image) {
    image = image;
}

public String getText() {
    return text;
}

public void setText(String text) {
    this.text = text;
}
}

The ViewHolder class for the Adapter is like this

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class CategoryViewHolder extends RecyclerView.ViewHolder{

public TextView categoryName;
public ImageView categoryImage;
public View mItemView;


public CategoryViewHolder(View itemView) {
    super(itemView);
    mItemView = itemView;
    categoryName = (TextView)itemView.findViewById(R.id.category_name);
    categoryImage = (ImageView)itemView.findViewById(R.id.category_image);
}
}

In the adapter class, you can see that I stored the image and text as String since it is hard to store image resource id as int in Sugar ORM database.

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

public class CategoryAdapter extends RecyclerView.Adapter<CategoryViewHolder>{

private Context context;
private List<ImageSugarPojo> categoryObject;

public CategoryAdapter(Context context, List<ImageSugarPojo> categoryObject) {
    this.context = context;
    this.categoryObject = categoryObject;
}

@Override
public CategoryViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.menu_category_list, parent, false);
    return new CategoryViewHolder(layoutView);
}

@Override
public void onBindViewHolder(CategoryViewHolder holder, int position) {
    final ImageSugarPojo catObject = categoryObject.get(position);
    holder.categoryName.setText(catObject.getText());
    int resId = getResourseId(context, "drawable", catObject.getImage(), context.getPackageName());
}

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

public static int getResourseId(Context context, String pVariableName, String pResourcename, String pPackageName) throws RuntimeException {
    try {
        return context.getResources().getIdentifier(pVariableName, pResourcename, pPackageName);
    } catch (Exception e) {
        throw new RuntimeException("Error getting Resource ID.", e);
    }
}
}

The layout file for the adapter class is simple

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="4dp"
    android:layout_marginTop="4dp"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="8dp">

    <ImageView
        android:id="@+id/category_image"
        android:layout_width="200dp"
        android:layout_height="150dp"
        android:adjustViewBounds="true"
        android:contentDescription="@string/app_name"
        android:src="@drawable/sandals" />

    <TextView
        android:id="@+id/category_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="@string/pizza"
        android:textColor="@color/colorPrimaryDark"
        android:textSize="14dp"
        android:textStyle="bold" />

</LinearLayout>

The MainActivity class

public class MainActivity extends AppCompatActivity {

private RecyclerView recyclerView;

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

    ImageSugarPojo imagepojo = new ImageSugarPojo();
    imagepojo.setImage("sandals");
    imagepojo.setText("Soup");
    imagepojo.save();

    long count = ImageSugarPojo.count(ImageSugarPojo.class);
    Log.i("Count: ",String.valueOf(count));
    Log.i("Display: ",String.valueOf(imagepojo.getImage()));

    recyclerView = (RecyclerView)findViewById(R.id.category_menu);
    GridLayoutManager mGrid = new GridLayoutManager(this, 2);
    recyclerView.setLayoutManager(mGrid);
    recyclerView.setHasFixedSize(true);

    List<ImageSugarPojo> imageSugar = ImageSugarPojo.listAll(ImageSugarPojo.class);

    CategoryAdapter mAdapter = new CategoryAdapter(this, imageSugar);
    recyclerView.setAdapter(mAdapter);
}
}

This is the result

enter image description here

Upvotes: 2

Related Questions