0

I am trying to create a database of restaurants and their items for an app as a starter project to get familiar with android. I have a list from a previous activity where a user clicks on the name of the restaurant and the items are shown. I created a helperDB class to handle the insert statements and database setup, although when I called my insert method from inside a new thread, they do not seem to execute. I have provided the code below:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_bar);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    Intent intent = getIntent();
    String barName = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
    final TextView tv = (TextView) findViewById(R.id.name);
    tv.setText(barName);

    restaurants = new ArrayList<String>();
    mydb = new DBHelper(this);

   new Thread(new Runnable() {
        @Override
        public void run() {
            mydb.insertDrink("cake", "McD's", 8);
            mydb.insertDrink("muffin", "The Woods", 8);
            restaurants = mydb.getDrinks("The Woods");
            System.out.println(restaurants);
        }
    }).start();


    ArrayAdapter arrayAdapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1, restaurants);

    obj = (ListView)findViewById(R.id.listview);
    obj.setAdapter(arrayAdapter);
}

The code for the insert statement and getDrinks method are as follows:

public boolean insertDrink(String drink, String name, int price){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();

    contentValues.put("drink", drink);
    contentValues.put("name", name);
    contentValues.put("price", price);
    db.insert("Bars", null, contentValues);
    return true;
}
public ArrayList<String> getDrinks(String name){
    ArrayList<String> arrayList = new ArrayList<>();

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor res = db.rawQuery("select * from Bars", null);
    res.moveToFirst();

    while(res.isAfterLast() == false){
        arrayList.add(res.getString(res.getColumnIndex(BAR_COLUMN_DRINK)));
        res.moveToNext();
    }
    return arrayList;
}

I know that I am not supposed to access any android toolkits from any thread besides the UI thread, although I don't think I am doing that. If this is not the normal way to populate a SQLite android database, I of course am willing to learn where to do that as well.

2
  • 1
    Are you basing your statement of they do not seem to execute on them not showing in your list? Commented Sep 1, 2015 at 20:39
  • 2
    That is probably because the ArrayAdapter is created before the restaurants variable is initialized, you should take a look at AsyncTask Commented Sep 1, 2015 at 20:41

2 Answers 2

2

although when I called my insert method from inside a new thread, they do not seem to execute.

How can you say that? Are you seeing data entries in database? Or by looking only for restaurant ArrayList?

My doubt is you are not getting your Restaurant ArrayList because your Thread runs asynchronously.

And your next statement after Thread executed without waiting to fill restaurnat arrayList.

Solution:

Use AsyncTask call your database stuff in doInBackground() and get result in onPostExecute() and set adapter with result in the same method.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok, when I move the ArrayAdapter into the onPostExecute() method, the "this" context no longer works because I am no long in the on create scope. What should I change that to?
I used DisplayBar.this as a context and it worked!
2

I suggest you to use AsyncTask.

Maybe something like this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_bar);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    Intent intent = getIntent();
    String barName = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
    final TextView tv = (TextView) findViewById(R.id.name);
    tv.setText(barName);

    new AsyncTask<Void, Void, ArrayList<String>> {
         protected Long doInBackground() {
           DBHelper mydb = new DBHelper(ThisActivityClassName.this);
           mydb.insertDrink("cake", "McD's", 8);
           mydb.insertDrink("muffin", "The Woods", 8);
           return mydb.getDrinks("The Woods");
         }

         protected void onPostExecute(ArrayList<String> restaurants) {
           ArrayAdapter arrayAdapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1, restaurants);

           obj = (ListView)findViewById(R.id.listview);
           obj.setAdapter(arrayAdapter);
         }
     }.execute();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.