Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

App crashes when trying to display Listview from Database

android listview sqlite

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1 DarkHeart

DarkHeart

    CC Newcomer

  • Member
  • PipPip
  • 16 posts

Posted 29 March 2015 - 08:51 PM

I am trying to display content from all columns of a certain table in a listview. I am using a navigation drawer, i dont know if this in any ways effects the listview.This is the code i have tried and everytime i run the activity the app crashes.

 

Database

 

package com.azmal.example.com.facilitiesreviewapp;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class SQL extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "details";

    private SQLiteDatabase database;
    private SQL dbHelper;
    private Context ourContext;




    static final String TABLE_NAME = "details";
    static final String COL_StationID = "StationID";
    static final String COL_StationName = "StationName";
    static final String COL_StationType = "StationType";
    static final String COL_Facilities = "Facilities";
    static final String COL_Email = "Email";
    static final String COL_Location = "Location";

    public SQL(Context context) {
        super(context, DATABASE_NAME, null, 1);
        database = getWritableDatabase();

    }

    @Override

    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE details (StationID TEXT PRIMARY KEY, StationName TEXT, StationType TEXT, Facilities TEXT, Location TEXT, Email TEXT );");
    }

    @Override

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        android.util.Log.w(this.getClass().getName(),
                DATABASE_NAME + " database upgrade to version " + newVersion + " old data lost");
        db.execSQL("DROP TABLE IF EXISTS details");
        onCreate(db);
    }

    public long insertStation(String StationID, String StationName, String StationType, String Facilities, String Location, String Email) {
        ContentValues rowValues = new ContentValues();


        rowValues.put("StationID", StationID);
        rowValues.put("StationName", StationName);
        rowValues.put("StationType", StationType);
        rowValues.put("Facilities", Facilities);
        rowValues.put("Location", Location);
        rowValues.put("Email", Email);

        return database.insertOrThrow("details", null, rowValues);
    }

    public long getNumberOfRecords() {


        Cursor c = database.query("details", null, null, null, null, null, null);
        return c.getCount();

    }


    public Cursor fetchAllStations() {

        Cursor mCursor = database.query(TABLE_NAME, new String[] {COL_StationID,
                        COL_StationName, COL_StationType, COL_Facilities, COL_Location, COL_Email},
                null, null, null, null, null, null);

        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }


    public void deleteStation(String StationID) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete("details", "StationID = ?",
                new String[] { String.valueOf(StationID) });
        db.close();
    }
}

DisplayStations.java

package com.azmal.example.com.facilitiesreviewapp;

import android.content.res.TypedArray;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class DisplayStations extends BaseActivity {
    private String[] navMenuTitles;
    private TypedArray navMenuIcons;
    private SQL dbHelper;
    private SimpleCursorAdapter dataAdapter;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.display_stations);
        dbHelper = new SQL(this);
        navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items); // load
        // titles
        // from
        // strings.xml

        navMenuIcons = getResources()
                .obtainTypedArray(R.array.nav_drawer_icons);// load icons from
        // strings.xml

        set(navMenuTitles, navMenuIcons);
        displayListView();


    }

    private void displayListView() {


        Cursor cursor = dbHelper.fetchAllStations();


        // The desired columns to be bound
        String[] columns = new String[] {
                SQL.COL_StationID,
                SQL.COL_StationName,
                SQL.COL_StationType,
                SQL.COL_Facilities,
                SQL.COL_Location,
                SQL.COL_Email
        };

        // the XML defined views which the data will be bound to
        int[] to = new int[] {
                R.id.ID,
                R.id.SName,
                R.id.Stype,
                R.id.SFacilities,
                R.id.SLocation,
                R.id.SEmail
        };

        // create the adapter using the cursor pointing to the desired data
        //as well as the layout information
        dataAdapter = new SimpleCursorAdapter(
                this, R.layout.stations,
                cursor,
                columns,
                to,
                0);

        ListView listView = (ListView) findViewById(R.id.listView1);
        // Assign adapter to ListView
        listView.setAdapter(dataAdapter);

    }

}

display_stations.xml

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- The main content view -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
        <!-- Add content here -->
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <EditText android:id="@+id/myFilter" android:layout_width="match_parent"
                android:layout_height="wrap_content" android:ems="10"
                android:hint="@string/some_hint">
                <requestFocus />
            </EditText>

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/StationsAdded"
                android:textSize="15sp" />

            <ListView android:id="@+id/listView1" android:layout_width="fill_parent"
                android:layout_height="fill_parent" />


       </LinearLayout>
     </ScrollView>


    </FrameLayout>
    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#111"/>

</android.support.v4.widget.DrawerLayout>

stations.xml

 



<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="6dip"
    android:id="@+id/lists">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Station ID: "
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:text="Station Name: "
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_below="@+id/textView2"
        android:text="Station Type: "
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView3"
        android:layout_below="@+id/textView3"
        android:text="Facilities: "
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Location: "
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_below="@+id/textView4"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Email: "
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_below="@+id/textView5"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:id="@+id/ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:layout_above="@+id/textView2"
        android:layout_centerHorizontal="true" />

    <TextView
        android:id="@+id/SName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:layout_alignBottom="@+id/textView2"
        android:layout_alignLeft="@+id/Stype"
        android:layout_alignStart="@+id/Stype" />

    <TextView
        android:id="@+id/Stype"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"

        android:layout_alignBottom="@+id/textView3"
        android:layout_alignLeft="@+id/SFacilities"
        android:layout_alignStart="@+id/SFacilities" />

    <TextView
        android:id="@+id/SFacilities"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:layout_alignBottom="@+id/textView4"
        android:layout_centerHorizontal="true" />
    <TextView
        android:id="@+id/SLocation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:layout_alignBottom="@+id/textView5"
        android:layout_alignLeft="@+id/SEmail"
        android:layout_alignStart="@+id/SEmail" />

    <TextView
        android:id="@+id/SEmail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:layout_alignBottom="@+id/textView6"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

just incase anyone needs it, here is the full source code. its just a simple add content to the database and view content of the database app..
https://drive.google...iew?usp=sharing



#2 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts

Posted 31 March 2015 - 06:25 PM

What we need is the error message. You'll find it in the logcat.

Anyway, in a quick overview I found you didn't try/catch your DB operations, and neither did you release the cursors.






Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download