title: Display data from SQLite database into ListView in Android
body: <p>Although this question has been asked many times on here, I just can't find the proper answer to fit my code. I realise it may be something small, but I just can't seem to find the problem as I'm only really new to this.</p>
  
  <p>Here's my code <strong>getClientNames</strong> in <strong>DatabaseHelper</strong> class:</p>
  
  <pre><code>public Cursor getSitesByClientname(String id) {
      String[] args={id};
  
  Cursor myCursor = db.rawQuery("SELECT client_sitename FROM " + CLIENT_SITES_TABLE + " WHERE client_id=?", args);
          String results = "";
          /*int count = myCursor.getCount();
          String[] results = new String[count + 1];
          int i = 0;*/
  
           if (myCursor != null) {           
                  if(myCursor.getCount() &gt; 0)
                  {   
                      for (myCursor.moveToFirst(); !myCursor.isAfterLast(); myCursor.moveToNext())
                      {
                          results = results + myCursor.getString(myCursor.getColumnIndex("client_sitename")); 
                      }
                  }
              }
              return results;
  }
  </code></pre>
  
  <p>One problem is that I'm returning a 'String' to the 'Cursor', but I'm not sure what is the best way around this, as I think I should be returning a Cursor.</p>
  
  <p>Here's the <strong>ClientSites</strong> class where I want to display the data:</p>
  
  <pre><code>public class ClientSites extends Activity {
  
  //public final static String ID_EXTRA="com.example.loginfromlocal._ID";
  
          private DBUserAdapter dbHelper = null;
      private Cursor ourCursor = null;
      private Adapter adapter=null;
  
  
      @SuppressWarnings("deprecation")
      @SuppressLint("NewApi")
      public void onCreate(Bundle savedInstanceState) {
          try
          {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.client_sites);
  
              Intent i = getIntent();
              String uID = String.valueOf(i.getIntExtra("userID", 0));
  
              ListView myListView = (ListView)findViewById(R.id.myListView);
  
              dbHelper = new DBUserAdapter(this);
  
              //dbHelper.createDatabase();
  
              dbHelper.openDataBase();
  
              ourCursor = dbHelper.getSitesByClientname(uID);
              Log.e("ALERT", uID.toString());
  
              startManagingCursor(ourCursor);
              Log.e("ERROR", "After start manage cursor: ");
  
              //@SuppressWarnings("deprecation") 
              //SimpleCursorAdapter adapter = new SimpleCursorAdapter(getBaseContext(), R.id.myListView, null, null, null);
              CursorAdapter adapter = new SimpleCursorAdapter(this, R.id.myListView, null, null, null, 0);
              adapter = new Adapter(ourCursor);
  
              //Toast.makeText(ClientSites.this, "Booo!!!", Toast.LENGTH_LONG).show();
  
              myListView.setAdapter(adapter);
  
              myListView.setOnItemClickListener(onListClick);
  
  
          }
          catch (Exception e)
          {
              Log.e("ERROR", "XXERROR IN CODE: " + e.toString());
              e.printStackTrace();
          } 
  
      }
  
      private AdapterView.OnItemClickListener onListClick=new AdapterView.OnItemClickListener() {
          public void onItemClick(AdapterView&lt;?&gt; parent,
                                  View view, int position,
                                  long id) 
          {
              Intent i=new Intent(ClientSites.this, InspectionPoints.class);
  
              i.putExtra(ID_EXTRA, String.valueOf(id));
              startActivity(i);
  
          }
      };
  
  
      class Adapter extends CursorAdapter {
          @SuppressWarnings("deprecation")
          Adapter(Cursor c) {
            super(ClientSites.this, c);
          }
  
          //@Override
          public void bindView(View row, Context ctxt,
                             Cursor c) {
              Holder holder=(Holder)row.getTag();
              holder.populateFrom(c, dbHelper);
          }
          @Override
          public View newView(Context ctxt, Cursor c,
                             ViewGroup parent) {
            LayoutInflater inflater=getLayoutInflater();
            View row = inflater.inflate(R.layout.row, parent, false);
            Holder holder=new Holder(row);
            row.setTag(holder);
            return(row);
          }
        }
  
      static class Holder {
          private TextView name=null;
  
          Holder(View row) {
              name=(TextView)row.findViewById(R.id.ingredientText);
          }
  
          void populateFrom(Cursor c, DBUserAdapter r) {
              name.setText(r.getName(c));
          }
      }
  }
  </code></pre>
  
  <p>Here is the code I'm now using to try and display the data in the Listview. I have altered it somewhat from my original attempt, but still not sure what I'm doing wrong.</p>
  
  <pre><code>public void onCreate(Bundle savedInstanceState) {
          try
          {
              super.onCreate(savedInstanceState);
              //setContentView(R.layout.client_sites);
  
              Intent i = getIntent();
              String uID = String.valueOf(i.getIntExtra("userID", 0));
              //int uID = i.getIntExtra("userID", 0);
  
              //ListView myListView = (ListView)findViewById(R.id.myListView);
  
              dbHelper = new DBUserAdapter(this);
  
              dbHelper.createDatabase();
  
              dbHelper.openDataBase();
  
              String[] results = dbHelper.getSitesByClientname(uID);
  
              //setListAdapter(new ArrayAdapter&lt;String&gt;(ClientSites.this, R.id.myListView, results));
              //adapter = new ArrayAdapter&lt;String&gt;(ClientSites.this, R.id.myListView, results);
              setListAdapter(new ArrayAdapter&lt;String&gt;(ClientSites.this, R.layout.client_sites, results));
  
              //ListView myListView = (ListView)findViewById(R.id.myListView);
              ListView listView = getListView();
              listView.setTextFilterEnabled(true);
  
              //ourCursor = dbHelper.getSitesByClientname(uID);
              //Log.e("ALERT", uID.toString());
  
              //startManagingCursor(ourCursor);
              //Log.e("ERROR", "After start manage cursor: ");
  
              //@SuppressWarnings("deprecation") 
              //SimpleCursorAdapter adapter = new SimpleCursorAdapter(getBaseContext(), R.id.myListView, null, null, null); // LOOK AT THIS IN THE MORNING!!!!!!!!!!! 
              //CursorAdapter adapter = new SimpleCursorAdapter(this, R.id.myListView, null, null, null, 0);
              //adapter = new Adapter(ourCursor);
  
              //Toast.makeText(ClientSites.this, "Booo!!!", Toast.LENGTH_LONG).show();
  
              //myListView.setAdapter(adapter);
  
              //myListView.setOnItemClickListener(onListClick);
  </code></pre>