根据Android微调器刷新SQLite数据库表

时间:2014-08-29 03:39:42

标签: android database sqlite android-spinner

我已根据WIllJBD's suggestion

完成了我的代码

我希望表根据微调器点击数据时刷新数据,但我的toast给出了“数据库错误”。这是spinner上所选项目的代码

public void onItemSelected(AdapterView<?> parent, View view, int position,long id) {
        String tam = spDealer.getSelectedItem().toString();
        String dealerID = in.getDealID(tam);
        String queryrow = in.MatchDealerID(dealerID);

        if (queryrow == dealerID)
        {
            Cursor c = in.getViewInfoBilling(queryrow);
            int rows = c.getCount();
            int cols = c.getColumnCount();
            c.moveToFirst();

            for (int i = 0; i < rows; i++) {
                  TableRow row = new TableRow(this);
                  row.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));        
                  // inner for loop
                  for (int j = 0; j < cols; j++) 
                  {
                    TextView tv = new TextView(this);
                    tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT));
                    tv.setText(c.getString(j));
                    row.addView(tv);
                  }
                  c.moveToNext();
                  mytable.addView(row);
              }
        in.close();
        } else
        {
            Toast.makeText(Billing.this, "Data Error!!",Toast.LENGTH_LONG).show();
        }
    }

这是我的方法:

public Cursor getViewInfoBilling(String id){
    Cursor mcursor = db.rawQuery("SELECT BillDate,SODashboardNo, SODashboardAmount " +
    "FROM SODashboard, Bill WHERE SODashboard.SODashboardNo = Bill.TampSODashboardNo " +
    "AND SODashboard.DealerID = id", null);
    return mcursor;}

public String getDealID(String dealName) throws SQLException{
    Cursor mCursor = db.query(Dealer_TABLE, null, ""+Col_DealerName+"=?", new String[]{dealName}, null, null,null);
    if(mCursor.getCount()<1){
    mCursor.close();
    return "Not Exist";
    }
    mCursor.moveToFirst();
    String tampDealID = mCursor.getString(mCursor.getColumnIndex(Col_DealerID));
    mCursor.close();
    return tampDealID;}

public String MatchDealerID(String tam){
        String matchquery = "SELECT DealerID FROM SODashboard, Dealer " +
                "WHERE SODashboard.TampDealerIDSOD = tam";
        return matchquery;
    }

在微调器上选择的ID是否与DB上的ID相同?我已经创建了MatchDealerID和getDealID来比较它们。我应该怎么做才能让表格在点击后刷新?请帮我解决这个问题......谢谢

1 个答案:

答案 0 :(得分:1)

这种方法有问题。

public Cursor getViewInfoBilling(String id){
Cursor mcursor = db.rawQuery("SELECT BillDate,SODashboardNo, SODashboardAmount " +
"FROM SODashboard, Bill WHERE SODashboard.SODashboardNo = Bill.TampSODashboardNo " +
"AND SODashboard.DealerID =" + id, null);

//OR
/*
Cursor mcursor = db.rawQuery("SELECT BillDate,SODashboardNo, SODashboardAmount " +
"FROM SODashboard, Bill WHERE SODashboard.SODashboardNo = Bill.TampSODashboardNo " +
"AND SODashboard.DealerID =?", new String[] { id });    
*/
return mcursor;}

有两个版本,试试两个,祝你好运