dimanche 26 juillet 2015

Android: Cannot display information from my SQLite Database in a ListView Activity

Although this question has been asked many times on here, I just can't find the proper answer to fit my code. I'm trying to implement a <ListView> in my app in order to display the data from the database on my activity but whenever I test it on the emulator, nothing is being displayed. My code has no errors whatsoever and I've been trying to find the problem for hours with no success. I realize it may be something small but I can't identify the problem.

Any help would be greatly appreciated.

My database activity:

public class LysandrosDatabaseAdapter {

LysandrosHelper helper;

public LysandrosDatabaseAdapter (Context context) {

    helper = new LysandrosHelper(context);
}

public long insertData (String name,
                        String surname,
                        String tnumber,
                        String bnumber,
                        String email,
                        String address,
                        String dob,
                        String department,

                        String workplace,
                        String stardate,
                        String notes)
{
    SQLiteDatabase db = helper.getWritableDatabase();

    ContentValues contentValues = new ContentValues();
    contentValues.put(LysandrosHelper.NAME, name);
    contentValues.put(LysandrosHelper.SURNAME, surname);
    contentValues.put(LysandrosHelper.NUMBER, tnumber);
    contentValues.put(LysandrosHelper.BNUMBER, bnumber);
    contentValues.put(LysandrosHelper.EMAIL, email);
    contentValues.put(LysandrosHelper.ADDRESS, address);
    contentValues.put(LysandrosHelper.DOB, dob);
    contentValues.put(LysandrosHelper.DEPARTMENT, department);
    contentValues.put(LysandrosHelper.WORKPLACE, workplace);
    contentValues.put(LysandrosHelper.STARTDATE, stardate);
    contentValues.put(LysandrosHelper.NOTES, notes);

    long id = db.insert(LysandrosHelper.TABLE_NAME, null, contentValues);
    db.close();
    return id;
}

static class LysandrosHelper extends SQLiteOpenHelper {
    static final String DATABASE_NAME = "LysandrosDatabase";
    static final String TABLE_NAME = "LysandrosTable";
    static final int DATABASE_VERSION = 4;

    static final String UID = "_id";
    static final String NAME = "Name";
    static final String SURNAME = "Surname";
    static final String NUMBER = "TelephoneNumber";
    static final String BNUMBER = "BusinessNumber";
    static final String EMAIL = "Email";
    static final String ADDRESS = "Address";
    static final String DOB = "DateofBirth";
    static final String DEPARTMENT = "Department";
    static final String WORKPLACE = "Workplace";
    static final String STARTDATE = "StartDate";
    static final String NOTES = "Notes";

    private static final String CREATE_TABLE = "CREATE TABLE "+TABLE_NAME+"("+UID+" INTEGER PRIMARY KEY AUTOINCREMENT, " +
            ""+NAME+" VARCHAR(255), " +
            ""+SURNAME+" VARCHAR(255), "+
            ""+NUMBER+" INTEGER," +
            ""+BNUMBER+" INTEGER," +
            ""+EMAIL+" VARCHAR(255)," +
            ""+ADDRESS+" VARCHAR(255)," +
            ""+DOB+" DATE," +
            ""+DEPARTMENT+" VARCHAR(255)," +
            ""+WORKPLACE+" VARCHAR(255)," +
            ""+STARTDATE+" DATE," +
            ""+NOTES+" TEXT);";

    private static final String DROP_TABLE = "DROP TABLE IF EXISTS " +TABLE_NAME;

    private Context context;


    public LysandrosHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        this.context = context;
        Message.message(context, "Constructor Called");
    }


    public void onCreate (SQLiteDatabase db) {

        try {
            db.execSQL(CREATE_TABLE);
            Message.message(context, "onCreate Called");
        } catch (SQLException e) {
            Message.message(context, ""+e);
        }
    }

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

        try {
            Message.message(context, "onUpgrade Called");
            db.execSQL(DROP_TABLE);
            onCreate(db);
        } catch (SQLException e) {
            Message.message(context, ""+e);
        }

    }

}

My EmployeeList activity where I want the information to be displayed:

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.EditText;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

/**
 * Created by User on 7/25/2015.
 */
public class EmployeeList extends Activity {

EditText searchBar;
SQLiteDatabase db;
Cursor cursor;
ListAdapter adapter;
ListView employeeList;

/** Called when the activity is first created */
@Override
public void onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.employeelist);

    db = (new LysandrosDatabaseAdapter.LysandrosHelper(this)).getWritableDatabase();
    searchBar = (EditText) findViewById (R.id.searchBar);
    employeeList = (ListView) findViewById(R.id.list);

}

public void Search(View view) {
    // || is the concatenation operation in SQLite
    cursor = db.rawQuery("SELECT _id, Name, Surname, Department FROM LysandrosTable WHERE Name || ' ' || Surname LIKE ?",
            new String[]{"%" + searchBar.getText().toString() + "%"});

    adapter = new SimpleCursorAdapter(
            this,
            R.layout.employee_list_item,
            cursor,
            new String[] {"Name", "Surname", "Department"},
            new int[] {R.id.EmployeeListName, R.id.EmployeeListSurname, R.id.EmployeeListDepartment});
    employeeList.setAdapter(adapter);
    }
}

My employee_list XML file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://ift.tt/nIICcg"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">


    <EditText
        android:id="@+id/searchBar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="@string/searchhere"
        android:layout_weight="1"/>

    <Button
       android:id="@+id/searchButton"
       android:text="@string/search"
        android:onClick="Search"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />

</LinearLayout>

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

</LinearLayout>

My employee_list_item XML file responsible for displaying the information in the activity:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://ift.tt/nIICcg"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="8dp">

<TextView
    android:id="@+id/EmployeeListName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/EmployeeListSurname"
    android:layout_marginStart="6dp"
    android:layout_toEndOf="@+id/EmployeeListName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/EmployeeListDepartment"
    android:layout_below="@+id/EmployeeListName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />



</RelativeLayout>

Aucun commentaire:

Enregistrer un commentaire