dimanche 29 mars 2015

Problems displaying database columns in Listview

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.




  1. Stations.xml




  2. Display.java




  3. Database Statement




  4. dislay_stations.xml







<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
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>



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;
ListView StationList;
Cursor cursor;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display_stations);
StationList = (ListView) findViewById(R.id.listView1);
dbHelper = new SQL(this);


// titles
// from
// strings.xml

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

set(navMenuTitles, navMenuIcons);

String[] from = {dbHelper.COL_StationID, dbHelper.COL_StationName, dbHelper.COL_StationType, dbHelper.COL_Facilities, dbHelper.COL_Location, dbHelper.COL_Email};
int[] to = {R.id.ID, R.id.SName, R.id.Stype, R.id.SFacilities, R.id.SLocation, R.id.SEmail};
cursor = dbHelper.fetchAllStations();
SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this,
R.layout.stations, cursor, from, to);
StationList.setAdapter(cursorAdapter);

}
}



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






<android.support.v4.widget.DrawerLayout
xmlns:android="http://ift.tt/nIICcg"
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://ift.tt/nIICcg"
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>



Aucun commentaire:

Enregistrer un commentaire