M
M
Michael2016-02-22 13:03:05
Android
Michael, 2016-02-22 13:03:05

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);


    }

}

I accept id , and I was told that it is possible to implement the element receiving through the load method mushrooms = dao.load(id);
. Also on the forums I found what can be done through the querybuilder (I commented it out above in the code). But here's how to load the info into the TextView further? What to do with the mushrooms variable next? Or, for example, how to proceed with List mushroomsList ? Thank you!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis Zagaevsky, 2016-02-22
@zagayevskiy

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.

M
Michael, 2016-02-23
@M-Misha-M

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 question

Ask a Question

731 491 924 answers to any question