あなたは私の最初のソリューションは、あなたのために動作しないと言ったので、私はあなたのコードを複製し。
これは
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;
}
}
アダプタの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);
}
}
のようなものです、あなたは私が保存されていることがわかります、それはあなたのものと同じであるImageSugarPojoクラスですSugar ORMデータベースにイメージリソースIDをintとして格納することは困難なため、イメージとテキストをStringとして扱います。
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>
MainActivityクラス
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);
}
}
である。これは、結果である
アダプタをしてください投稿 – ste9206