Android列表无法从sqlite数据库加载

时间:2012-09-28 21:17:30

标签: android database sqlite csv

我一直致力于使用数据库来填充列表的Android应用。数据库中填充了位于/ assets文件夹中的.csv文件中的记录。一切都在几分钟前工作,但自从我刷新了我的CSV以来,数据列表不再显示了(它只是说“没有命令显示”,这意味着它没有找到任何数据到使用)填充列表。此外,代码不会产生任何错误,LogCat对我来说并不是很有用。

我想知道是否有人可以帮助我:谢谢你!

这是DatabaseActivity.java文件:

public class DatabaseActivity extends ListActivity {

String ID;
String title;
String location;
String startTime;
String endTime;
String date;
String description;
private DatabaseHelper dbh;

private ProgressDialog m_ProgressDialog = null; 
private ArrayList<Order> m_orders = null;
private OrderAdapter m_adapter;
private Runnable viewOrders;

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

    this.dbh = new DatabaseHelper(this);

    try {
        csvtodb();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    m_orders = new ArrayList<Order>();
    this.m_adapter = new OrderAdapter(this, R.layout.row, m_orders);
    setListAdapter(this.m_adapter);

    viewOrders = new Runnable(){
        @Override
        public void run() {
            getOrders();
        }
    };
    Thread thread =  new Thread(null, viewOrders, "MagentoBackground");
    thread.start();
    m_ProgressDialog = ProgressDialog.show(DatabaseActivity.this,    
            "Even geduld...", "Database wordt geladen...", true);

}

private Runnable returnRes = new Runnable() {

    @Override
    public void run() {
        if(m_orders != null && m_orders.size() > 0){
            m_adapter.notifyDataSetChanged();
            for(int i=0;i<m_orders.size();i++)
                m_adapter.add(m_orders.get(i));
        }
        m_ProgressDialog.dismiss();
        m_adapter.notifyDataSetChanged();
    }
};

private void getOrders(){
    try{
        m_orders = new ArrayList<Order>();
        SQLiteDatabase db = dbh.getReadableDatabase();
        Cursor c = db.query(DatabaseHelper.tblEvents, 
                new String[] {DatabaseHelper.title, 
                DatabaseHelper.location, 
                DatabaseHelper.startTime,
                DatabaseHelper.description}, 
                null, null, null, null, 
                DatabaseHelper.startTime + " ASC");
        int numberOfRows = c.getCount();
        for (int i = 0; i < numberOfRows; i++){
            c.moveToPosition(i);
            // Obtain the title
            int col1 = c.getColumnIndex(DatabaseHelper.title);
            String title = c.getString(col1);
            // Obtain the location
            int col2 = c.getColumnIndex(DatabaseHelper.location);
            String location = c.getString(col2);
            // Obtain the time
            int col3 = c.getColumnIndex(DatabaseHelper.startTime);
            String startTime = c.getString(col3);
            // Obtain the description
            int col4 = c.getColumnIndex(DatabaseHelper.description);
            String description = c.getString(col4);
            Order o = new Order();
            o.setOrderTime(startTime);
            o.setOrderTitle(title);
            o.setOrderLocation(location);
            o.setOrderDescription(description);
            m_orders.add(o);
        }
        db.close();
        Thread.sleep(2000);
        Log.i("ARRAY", ""+ m_orders.size());
    } catch (Exception e) { 
        Log.e("BACKGROUND_PROC", e.getMessage());
    }
    runOnUiThread(returnRes);
}



public void csvtodb() throws IOException {
    // Get a writable database
    SQLiteDatabase db = dbh.getWritableDatabase();
    if (dbh.isDbInitialized(db)) {
        db.close();
        return;
    }

    AssetManager assetManager = getAssets();
    InputStream input = assetManager.open("testcsv.csv");

    BufferedReader in = new BufferedReader(new InputStreamReader(input, "UTF-8"));
    String reader = "";
    while ((reader = in.readLine()) != null)
    {
        String[] RowData = reader.split(",");

        ID = RowData[0];
        title = RowData[1];
        location = RowData[2];
        startTime = RowData[3];
        endTime = RowData[4];
        date = RowData[5];
        description = RowData[6];

        DatabaseHelper.fillTable(db, ID, title, location, startTime, endTime, date, description);
    }

    in.close();
}

public class OrderAdapter extends ArrayAdapter<Order> {

