0

I'm new to Android development. I'm facing a database problem in which I'm successfully able to create a database and insert data into it. But when it comes to fetching I'm unable to do that. I have a DBAdapter.java having -

public ArrayList<String> getAllCotacts() {
    ArrayList<String> array_list = new ArrayList<String>();

    Cursor res = db.rawQuery("select * from patients", null);
    res.moveToFirst();

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

Now, I'm calling this array_list in an activity -

    DBAdapter db = new DBAdapter(this);
    final ArrayList array_list = db.getAllCotacts();
    final int itemCount = array_list.size();

    Button search = (Button) findViewById(R.id.searchbtn_searchpatient);
    search.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            for (int i = 0; i < itemCount; i++) {
                pd = new Button(myActivity.this);
                pd.setText(array_list.get(i).toString());
                pd.setTextSize(17);
                pd.setOnClickListener(pdClass);
                Layout.addView(pd, params);
            }
        }
    });

When I run my application then it says like - unfortunately, myapp has stopped.

albvs katz
  • 49
  • 4
  • have you referenced the "Layout" view or gives you null? check it and please post here log cat errors. – Marta Tenés Aug 24 '15 at 14:48
  • I've double checked the 'Layout' view. It works perfectly. `final LinearLayout Layout = (LinearLayout) findViewById(R.id.layout_searchpatient); Layout.setOrientation(LinearLayout.VERTICAL); final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);` Here's the [LogcatWindow](http://textuploader.com/ajv0m). – albvs katz Aug 24 '15 at 15:01
  • Ok then the problem is that your array_list is null, you are not retrieving any data from database. Look at this line array_list.add(res.getString(res.getColumnIndex(KEY_NAME))); and check if it is returning you something. – Marta Tenés Aug 24 '15 at 15:18

0 Answers0