Хочу при открытии фрагмента считать данные из БД и добавить их в ListView. Данные считываются, судя по отладке они добавляются в ListView, но на экране не отображаются. Также не получается изменить текст в TextView.
Код во фрагменте:
package apk.fridge.ru.fridge;
import android.app.Fragment;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import DBSetup.DBAssetHelper;
public class FragmentAllIngredients extends Fragment {
View view;
Context context;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_all_ingredients, null);
context = getActivity().getApplicationContext();
DBAssetHelper dbSetup = new DBAssetHelper(context);
dbSetup.getWritableDatabase();
try {
SQLiteOpenHelper databaseHelper = new DBOpenHelper(context);
SQLiteDatabase db = databaseHelper.getReadableDatabase();
String[] columns = {DBOpenHelper.TableAllIngredientsColumnName};
Cursor cursor = db.query(DBOpenHelper.TableAllIngredients, columns, null, null, null, null, DBOpenHelper.TableAllIngredientsColumnName);
if (cursor.moveToFirst()) {
String[] ingredients = new String[cursor.getCount()];
int i = 0;
do {
ingredients[i] = cursor.getString(0);
i++;
}
while (cursor.moveToNext());
ListView lviAllIngredients = view.findViewById(R.id.lvi_allIngredients);
ArrayAdapter<String> adapter = new ArrayAdapter<>(context, android.R.layout.simple_list_item_1, ingredients);
lviAllIngredients.setAdapter(adapter);
}
cursor.close();
db.close();
} catch (SQLiteException e) {
Toast.makeText(context, "База данных недоступна" + '\n' + e.getMessage(), Toast.LENGTH_SHORT).show();
}
return inflater.inflate(R.layout.fragment_all_ingredients, container, false);
}
}
В чем может быть причина?