Answer the question
In order to leave comments, you need to log in
How to pull data from DB into Textview in greenDao ORM?
Hello. I have a recyclerView with a list of items. Each element has its own id (as in ListView ), I need it to go to the second activity depending on the clicked id and pull out the necessary information from the description column in the Textview. The database has a simple form: it is _id , mushroom , description . Here is my code:
public class DetailActivity extends AppCompatActivity {
private long id = 0;
public Mushrooms mushrooms;
public TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
String item = getIntent().getExtras().getString("title");
id = getIntent().getExtras().getInt("id" , 0);
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this , "mushrooms-db" , null);
SQLiteDatabase db = helper.getWritableDatabase();
DaoMaster daoMaster = new DaoMaster(db);
DaoSession daoSession = daoMaster.newSession();
MushroomsDao dao = daoSession.getMushroomsDao();
mushrooms = dao.load(id);
// List<Mushrooms> mushroomsList = dao.queryBuilder().where(MushroomsDao.Properties.Description.in(mushrooms)).list();
textView = (TextView) findViewById(R.id.textViewDetail);
}
}
mushrooms = dao.load(id);
Answer the question
In order to leave comments, you need to log in
You should pull up Java Core, work with collections ... List<Mushrooms>
- this is a list of objects of the Mushrooms type. It has an isEmpy() method that checks if the list is empty. There is a get(int) method - returning an element at a specific position, counting starts from 0.
Textview has a setText(CharSequance) method - you can pass the string you want to display there.
Your Mushrooms probably have some get*() methods - apparently you can get info from them.
Well, the docks on GreenDao should be read. For example, Query has a unique() method. But, in my opinion, in your builder the request is incorrectly formed, it is not clear.
cheers, it seems like it turned out the truth is not the id that you need to display, but this is a problem with the _id numbering itself. The main thing is that the text outputs
if (id !=0) {
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "mushrooms-db", null);
SQLiteDatabase db = helper.getReadableDatabase();
DaoMaster daoMaster = new DaoMaster(db);
DaoSession daoSession = daoMaster.newSession();
MushroomsDao dao = daoSession.getMushroomsDao();
mushrooms = dao.load(id);
if (mushrooms != null) {
textView.setText(mushrooms.getDescription());
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question