samedi 9 janvier 2016

Saving appwiget id's in sqlite database android

I am trying to insert, save, delete and get the app widget id's(int values) from sqlite database.But I am unable to do any crud options mentioned above. Could any one please help and correct me on where Iam doing wrong. Here is my source code for your reference.

this is my mainactivity:

public class MainActivity extends Activity {
    private static final String TAG = "WidToGo";
    private AppWidgetHostView hostView;
    AppWidgetManager mAppWidgetManager;
    AppWidgetHost mAppWidgetHost;
    Button selcBtn,romBtn;;
    public  int REQ_CREATE_APPWIDGET = 900;
    public   int REQ_PICK_APPWIDGET=700;
    ViewGroup mainlayout;
   int id;
   private DataHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        selcBtn = (Button)findViewById(R.id.selcd);
        mainlayout = (LinearLayout) findViewById(R.id.main_layout);
        selcBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                selectWidget();
            }
        });

mAppWidgetManager = AppWidgetManager.getInstance(this);
mAppWidgetHost = new AppWidgetHost(this, R.id.APPWIDGET_HOST_ID);
dbHelper=new DataHelper(this);
dbHelper.getappWidgets(id);
//id++;
    }
    @Override
    public void onBackPressed() {

       finish();

    }
    void selectWidget() {
        int appWidgetId = mAppWidgetHost.allocateAppWidgetId();
        Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
        pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
        addEmptyData(pickIntent);
        startActivityForResult(pickIntent, REQ_PICK_APPWIDGET);
    }

    void addEmptyData(Intent pickIntent) {
        ArrayList<AppWidgetProviderInfo> customInfo = new ArrayList<AppWidgetProviderInfo>();
        pickIntent.putParcelableArrayListExtra(AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo);
        ArrayList<Bundle> customExtras = new ArrayList<Bundle>();
        pickIntent.putParcelableArrayListExtra(AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if (requestCode == REQ_PICK_APPWIDGET) {
                configureWidget(data);
            } else if (requestCode ==  REQ_CREATE_APPWIDGET) {
                createWidget(data);
            }
        } else if (resultCode == RESULT_CANCELED && data != null) {
            int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
            if (appWidgetId != -1) {
                mAppWidgetHost.deleteAppWidgetId(appWidgetId);
            }
        }
    }

    private void configureWidget(Intent data) {
        Bundle extras = data.getExtras();
        int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
        AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
        if (appWidgetInfo.configure != null) {
            Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
            intent.setComponent(appWidgetInfo.configure);
            intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
            startActivityForResult(intent, REQ_CREATE_APPWIDGET);
        } else {
            createWidget(data);
        }
    }


    public void createWidget(Intent data) {
    Bundle extras = data.getExtras();
    int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
    AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
    hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
    hostView.setAppWidget(appWidgetId, appWidgetInfo);
    mainlayout.addView(hostView);

    dbHelper.insertAppId(appWidgetId);
//appWidgetId++;
    }



    public void removeWidget(AppWidgetHostView hostView) {
        mAppWidgetHost.deleteAppWidgetId(hostView.getAppWidgetId());
        mainlayout.removeView(hostView);
        }
    /**
     * Registers the AppWidgetHost to listen for updates to any widgets this app
     * has.
     */

    @Override
    public void onStart() {
        super.onStart();
        mAppWidgetHost.startListening();
    }
    @Override
    public void onPause() {
        super.onPause();
        mAppWidgetHost.startListening();
    }
    @Override
    public void onResume() {
        super.onResume();
        mAppWidgetHost.startListening();
    }
    /**
     * Stop listen for updates for our widgets (saving battery).
     */
    @Override
    public void onStop() {
        super.onStop();
        mAppWidgetHost.stopListening();
    }
}

this is my DataHelper class:
public class DataHelper extends SQLiteOpenHelper {
    public static final String DATABASE_NAME = "imgdb";
    public static final String TABLE_NAME = "tbl_img";
    public static final int DATABASE_VERSION = 1;
    public static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS "+ TABLE_NAME+ "(id INTEGER PRIMARY KEY AUTOINCREMENT, img INTEGER NOT NULL, description TEXT NULL)"; 
    public static final String DELETE_TABLE="DROP TABLE IF EXISTS " + TABLE_NAME;
    Context context;
public DataHelper(Context context)  {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);

} 
public void onCreate(SQLiteDatabase db)  {
    // Create the table
    db.execSQL(CREATE_TABLE);

    }
//Upgrading database 
@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    //Drop older table if existed
    db.execSQL(DELETE_TABLE);
    //Create tables again
    onCreate(db);
    }

public void insertAppId(int appWidgetId)  {


    // Open the database for writing
    SQLiteDatabase db = this.getWritableDatabase();
    // Start the transaction.
    db.beginTransaction();
    ContentValues values;

    try
    {
        values = new ContentValues();
        values.put("img", appWidgetId);


        values.put("description", "Hi");

        // Insert Row
        long i = db.insert(TABLE_NAME, null, values);
        Log.i("Insert", i + "");
        // Insert into database successfully.
        db.setTransactionSuccessful();

    }
    catch (SQLiteException e)
    {
        e.printStackTrace();

    }
    finally
    {
        db.endTransaction();
        // End the transaction.
        db.close();
        // Close database
        } 
    }

    public int getappWidgets(int id){
        int appwid = 0;
        // Open the database for reading
        SQLiteDatabase db = this.getReadableDatabase();
        // Start the transaction.
        db.beginTransaction();

        try
        {
            String selectQuery = "SELECT * FROM "+ TABLE_NAME+" WHERE id = " + id;
            Cursor cursor = db.rawQuery(selectQuery, null);
                    if(cursor.getCount() >0)
            {
                    while (cursor.moveToNext()) {
                        appwid = cursor.getInt(cursor.getColumnIndex("img"));


                    }

            }
            db.setTransactionSuccessful();

        }
        catch (SQLiteException e)
        {
            e.printStackTrace();

        }
        finally
        {
            db.endTransaction();
            // End the transaction.
            db.close();
            // Close database
        }
        return appwid; 

} 
    public void deleteData(long memberID) {
        SQLiteDatabase db = this.getReadableDatabase();
        db.delete(TABLE_NAME, "  id = " + memberID, null);
    }
}


finally this my activity_main layout:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://ift.tt/nIICcg"
    android:orientation="vertical"
    android:background="#343a4e"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/main_layout"
        android:orientation="vertical"
        android:background="#343a4e"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:id="@+id/selcd"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:text="+"
            android:background="#343a4e"
            android:textAppearance="?android:textAppearanceMedium"
            android:textColor="#ffffff"
            android:textSize="30sp" />
    </LinearLayout>

</ScrollView>

Aucun commentaire:

Enregistrer un commentaire