0

I need to fix this code Objects item = getItem(position); can you please help tell me why I am having getting null here its showing the result null

System.out﹕ item.getUrl()
System.out﹕ null

please note that the database sqlite is returning data.

www.i I/System.out﹕ www.i.Objects@c880a09, com.justedhak.www.i.Objects@14f1340e, www.i.Objects@10f6dc3c, com.justedhak.www.i.Objects@29e3dc5, www.i.Objects@1f90831a, com.justedhak.www.i.Objects@3895ee4b, ]

I am trying to display the values from sqlite to the grid view adapter

List<Objects> Objects = db.getAllObjects();
Log.d("he","tttttt");
System.out.println(Objects);
DBadapter adapter = new DBadapter(getApplicationContext(), R.layout.grid_item_layout, Objects);

adapter.notifyDataSetChanged();
mGridView.setAdapter(adapter);

I add the system.out.print and the result is null, its seem position is being null

Objects item = getItem(position);
System.out.println("item.getUrl() ");
System.out.println(item.getUrl());
Picasso.with(mcontext).setIndicatorsEnabled(true);
//holder.imageTitle.setText(item.getId());
holder.imageTitle.setText(String.valueOf(item.getId()));
Picasso.
        with(mcontext).
        load(item.getUrl())
        .placeholder(R.drawable.logo)
        .fit()
        .noFade()
        .into(holder.imageView);

this is full code adapter

public class DBadapter extends ArrayAdapter<Objects> {
    private static Uri[] mUrls = null;
    private static String[] strUrls = null;
    private String[] mNames = null;
    private Cursor cc = null;
    private Context mcontext;
    private int layoutResourceId;
    private List<?> listitems;

    public DBadapter(Context context, int layoutResourceId, List<Objects> listitem) {
        super(context, layoutResourceId, listitem);
        this.layoutResourceId = layoutResourceId;
        this.mcontext = context;
        this.listitems = listitem;
        System.out.println("entering adapter");
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        System.out.println("entering adapter1");

        View row = convertView;
        final ViewHolder holder;

        if (row == null) {
            LayoutInflater inflater = LayoutInflater.from(mcontext);
            row = inflater.inflate(layoutResourceId, parent, false);
            holder = new ViewHolder();
            holder.imageTitle = (TextView) row.findViewById(R.id.Nameview);
            holder.imageView = (ImageView) row.findViewById(R.id.imageView);
            row.setTag(holder);
        } else {
            holder = (ViewHolder) row.getTag();
        }
        Objects item = getItem(position);
        System.out.println("item.getUrl() ");
        System.out.println(item.getUrl());
        Picasso.with(mcontext).setIndicatorsEnabled(true);
        //holder.imageTitle.setText(item.getId());
        holder.imageTitle.setText(String.valueOf(item.getId()));
        Picasso.
                with(mcontext).
                load(item.getUrl())
                .placeholder(R.drawable.logo)
                .fit()
                .noFade()
                .into(holder.imageView);

        holder.imageView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                Log.d("OnImageButton", "Clicked");
                Intent intnt = new Intent(mcontext, SingleViewActivity.class);
                intnt.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                //Bitmap imageID=holder.imageView;
                //intnt.putExtra("ImageId", imageID);
                mcontext.startActivity(intnt);


                Toast.makeText(mcontext, "intent",
                        Toast.LENGTH_LONG).show();
            }
        });


        return row;
    }

    static class ViewHolder {
        TextView imageTitle;
        ImageView imageView;
    }
}

edit

   ContentValues values = new ContentValues();

        values.put(OBJECT_ID,"10" ); // OBJECT Name
        values.put(OBJECT_NAME, "H"); // OBJECT Name
        values.put(OBJECT_URL, "http://api.androidhive.info/images/sample.jpg"); // OBJECT URL
        values.put(OBJECT_TYPE, "image"); // Contact type
        values.put(OBJECT_CATEGORY, "funny"); // Contact category

        // Inserting Row
        db.insert(TABLE_OBJECTS, null, values);
        db.close(); // Closing database connection
    }
  //  Objects object=new Objects(String );

    // Getting single contact
    Objects geturl(String name) {
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.query(TABLE_OBJECTS, new String[] {OBJECT_URL,
                        OBJECT_NAME , OBJECT_CATEGORY, OBJECT_TYPE }, OBJECT_NAME + "=?",
                new String[] { String.valueOf(name) }, null, null, null, null);

getallobjects

// Getting All Contacts
public List<Objects> getAllObjects() {
    List<Objects> Objectslist = new ArrayList<Objects>();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_OBJECTS;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            Objects object = new Objects();
            object.setId(Integer.parseInt(cursor.getString(0)));
            object.setName(cursor.getString(1));
            object.setUrl(cursor.getString(2));
            // Adding contact to list
            Objectslist.add(object);
        } while (cursor.moveToNext());
    }

    // return contact list
    return Objectslist;
}
Dhaval Patel
  • 9,209
  • 4
  • 42
  • 44
