0

当我的小部件代码试图从 WidgetProvider 类调用数据库时,它没有检索任何内容。当我调试我的代码时,当它到达 database.open() 时,调试点突然消失了。是否有导致问题的超时设置?执行过程中也没有抛出异常。有人可以告诉我这里有什么问题吗?

代码详情如下:

public class PEMWidgetProvider extends AppWidgetProvider {

    private DataVO vo = new DataVO();

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {

        ComponentName thisWidget = new ComponentName(context,PEMWidgetProvider.class);

        int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);

        for (int widgetId : allWidgetIds) {

            RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);


            DataDAO dao = new  DataDAO (context);




                String month = "4";
                vo = dao.getData(month);



            }

          //get all detail
            remoteViews.setTextViewText(R.id.v_widgetFieldName, vo.getFiedl1());
            remoteViews.setTextViewText(R.id.v_widgetFieldValue, vo.getFiedle2());

         // Register an onClickListener
            Intent intent = new Intent(context, PEMWidgetProvider.class);

            intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
            intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);

            PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
                0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
            remoteViews.setOnClickPendingIntent(R.id.v_widgetFieldValue, pendingIntent);
            appWidgetManager.updateAppWidget(widgetId, remoteViews);
        }
    }

数据道:

public class DataDAO{

    private static final String DB_NAME = "DB";
    private SQLiteDatabase db;
    private BaseDAO baseDAO;
    private Context context;

    public DataDAO(Context context){
        baseDAO = new BaseDAO(context, DB_NAME, null, 1);
        this.context = context;
    }



 //this method works fine when i am calling from MainActivity
   public DataVO  getData(String month){


        SQLiteDatabase database = baseDAO.open();

        String whereClasue = "fieldName= ? and month = ?";
        Cursor cursor =database.query(Constants.DATA_TBL, 
        new String[]{"field1, fieldValue2"}, 
        whereClasue, new String[]{"Data1", month}, null, null, null);

        //there will be only record
        if(cursor.moveToFirst()){
            do{
                DataVO vo = new DataVO();
                vo.setField1(cursor.getString(0));              
                vo.setField2(cursor.getString(1));
            }while(cursor.moveToNext());

        }       

        if (cursor != null && !cursor.isClosed()) {
            cursor.close();
            database.close();
        }
        return vo;
    }




public class BaseDAO extends SQLiteOpenHelper {

    public static SQLiteDatabase database;
    public BaseDAO(Context context, String name, CursorFactory factory,
            int version) {
        super(context, name, factory, version);
    }

    public SQLiteDatabase open() throws SQLException{
            database = super.getWritableDatabase();

            return database;
        }


        public void close(){
            if(database != null)
                database.close();
        }

    }

谢谢钦坦

4

1 回答 1

0

请更正您的代码,它将起作用。

database = super.getWritableDatabase();// replace this line of code with following line   
database = getWritableDatabase();
于 2013-04-02T04:40:08.237 回答