图像在RecyclerView上不可见

时间:2017-01-19 17:12:54

标签: java android

我试图使用糖orm数据库来存储图像和文本,但是在recylerview上没有显示任何内容,但我确认数据已存储在我的数据库中。这是我的MainActivity.class中的一些代码,它为数据库保存了值,并将保存的数据显示到recyclerview。但我的问题是,当我运行我的代码时,它没有显示任何内容。请帮忙!

    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();

1 个答案:

答案 0 :(得分:0)

我复制了你的代码,因为你说我的第一个解决方案不适合你。

这是ImageSugarPojo类,与你的相同

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);
}
}

适配器的ViewHolder类就像这样

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);
    }
}
}

在适配器类中,您可以看到我将图像和文本存储为String,因为在Sugar ORM数据库中很难将图像资源ID存储为int。

<?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>

适配器类的布局文件很简单

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);
}
}

MainActivity类

import hudson.model.*

allTests(Hudson.instance.items)


def allTests(items){
def list = []

for (item in items) {
  def name = item.getName()

  if (name.contains("API")){
  } else {
    list << item.getName()
 }
}

list.each { job ->  

mavenJob("${job}")
{
publishers {
    textFinder(/There are test failures/, '', false, false, true)
   }
  }
 }
}

这是结果

enter image description here