Moudiz
  • 6,775
  • 18
  • 67
  • 139
  • Override toString method in Objects class and print Objects.toString() to check it's content. I have doubt that getUrl is returning null. – Dhaval Patel Nov 18 '15 at 18:40
  • Or may be just put log in getAllObjects method to check url is null or not while getting data from database? – Dhaval Patel Nov 18 '15 at 18:45
  • @DhavalPatel I did add log in getallobjects ...`List Objects = db.getAllObjects(); Log.d("he","tttttt"); System.out.println(Objects);` and the output as I mentioned in my questions is www.i I/System.out﹕ www.i.Objects@c880a09, com.justedhak.www.i.Objects@14f1340e, www.i.Objects@10f6dc3c, com.justedhak.www.i.Objects@29e3dc5, www.i.Objects@1f90831a, com.justedhak.www.i.Objects@3895ee4b, ] – Moudiz Nov 18 '15 at 18:50
  • It's just print object hash-code, Override toString method to see the content of Objects class. – Dhaval Patel Nov 18 '15 at 18:51
  • if you are using android studio then press Alt+Insert and select toString in Objects class, IDE will generate toString method for you. – Dhaval Patel Nov 18 '15 at 18:53
  • @DhavalPatel when I clicked alt+insert I had something like this @Override public String toString() { return "MainActivity{" + "txtBytes=" + txtBytes + ", list=" + list + ", imageView=" + imageView + ", toolbar=" + toolbar + ", tabLayout=" + tabLayout + ", viewPager=" + viewPager + ", mGridView=" + mGridView + '}'; } however I am still havingthe same ouput `www.i.Objects@c880a09,` – Moudiz Nov 18 '15 at 19:09
  • No, do this for Objects class. – Dhaval Patel Nov 18 '15 at 19:11
  • @DhavalPatel oh thanks i like this tostring now I can see the values `.www.i I/System.out﹕ [Objects{id=10, name='image', url='null', type='null', category='null'}, Objects{id=10, name='image', url='null', type='null', category='null'}, Objects{id=10, name='image', url='null',` so url is null I edit my question and I added how I am inserting , should I fix the order of the strings ? – Moudiz Nov 18 '15 at 19:20
  • post your code where you form List? – Dhaval Patel Nov 18 '15 at 19:23
  • @DhavalPatel I added in my edit answer, do yuo something else? please tell me what exactly is it – Moudiz Nov 18 '15 at 19:26
  • post code of getAllObjects() method. – Dhaval Patel Nov 18 '15 at 19:28
  • @DhavalPatel check my edit please .. if you need anything please tell me – Moudiz Nov 18 '15 at 19:35
  • By looking at your code, it's look like ColumnIndex is misplaced. – Dhaval Patel Nov 18 '15 at 19:39
  • @dhaval patel please tell me how to sort it – Moudiz Nov 18 '15 at 19:41

1 Answers1

2

Instead of

 object.setId(Integer.parseInt(cursor.getString(0)));
 object.setName(cursor.getString(1));
 object.setUrl(cursor.getString(2));

Use

object.setId(cursor.getInt(cursor.getColumnIndex(OBJECT_ID)));
object.setName(cursor.getString(cursor.getColumnIndex(OBJECT_NAME)));
object.setUrl(cursor.getString(cursor.getColumnIndex(OBJECT_URL)));
Dhaval Patel
  • 9,209
  • 4
  • 42
  • 44
  • `c` is refering to what ? – Moudiz Nov 18 '15 at 19:46
  • its ok.. however still getting null `[Objects{id=10, name='H', url='null', type='null', category='null'},` – Moudiz Nov 18 '15 at 19:53
  • why you think I am getting null to url however I am setting value to it ? `values.put(OBJECT_URL, "http://api.androidhive.info/images/sample.jpg"); // OBJECT URL` – Moudiz Nov 18 '15 at 19:55
  • Check the value of **db.insert(TABLE_OBJECTS, null, values);**, while inserting data. it will return the row ID of the newly inserted row, or -1 if an error occurred. – Dhaval Patel Nov 18 '15 at 20:01
  • Have you tried to insert same data without url before. – Dhaval Patel Nov 18 '15 at 20:07
  • values.put(OBJECT_ID,"10" ); // OBJECT Name values.put(OBJECT_NAME, "H"); // OBJECT Name values.put(OBJECT_URL, "http://api.androidhive.info/images/sample.jpg"); // OBJECT URL values.put(OBJECT_TYPE, "image"); // Contact type System.out.println(values); ... the output of logcatwas like this TYPE=image NAME=H ID=10 URL=http://api.androidhive.info/images/sample.jpg – Moudiz Nov 18 '15 at 20:15
  • try long id = db.insert(TABLE_OBJECTS, null, values); System.out.println( "id:"+id); – Dhaval Patel Nov 18 '15 at 20:19
  • I had something like this TYPE=image NAME=H ID=10 URL=http://api.androidhive.info/images/sample.jpg 11-18 22:22:11.549 12801-12801/comak.www.i I/System.out﹕ id:39 .. id=41... id=43.. – Moudiz Nov 18 '15 at 20:23
  • ill post this issue in new question – Moudiz Nov 18 '15 at 20:24
  • Is your http://stackoverflow.com/questions/33717120/sqlite-get-all-data-and-pass-it-to-an-adapter problem resolved? – Dhaval Patel Nov 18 '15 at 20:25
  • Yes it did bnk solve it but i am making sure that its returning data – Moudiz Nov 18 '15 at 20:30
  • I add a new question related to this http://stackoverflow.com/questions/33789991/sqlite-cursor-returning-wrong-value-for-a-column and pleave can you upvote my questions ? thanks – Moudiz Nov 18 '15 at 20:44