0

I am creating widget for my application. i downloaded the LoremWidget project from github. link is here

here one array named items. when i hardcoded the array it will display values in widget correctly. but it does not show any thing when i try to fetch the data from database and copied the data to array by element by element. i given the Write and Read permission to manifest file.can any one find my errors pls. but i used this database code to another application (not widget) its retrieving correctly.

LoremViewsFactory.java

         public class LoremViewsFactory extends Activity implements    RemoteViewsService.RemoteViewsFactory  
         {

        public String[] items = new String[10];
        private Context ctxt = null;

        private int appWidgetId;

        public LoremViewsFactory(Context ctxt, Intent intent) {
            int i = 0;
            DatabaseHandler db = new DatabaseHandler(LoremViewsFactory);
            List < Contact > contacts = db.getAllContacts();
            for (Contact cn: contacts) {
                String log = cn.getURL();
                Toast.makeText(LoremViewsFactory.this, log, Toast.LENGTH_LONG).show();
                items[i] = log;
                i++;
            }
            this.ctxt = ctxt;
            appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                AppWidgetManager.INVALID_APPWIDGET_ID);
        }

This my getAllContactsfunction DatabseHandler class

        public List < Contact > getAllContacts() {
            List < Contact > contactList = new ArrayList < Contact > ();
            String selectQuery = "SELECT  * FROM " + TABLE_CONTACTS;
            SQLiteDatabase db = this.getWritableDatabase();
            Cursor cursor = db.rawQuery(selectQuery, null);
            if (cursor.moveToFirst()) {
                do {
                    Contact contact = new Contact();
                    contact.setURL(cursor.getString(0));
                    // Adding contact to list
                    contactList.add(contact);
                } while (cursor.moveToNext());
            }
            return contactList;
        }
    }
Cœur
  • 32,421
  • 21
  • 173
  • 232
  • NOT THE ANSWER, JUST TIPS: First, you don't need the database to be **writable**, if you only **read** from it. Second, you don't need `SELECT *`, if you only use **1** column. – Phantômaxx Feb 13 '15 at 11:42
  • ok thank u...any error in my code... i tried several times... – user4562919 Feb 13 '15 at 11:45
  • There's a much better way to convert a list to an array: http://stackoverflow.com/questions/9572795/convert-list-to-array-in-java – Phantômaxx Feb 13 '15 at 11:50
  • It seems it has a hard coded array called items... So you have to remove that and pass it as a parameter (possibly in the constructor). Or you might try to change this `private static final String[] item` to this `protected static String[] item` in LoremViewsFactory.java – Phantômaxx Feb 13 '15 at 11:54
  • if i use hardcoded array it show the values in widget. if i use the array and that array's values are from database then it shows nothing... – user4562919 Feb 13 '15 at 11:55
  • Read my latest (edited) comment – Phantômaxx Feb 13 '15 at 11:55
  • i will try and say to u – user4562919 Feb 13 '15 at 11:57
  • i tried to protectd it shows unfortunately stopped and in widget it showing nothing @DerGolem – user4562919 Feb 13 '15 at 12:03
  • Maybe you change the innings of the Factory to receive the array as an extra. And pass your array with `intent.putExtra()`. – Phantômaxx Feb 13 '15 at 12:30

0 Answers0