Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing listviews with a database
    primarykey
    data
    text
    <p>and good day. My project is having 2 activities, first one to insert data into the database which works well, but then I want to show a list view of the data within the database in another activity. </p> <p>I've tried local data eg fred, george in an array and the list view works great however, when I try to use the database. The activity crashes. It seems that I am unable to use "database.open"</p> <pre><code>public class MyListActivity extends Activity{ DatabaseAdapter database; //Creates item_details based on the ListItemDetails class ListItemDetails item_details; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_list); //Uses the arraylist and set the result ArrayList&lt;ListItemDetails&gt; result = GetSearchResults(); ListView lv = (ListView)findViewById(R.id.listView1); lv.setAdapter(new CustomListAdapter(getApplicationContext(),result)); } private ArrayList&lt;ListItemDetails&gt; GetSearchResults() { ArrayList&lt;ListItemDetails&gt; results = new ArrayList&lt;ListItemDetails&gt;(); item_details = new ListItemDetails(); /* database.open(); Cursor c = database.getAllContacts(); if (c.moveToFirst()) { do { item_details.setFirstName(c.getString(1)); results.add(item_details); } while (c.moveToNext()); } */ return results; } } </code></pre> <p>Here is the main activity. The database opens fine when I use the buttonShow, so I tried buttonTest and storing them into the list but it got a bit confusing for me.</p> <pre><code>public class FamousPersonActivity extends Activity { EditText editFirstName, editLastName; Button buttonAdd, buttonShow, buttonTest; ListView lv; ListItemDetails item_details; DatabaseAdapter database; int request_Code = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_famous_person); database = new DatabaseAdapter(this); lv = (ListView)findViewById(R.id.listView1); //ArrayList&lt;ListItemDetails&gt; result = GetSearchResults(); //lv.setAdapter(new CustomListAdapter(getApplicationContext(),result)); editFirstName = (EditText)findViewById(R.id.editFirstName); editLastName = (EditText)findViewById(R.id.editLastName); buttonAdd = (Button)findViewById(R.id.buttonAdd); buttonShow = (Button)findViewById(R.id.buttonShow); buttonTest = (Button)findViewById(R.id.buttonTest); buttonAdd.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { Toast.makeText(getApplicationContext(), "added to database", Toast.LENGTH_SHORT).show(); database.open(); long id = database.insertContact("Test Contact", "Test Email") ; database.close(); } }); buttonTest.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent("com.id11020260.exercise6part2.MyListActivity"); startActivityForResult(i, request_Code); } }); buttonShow.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { try { String destPath = "/data/data/" + getPackageName() + "/databases"; File f = new File(destPath); if (!f.exists()) { f.mkdirs(); f.createNewFile(); //---copy the db from the assets folder into // the databases folder--- CopyDB(getBaseContext().getAssets().open("mydb"), new FileOutputStream(destPath + "/MyDB")); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } //---get all contacts--- database.open(); Cursor c = database.getAllContacts(); if (c.moveToFirst()) { do { DisplayContact(c); } while (c.moveToNext()); } database.close(); } }); } private ArrayList&lt;ListItemDetails&gt; GetSearchResults() { ArrayList&lt;ListItemDetails&gt; results = new ArrayList&lt;ListItemDetails&gt;(); item_details = new ListItemDetails(); database.open(); Cursor c = database.getAllContacts(); if (c.moveToFirst()) { do { item_details.setFirstName(c.getString(1)); results.add(item_details); } while (c.moveToNext()); database.close(); } return results; } public void CopyDB(InputStream inputStream, OutputStream outputStream) throws IOException { //---copy 1K bytes at a time--- byte[] buffer = new byte[1024]; int length; while ((length = inputStream.read(buffer)) &gt; 0) { outputStream.write(buffer, 0, length); } inputStream.close(); outputStream.close(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.famous_person, menu); return true; } public void DisplayContact(Cursor c) { Toast.makeText(this, "id: " + c.getString(0) + "\n" + "Name: " + c.getString(1) + "\n" + "Email: " + c.getString(2), Toast.LENGTH_LONG).show(); } } </code></pre> <p>This is the stack trace, I'm not sure if this is entirely correct</p> <p><img src="https://i.stack.imgur.com/hmJcC.png" alt="enter image description here"></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload