Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Cant display database table in a listview

android sqlite listview

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

#1 DarkHeart

DarkHeart

    CC Newcomer

  • Member
  • PipPip
  • 16 posts

Posted 29 March 2015 - 01:19 AM

I am having a problem displaying all the column in a certain table. I am trying use a listview to display these but everytime i run the activity it crashes. i dont know if it has something to do with my Navigation drawer or something. But here is what i have tried....

 

 

 

Database statement

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;
    }

DisplayStation.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);



        displayListView();

        // titles
        // from
        // strings.xml

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

        set(navMenuTitles, navMenuIcons);

    }

        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);

    }
}







 

Stations.java

package com.azmal.example.com.facilitiesreviewapp;

public class Stations{


    String StationID = null;
    String StationName = null;
    String StationType = null;
    String Facilities = null;
    String Email = null;
    String Location = null;


    // getting StationID
    public String StationID() {
        return StationID;
    }

    // setting StationID
    public void setStationID(String StationID) {
        this. StationID = StationID;
    }

    // getting StationName
    public String getStationName() {
        return StationName;
    }

    // setting StationName
    public void setStationName(String StationName) {
        this. StationName = StationName;
    }

    //getting StationType
    public String getStationType() {
        return StationType;
    }

    // setting StationType
    public void setStationType(String StationType) {
        this. StationType = StationType;
    }

    //getting Facilities
    public String getFacilities() {
        return Facilities;
    }

    // setting Facilities
    public void setFacilities(String Facilities) {
        this. Facilities = Facilities;
    }

    //getting Location
    public String getLocation() {
        return Location;
    }

    // setting Location
    public void setLocation(String Location) {
        this. Location = Location;
    }

    public String getEmail() {
        return Email;
    }

    // setting Email
    public void setEmail(String Email) {
        this. Email = Email;
    }

}


 

display_Stations.xml

 

package com.azmal.example.com.facilitiesreviewapp;

public class Stations{


    String StationID = null;
    String StationName = null;
    String StationType = null;
    String Facilities = null;
    String Email = null;
    String Location = null;


    // getting StationID
    public String StationID() {
        return StationID;
    }

    // setting StationID
    public void setStationID(String StationID) {
        this. StationID = StationID;
    }

    // getting StationName
    public String getStationName() {
        return StationName;
    }

    // setting StationName
    public void setStationName(String StationName) {
        this. StationName = StationName;
    }

    //getting StationType
    public String getStationType() {
        return StationType;
    }

    // setting StationType
    public void setStationType(String StationType) {
        this. StationType = StationType;
    }

    //getting Facilities
    public String getFacilities() {
        return Facilities;
    }

    // setting Facilities
    public void setFacilities(String Facilities) {
        this. Facilities = Facilities;
    }

    //getting Location
    public String getLocation() {
        return Location;
    }

    // setting Location
    public void setLocation(String Location) {
        this. Location = Location;
    }

    public String getEmail() {
        return Email;
    }

    // setting Email
    public void setEmail(String Email) {
        this. Email = Email;
    }

}


 

Stations.xml

<?xml version="1.0" encoding="utf-8"?>
<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" >

    <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>
 


#2 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts

Posted 29 March 2015 - 10:04 AM

What does the error say?



#3 DarkHeart

DarkHeart

    CC Newcomer

  • Member
  • PipPip
  • 16 posts

Posted 29 March 2015 - 01:06 PM

What does the error say?

It doesnt say any errors but when i run it, it crashes



#4 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts

Posted 07 April 2015 - 05:22 PM

You have an error message and a stack trace. You won't see it in the emulator, though. 

 

It's in the android logcat, which should be a tab at the bottom of the IDE.

Which IDE are you using?






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