[英]Image not visible on RecyclerView
我正在嘗試使用糖 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();
我復制了你的代碼,因為你說我的第一個解決方案不適合你。
這是 ImageSugarPojo 類,它與您的相同
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;
}
}
Adapter的ViewHolder類是這樣的
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);
}
}
在適配器類中,您可以看到我將圖像和文本存儲為 String,因為在 Sugar ORM 數據庫中很難將圖像資源 id 存儲為 int。
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);
}
}
}
適配器類的布局文件很簡單
<?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);
}
}
這是結果
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.