For a very long time, I can’t cope with what I’ve planned, namely: how to include a ready-made database in the project. I have already tried a lot of ways, different libraries ... but without success. Minutes probably 30 ago, came across a similar question: question
A beginner is still in programming, half a year as I was doing android development and was about to encounter databases. Now I will demonstrate my finished database and code snippets.
class DBAssetHelper
import android.content.Context; import com.readystatesoftware.sqliteasset.SQLiteAssetHelper; public class DBAssetHelper extends SQLiteAssetHelper { private static final int DBVersion = 1; private static final String DBName = "accounts"; public DBAssetHelper(Context context) { super(context, DBName, null, DBVersion); } } class DatabaseHelper
import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class DatabaseHelper extends SQLiteOpenHelper { private static final int DBVersion = 1; private static final String DBName = "accounts"; private static final String TableName = "accounts"; private static final String mTable = "CREATE TABLE " + TableName + "(" + "_id" + " INTEGER PRIMARY KEY AUTOINCREMENT, " + "CONTENT_ONE" + " TEXT, " + "CONTENT_TWO" + " TEXT)"; DatabaseHelper(Context context) { super(context, DBName, null, DBVersion); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(mTable); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS " + TableName); this.onCreate(db); } } class ListFragment extends Fragment
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { DBAssetHelper dbSetup = new DBAssetHelper(getActivity()); dbSetup.getWritableDatabase(); View v = inflater.inflate(R.layout.fragment_list, container, false); ArrayList<ExampleItem> exampleList = new ArrayList<>(); //exampleList.add(new ExampleItem(R.drawable.ic_search_black_24dp, "1000", "Денежные средства")); //exampleList.add(new ExampleItem(R.drawable.ic_search_black_24dp, "1010", "Денежные средства в кассе")); try{ SQLiteOpenHelper databaseHelper = new DatabaseHelper(getActivity()); SQLiteDatabase db = databaseHelper.getReadableDatabase(); Cursor cursor = db.query( "accounts", new String[]{"CONTENT_ONE", "CONTENT_TWO"}, "_id = ?", new String[3], null, null, null); do { final String CONTENT_ONE = cursor.getString(1); final String CONTENT_TWO = cursor.getString(2); exampleList.add(new ExampleItem(R.drawable.ic_search_black_24dp, CONTENT_ONE, CONTENT_TWO)); }while (cursor.moveToNext()); cursor.close(); db.close(); } catch (SQLiteException e) { Toast.makeText(getActivity(), "База данных недоступна", Toast.LENGTH_SHORT).show(); } mRecyclerView = v.findViewById(R.id.recyclerView); mRecyclerView.setHasFixedSize(true); mLayoutManager = new LinearLayoutManager(getActivity()); mAdapter = new ExampleAdapter(exampleList); mRecyclerView.setLayoutManager(mLayoutManager); mRecyclerView.setAdapter(mAdapter); return v; } The action takes place in the fragment, and as I understood, in order to take the Context, you need to apply the getActivity () method. It seems to have described the problem to the full. there should be something like this, only this method does not suit me, I need to take information from the finished database.