    private ArrayList<Order> items;

    public OrderAdapter(Context context, int textViewResourceId, ArrayList<Order> items) {
        super(context, textViewResourceId, items);
        this.items = items;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.row, null);
        }
        Order o = items.get(position);
        if (o != null) {
            TextView time = (TextView) v.findViewById(R.id.time);
            TextView title = (TextView) v.findViewById(R.id.title);
            TextView location = (TextView) v.findViewById(R.id.location);
            TextView description = (TextView) v.findViewById(R.id.description);
            if (time != null) {
                time.setText(o.getOrderTime());                            
            }
            if(title != null){
                title.setText(o.getOrderTitle());
            }
            if(location != null){
                location.setText("Locatie: " + o.getOrderLocation());
            }
            if(description != null){
                description.setText(o.getOrderDescription());
            }
        }
        return v;
    }
}
}

这是DatabaseHelper.java:

public class DatabaseHelper extends SQLiteOpenHelper{
// Database name and version
static final String dbName = "Evenementen";
static final int dbVersion = 1;

// Names of tables and columns
static final String tblEvents = "Evenementen";
static final String eventID = "ID";
static final String title = "Titel";
static final String location = "Locatie";
static final String startTime = "Begintijd";
static final String endTime = "Eindtijd";
static final String date = "Datum";
static final String description = "Beschrijving";


// Constructor
public DatabaseHelper(Context context) {
    super(context, dbName, null, dbVersion);
}

@Override
public void onCreate(SQLiteDatabase db) {
    createDB(db);
}

public void createDB(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE " + tblEvents + " (" +
            eventID   + " INTEGER PRIMARY KEY, " +
            title   + " TEXT , " +
            location   + " TEXT , " +
            startTime   + " TEXT , " +
            endTime   + " TEXT , " +
            date   + " TEXT , " +
            description + " TEXT" +
            ")");
}

public boolean isDbInitialized(SQLiteDatabase db) {
    return db.rawQuery("SELECT DISTINCT tbl_name FROM sqlite_master WHERE tbl_name = '"+ tblEvents + "'", null) != null;
}

public static void fillTable(SQLiteDatabase db, String first, String second, String third, 
        String fourth, String fifth, String sixth, String seventh) {
    String template = "INSERT INTO " + tblEvents + 
            "(" + eventID + "," + title + "," + location + "," + startTime + "," + endTime + "," + date + "," + description + ")" + 
            "VALUES (?,?,?,?,?,?,?)";
    Object[] args = new Object[]{first, second, third, fourth, fifth, sixth, seventh};
    db.execSQL(template, args);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Bluntly remove table (and thus discard ALL data in it!) if we're upgrading  
    db.execSQL("DROP TABLE IF EXISTS " + tblEvents);
    onCreate(db);
}

}

这是我的CSV文件(它是荷兰语。这些值用逗号分隔):

1,Cross Rubicon,van der Werff & Studio de Veste,15:00,Nvt,03-okt,Geen beschrijving beschikbaar
2,Ferry Nick & Lodewijk Band,van der Werff & Studio de Veste,18:30,Nvt,03-okt,Bekend van de Edwin Evers Band & Marco Borsato
3,LOS,van der Werff & Studio de Veste,21:00,Nvt,03-okt,Nederpopcovers
4,La Cubaniza,Scheltema,16:00,Nvt,03-okt,Cuban liveband & DJ

仅供参考:我正在使用this reference在列表中显示数据库中的数据。

1 个答案:

答案 0 :(得分:0)

您的活动看起来像标题

// Obtain the time
            int col1 = c.getColumnIndex(DatabaseHelper.title);

但包含的CSV列表没有标题......