android spinner display blank items no error displayed

  • Replies:4
  • OpenNot stickiedUnanswered
  • Forum posts: 3

Apr 1, 2016 5:26:25 AM via Website

I have tried to use Spinner to display data from SQLite. There was no errors but the spinner displayed nothing. Can anyone solve this problem before. Below is my code
CategoryBO.java

package com.giao.ordersystem;

/**
 * Created by Long on 2/13/2016.
 */

    public class CategoryBO {
        private String categoryName;
        public CategoryBO()
        {

        }
        public CategoryBO(String categoryName)
        {
            this.categoryName=categoryName;
        }
        public String getCategoryName()
        {
            return this.categoryName;
        }
        public void setCategoryName(String categoryName)
        {
            this.categoryName=categoryName;
        }
    }

DishDAO.java

package com.giao.ordersystem;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import java.util.List;
import java.util.ArrayList;
/**
 * Created by Long on 2/13/2016.
 */
public class DishDAO {
    private static final String KEY_ROWID = "dishID";
    private static final String CATEGORYID = "categoryID";
    private static final String DISH_NAME = "dishName";
    private static final String DISH_PRICE = "dishPrice";
    private static final String DISH_DESCRIPTION = "dishDescription";
    private static final String DISH_AVAILABILITY="availability";
    private static final String DATABASE_TABLE = "Menu";
    private static DatabaseHelper databaseHelper;
    //
    public final Context context;
    private SQLiteDatabase database;

    public DishDAO(Context context) {
        this.context = context;
        // TODO Auto-generated constructor stub
    }

    public DishDAO open() throws SQLException {
        database = databaseHelper.getReadableDatabase();
        return this;
    }

    public void close() throws SQLException {
        this.close();
    }

    public long create(String categoryID, String dishName, String dishPrice, String dishDecription, String availability) throws SQLException {
        ContentValues cv = new ContentValues();
        cv.put(CATEGORYID, categoryID);
        cv.put(DISH_NAME, dishName);
        cv.put(DISH_PRICE, dishPrice);
        cv.put(DISH_DESCRIPTION, dishDecription);
        cv.put(DISH_AVAILABILITY,availability);
        return database.insert(DATABASE_TABLE, null, cv);
    }

    public List<DishBO> list() throws SQLException {
        String query = "SELECT * FROM Tables";
        Cursor cur = database.rawQuery(query, null);
        List<DishBO> list = new ArrayList<DishBO>();
        int iRow = cur.getColumnIndex(KEY_ROWID);
        for (cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()) {
            DishBO record = new DishBO(Integer.parseInt(cur.getString(0)), Integer.parseInt(cur.getString(1)), cur.getString(2)
                    , Integer.parseInt(cur.getString(3)), cur.getString(4),Integer.parseInt(cur.getString(5)));
            list.add(record);
        }
        cur.close();
        return list;
    }

    public boolean remove(int dishID) throws SQLException {
        return database.delete(DATABASE_TABLE, KEY_ROWID + "=" + dishID, null) > 0;
    }

    public boolean removeAll() {
        return database.delete(DATABASE_TABLE, null, null) > 0;
    }

    public long update(String dishID, String categoryID, String dishName, String dishPrice, String dishDecription) throws SQLException
    {
        ContentValues cv = new ContentValues();
        if (categoryID != null)
            cv.put(CATEGORYID, categoryID);
        if (dishName != null)
            cv.put(DISH_NAME, dishName);
        if (dishPrice != null)
            cv.put(DISH_PRICE, dishPrice);
        if (dishDecription != null)
            cv.put(DISH_DESCRIPTION, dishDecription);
        return database.update(DATABASE_TABLE, cv, KEY_ROWID + "=?", new String[]{dishID});
    }
}

Menu_Event.Java

package com.giao.ordersystem;

import android.content.Context;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Long on 2/8/2016.
 */
public class Menu_Event {
    private Context context;
    private CategoryDAO categoryDAO;
    public Menu_Event()
    {}
    public Menu_Event(Context context)
    {
        this.context=context;
        categoryDAO = new CategoryDAO(context);
    }
    public void categoryListView_OnLoad(Spinner categorySpinner)
    {
        categoryDAO.open();
        List<String> categoryList = new ArrayList<String>();
        for(int i=0;i<categoryDAO.list().size();i++)
        {
            categoryList.add(categoryDAO.list().get(i).getCategoryName());
        }
        categoryDAO.close();
        ArrayAdapter<String> categoryAdapter =  new ArrayAdapter(context,android.R.layout.simple_spinner_item,categoryList);
        categoryAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        categorySpinner.setAdapter(categoryAdapter);
        categoryAdapter.notifyDataSetChanged();
    }
}

style.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

</resources>

— modified on Apr 1, 2016 5:31:11 AM

  • Forum posts: 6

Apr 3, 2016 9:56:23 PM via Website

Hey, where do you call the method categoryListView_OnLoad(Spinner categorySpinner)?
I mean, this is the method where you create your Adapter, but i cant find the code where you call it.

Have you debugged the method categoryListView_OnLoad(Spinner categorySpinner), to see if the list even gets filled with data?

And post the Code of your CategoryDAO Class pls

I havent been into android programming for long but i had a similar issue once

  • Forum posts: 3

Apr 19, 2016 7:45:33 AM via Website

Sorry guys, I'm just back from the vacation. Below is my categoryDAO

package com.giao.ordersystem; import android.content.Context; import
android.database.sqlite.SQLiteDatabase; import
android.content.ContentValues; import android.database.Cursor; import
android.database.SQLException; import java.util.List; import
java.util.ArrayList; /** * Created by Long on 2/12/2016. */ public
class CategoryDAO{
public static final String KEY_TableName="CategoryName";
private static final String DATABASE_TABLE="Category";
private static DatabaseHelper databaseHelper;
//
public final Context context;
private SQLiteDatabase database;
public CategoryDAO(Context context) {
this.context=context;
databaseHelper= new DatabaseHelper(context);
// TODO Auto-generated constructor stub
}
public CategoryDAO open() throws SQLException
{
database=databaseHelper.getReadableDatabase();
return this;
}
public void close() throws SQLException
{
databaseHelper.close();
}
public long create(String tableName) throws SQLException
{
ContentValues cv= new ContentValues();
cv.put(KEY_TableName, tableName);
return database.insert(DATABASE_TABLE,null ,cv);
}
public ArrayList list()
{
String query="SELECT * FROM Category";
Cursor cur=database.rawQuery(query,null);
ArrayList list = new ArrayList();
int iRow= cur.getColumnIndex(KEY_TableName);
for(cur.moveToFirst();!cur.isAfterLast();cur.moveToNext()) {
CategoryBO record = new CategoryBO(cur.getString(0));
list.add(record); }
cur.close();
return list;
}
public boolean remove(int categoryName)
{
return database.delete(DATABASE_TABLE, KEY_TableName + "=" + categoryName, null) > 0;
}
public boolean removeAll()
{
return database.delete(DATABASE_TABLE, null, null) > 0;
}

}

  • Forum posts: 3

Apr 19, 2016 7:49:44 AM via Website

I have debug the categoryListView_Onload(Spinner categorySpinner), the list filled with data for sure, but it displayed blank.