私は、RecyclerViewに2種類のサラダを表示するアプリケーションを用意しています。そのうち1つは正確な詳細を示すビューを表示する必要があります。私たちはそれを好まない。これらのデータは明らかにデータベースから取り出されます。私たちが詳細なビューに行くまでは、すべてが機能します - ギリシャのサラダの写真をクリックすると空白の表示が表示されますが、ギリシャのサラダをクリックすると興味深いのはギリシャのサラダだけです。では、チキンサラダの眺めはどこですか?そしてなぜその見解が立場に変わったのか。私はあなたが問題が何であるかを正確に理解できるように、写真の下にコードを付けます。データベースにデータが正しく表示されない
DatabaseHelper
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "app";
private static final int DB_VERSION = 1;
public MiodzioDatabaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE SALAD (_id INTEGER PRIMARY KEY AUTOINCREMENT, "
+ "NAME TEXT, "
+ "IMAGE_RESOURCE_ID INTEGER, "
+ "FAVORITE INTEGER);");
insertSalatki(db, "Greek salad", R.drawable.salad_greek, 0);
insertSalatki(db, "Chicken salad", R.drawable.salad_chicken, 0);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
private static void insertSalatki(SQLiteDatabase db, String name, int resourceId, int favorite){
ContentValues saladValues = new ContentValues();
salatkiValues.put("NAME", name);
salatkiValues.put("IMAGE_RESOURCE_ID", resourceId);
salatkiValues.put("Favorite", favorite);
db.insert("SALAD", null, salatkiValues);
}
}
サラダ
public class Salad {
private String name;
private int imageResourceId;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getImageResourceId() {
return imageResourceId;
}
public void setImageResourceId(int imageResourceId) {
this.imageResourceId = imageResourceId;
}
}
SaladDe tailActivity
public class SaladDetailActivity extends AppCompatActivity {
public static final String EXTRA_SALAD = "salad";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_salatki_detail);
int salads = (Integer) getIntent().getExtras().get(EXTRA_SALAD);
try {
SQLiteOpenHelper miodzioDatabaseHelper = new MiodzioDatabaseHelper(this);
SQLiteDatabase db = miodzioDatabaseHelper.getWritableDatabase();
Cursor cursor = db.query("SALATKI",
new String[]{"NAME", "IMAGE_RESOURCE_ID", "FAVORITE"},
"_id = ?",
new String[]{Integer.toString(salads)},
null, null, null);
if(cursor != null) {
if (cursor.moveToFirst()) {
do {
String nameText = cursor.getString(0);
int photoId = cursor.getInt(1);
boolean isFavorite = (cursor.getInt(2) == 1);
TextView name = (TextView) findViewById(R.id.salad_text);
name.setText(nameText);
ImageView photo = (ImageView) findViewById(R.id.salad_image);
photo.setImageResource(photoId);
photo.setContentDescription(nameText);
CheckBox favorite = (CheckBox) findViewById(R.id.favorite);
favorite.setChecked(isFavorite);
}while (cursor.moveToNext());
}
}
cursor.close();
db.close();
}catch (SQLiteException e){
Toast.makeText(this, "Database does not work!", Toast.LENGTH_SHORT).show();
}
Toolbar myChildToolbar = (Toolbar) findViewById(R.id.my_child_toolbar_salad_detail);
setSupportActionBar(myChildToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_create_order:
Intent intent = new Intent(this, AddActivity.class);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void onFavoriteClicked(View view) {
int salads = (Integer) getIntent().getExtras().get(EXTRA_SALAD);
CheckBox favorite = (CheckBox) findViewById(R.id.favorite);
ContentValues saladValues = new ContentValues();
saladValues.put("FAVORITE", favorite.isChecked());
SQLiteOpenHelper databaseHelper = new DatabaseHelper(this);
SQLiteDatabase db = databaseHelper.getWritableDatabase();
db.update("SALAD", saladValues,
"_id = ?", new String[]{Integer.toString(salads)});
db.close();
}
}
SaladMaterialFragment
public class SaladMaterialFragment extends Fragment {
private DatabaseHelper dataBaseHelper;
private Cursor cursor;
private ArrayList<Salad> arrayList = new ArrayList<>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
RecyclerView saladRecyler = (RecyclerView) inflater.inflate(R.layout.fragment_salad_material, container, false);
try{
SQLiteOpenHelper databaseHelper = new databaseHelper(inflater.getContext());
SQLiteDatabase db = databaseHelper.getReadableDatabase();
cursor = db.query("SALAD",
new String[] {"NAME", "IMAGE_RESOURCE_ID"},
null, null, null, null, null);
if(cursor != null){
if(cursor.moveToFirst()){
do{
Salad salads = new Salad();
salad.setName(cursor.getString(0));
salad.setImageResourceId(cursor.getInt(1));
arrayList.add(salad);
}while (cursor.moveToNext());
}
}
}catch (SQLiteException e){
Toast.makeText(inflater.getContext(), "Database does not work!", Toast.LENGTH_SHORT).show();
}
CaptionedImagesAdapter adapter = new CaptionedImagesAdapter(getActivity(), arrayList);
saladRecyler.setAdapter(adapter);
GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 2);
saladRecyler.setLayoutManager(gridLayoutManager);
adapter.setListener(new CaptionedImagesAdapter.Listener() {
@Override
public void onClick(int position) {
Intent intent = new Intent(getActivity(), SaladDetailActivity.class);
intent.putExtra(SaladDetailActivity.EXTRA_SALAD, position);
getActivity().startActivity(intent);
}
});
return salatkaRecyler;
}
}
SaladDetailLayout
<LinearLayout
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"
android:orientation="vertical"
tools:context="com.hfad.SaladDetailActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/my_child_toolbar_salad_detail"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:background="@android:color/holo_green_light"
android:elevation="4dp"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
/>
<TextView
android:id="@+id/salatki_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<ImageView
android:id="@+id/salad_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"/>
<CheckBox
android:id="@+id/favorite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/favorite"
android:onClick="onFavoriteClicked"/>
私はサラダギリシャをクリックしたときに、私はそれが好きなであるかどうかのオプションを指定して空のビューを取得します。私がチキンサラダを打つと、私はギリシャのサラダを見ることができます。何が起こっているのか分かりません。
私はあなたのrecyclerviewまたはアダプターを見ません。それらを使用していますか? – msecilmis
私自身は言いますが、スクリーンショットが必要ないとは思いません。 Layout xmlの内容を見たいと思います。 – Barns
@msecilmis あなたは間違いなく、私の間違いは2度私はSaladDetailActivityコードをコピーしました。私はすでにそれを修正しました。また、SaladFragmentクラスとそれがRecyclerViewである必要があります。私はインターフェイスリスナを持っているRecyclerAdapterクラスを追加しました。 – newActivity21