title: Display data from database in listview on android
body: <p>I have 3 class and DB</p>
  
  <p>1.class MainActivity</p>
  
  <pre><code>public class MainActivity extends ListActivity {
      private static final String DB_NAME = "sajgo_v1";
      private static final String TABLE_NAME = "PREHRANA";
      private static final String PREHRANA_ID = "_id";
      private static final String NAZIV_PREHRANE = "naziv_prehrane";
  
      private SQLiteDatabase database;
      private ListView listView;
      private ArrayList&lt;String&gt; prehrana;
  
  
      public static final String ID_EXTRA="naziv_prehrane";
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
  
          ExternalDbOpenHelper dbOpenHelper = new ExternalDbOpenHelper(this, DB_NAME);
          database = dbOpenHelper.openDataBase();
  
          fillPrehrana();
          setUpList();      
      }
  
      private void setUpList() {
  
          setListAdapter(new ArrayAdapter&lt;String&gt;(this,
                          android.R.layout.simple_list_item_1, prehrana));
          listView = getListView();
  
  
          listView.setOnItemClickListener(new OnItemClickListener() {
              public void onItemClick(AdapterView&lt;?&gt; parent, View view,
                                  int position,long id) {
          Intent i = new Intent (MainActivity.this, Jelovnik.class);
          i.putExtra(ID_EXTRA, String.valueOf(id));
          startActivity(i);
              }
          });
      }
  
      private void fillPrehrana() {
          prehrana = new ArrayList&lt;String&gt;();
          Cursor prehranaCursor = database.query(TABLE_NAME,
                                               new String[] 
                                               {PREHRANA_ID, NAZIV_PREHRANE},
                                               null, null, null, null
                                               , null);
          prehranaCursor.moveToFirst();
          if(!prehranaCursor.isAfterLast()) {
              do {
                  String name = prehranaCursor.getString(1);
                  prehrana.add(name);
              } while (prehranaCursor.moveToNext());
          }
          prehranaCursor.close();
      }
  
  }
  </code></pre>
  
  <p>2.Class Jelovnik</p>
  
  <pre><code>package com.prehrana;
  
  import android.app.Activity;
  import android.os.Bundle;
  import android.widget.TextView;
  
  public class Jelovnik extends Activity {
  
      String passedVar=null;
      private TextView passedView=null;
  
      protected void onCreate(Bundle savedInstanceState){
      super.onCreate(savedInstanceState);
      setContentView(R.layout.jelovnik);
  
      passedVar=getIntent().getStringExtra(MainActivity.ID_EXTRA);
  
      passedView=(TextView)findViewById(R.id.passed);
  
      passedView.setText("Kliknuli ste na " + passedVar);
  }
  }
  </code></pre>
  
  <p>In this class when i click it shows me number</p>
  
  <p>3.Class DB helper</p>
  
  <p>public class ExternalDbOpenHelper extends SQLiteOpenHelper {</p>
  
  <pre><code>    public static String DB_PATH;
  
      public static String DB_NAME;
      public SQLiteDatabase database;
      public final Context context;
  
      public SQLiteDatabase getDb() {
          return database;
      }
  
      public ExternalDbOpenHelper(Context context, String databaseName) {
          super(context, databaseName, null, 1);
          this.context = context;
  
          String packageName = context.getPackageName();
          DB_PATH = String.format("//data//data//%s//databases//", packageName);
          DB_NAME = databaseName;
          openDataBase();
      }
  
  
      public void createDataBase() {
          boolean dbExist = checkDataBase();
          if (!dbExist) {
              this.getReadableDatabase();
              try {
                  copyDataBase();
              } catch (IOException e) {
                  Log.e(this.getClass().toString(), "Copying error");
                  throw new Error("Error copying database!");
              }
          } else {
              Log.i(this.getClass().toString(), "Database already exists");
          }
      }
  
      private boolean checkDataBase() {
          SQLiteDatabase checkDb = null;
          try {
              String path = DB_PATH + DB_NAME;
              checkDb = SQLiteDatabase.openDatabase(path, null,
                      SQLiteDatabase.OPEN_READONLY);
          } catch (SQLException e) {
              Log.e(this.getClass().toString(), "Error while checking db");
          }
  
          if (checkDb != null) {
              checkDb.close();
          }
          return checkDb != null;
      }
  
  
      private void copyDataBase() throws IOException {
  
          InputStream externalDbStream = context.getAssets().open(DB_NAME);
  
  
          String outFileName = DB_PATH + DB_NAME;
  
  
          OutputStream localDbStream = new FileOutputStream(outFileName);
  
  
          byte[] buffer = new byte[1024];
          int bytesRead;
          while ((bytesRead = externalDbStream.read(buffer)) &gt; 0) {
              localDbStream.write(buffer, 0, bytesRead);
          }
  
          localDbStream.close();
          externalDbStream.close();
  
      }
  
      public SQLiteDatabase openDataBase() throws SQLException {
          String path = DB_PATH + DB_NAME;
          if (database == null) {
              createDataBase();
              database = SQLiteDatabase.openDatabase(path, null,
                  SQLiteDatabase.OPEN_READWRITE);
          }
          return database;
      }
      @Override
      public synchronized void close() {
          if (database != null) {
              database.close();
          }
          super.close();
      }
      @Override
      public void onCreate(SQLiteDatabase db) {}
      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}
  }
  </code></pre>
  
  <p>My problem is</p>
  
  <p>I want to call data from data base in listview</p>