4

I have this code which gets some data from a database

  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());
        }

In my MainActivity, I am getting it like this

  DatabaseHandler db = new DatabaseHandler(getApplicationContext());

                List<Objects> Objects = db.getAllObjects();
                DBadapter adapter = new DBadapter(getApplicationContext(), R.layout.grid_item_layout, Objects);

I am getting an error in my DBadatper, here

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

the error is: Cannot Resolve method..
I guess because of the List object, but how to fix that?

adapter class

public class DBadapter extends ArrayAdapter<Listitem> {
    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.text);
            holder.imageView = (ImageView) row.findViewById(R.id.imageView);
            row.setTag(holder);
        } else {
            holder = (ViewHolder) row.getTag();
        }
        Listitem item = getItem(position);
        System.out.println("item.getUrl() ");
        System.out.println(item.getUrl());
        Picasso.with(mcontext).setIndicatorsEnabled(true);
        holder.imageTitle.setText(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)  ; //This line raises error


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


        return row;
    }

    static class ViewHolder {
        TextView imageTitle;
        ImageView imageView;
    }


}

Objects.java

public class Objects(){
private int id;
private String name;
private String url;

public Objects ( String name, String url)
{
this.name = name;
this.url=url;
}
public Objects()
{
}
public int getId() { return id; }

public String getName() { return name;}
public String getUrl() { return url;}
}
BNK
  • 22,674
  • 8
  • 69
  • 81
Moudiz
  • 6,775
  • 18
  • 67
  • 139
  • What does the stack trace exactly say? – TheRealChx101 Nov 15 '15 at 06:36
  • @chx101 i cant run the app because I am getting error here `super(context, layoutResourceId, listitem);` the error is `Cannot resolve method super(android.content.Context,int,java.util.list)` – Moudiz Nov 15 '15 at 06:38
  • What class does `DBadapter` extend? Make sure you have required imports. – TheRealChx101 Nov 15 '15 at 06:40
  • @chx101 it extends SQLiteOpenHelper and I add all the imports required – Moudiz Nov 15 '15 at 06:42
  • 2
    Please take a look at the `SQLiteOpenHelper` constructors. The constructor you are calling doesn't exist. http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html Here's a tutorial: http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/ – TheRealChx101 Nov 15 '15 at 06:45
  • @chx101 I am already reading this tutorial, and there is not constructor for `SQLiteOpenHelper` beside my question is in the adapter not in sqlite. I am passing a list in the adapter however I am getting error . is it correct how I am passing the List object? – Moudiz Nov 15 '15 at 06:50
  • @Moudiz Upvote, question is clear an no downvote needed. – Milad Faridnia Nov 15 '15 at 06:51
  • @Milad thank you man, can you help me in my question? – Moudiz Nov 15 '15 at 06:52
  • 1
    I'm trying to do so ;) – Milad Faridnia Nov 15 '15 at 06:53
  • Are you following that tutorial or do you mind posting your whole code here (the adapter class)? – TheRealChx101 Nov 15 '15 at 06:57
  • @chx101 in that site they are just explaining about sqlite , what I am trying to do is pass those data to adapter . anyway check my edit – Moudiz Nov 15 '15 at 07:16
  • @chx101 still error exists, maybe you right I am extending arrayadapter however I am using list. is it possible it is the problem ? – Moudiz Nov 15 '15 at 07:20
  • Just use the two argument constructor. Change `super(context, layoutResourceId, listitem);` to `super(content, 0);` Override the other functions, `getCount()`, `getItem()`, etc – TheRealChx101 Nov 15 '15 at 07:22
  • @chx101 do you a good tutorial on how to diplay sqlite data into a list customized adapter – Moudiz Nov 15 '15 at 07:35
  • `public List getAllObjects()` do not return a `List`, simply return a `Cursor` instead and pass it to `SimpleCursorAdapter` that is all... just compare [this](https://thinkandroid.wordpress.com/2010/01/09/simplecursoradapters-and-listviews/) with your (not working yet) code – pskink Nov 15 '15 at 07:56
  • @pskink the link had a `SimpleCursorAdapter`, I am working on custom, do you have an example ? – Moudiz Nov 18 '15 at 12:33
  • i sent you a link, it is a custom adapter – pskink Nov 18 '15 at 12:40
  • @pskink in the link you provided they mentioned simplecursoradapter, can you please point where custom adapter – Moudiz Nov 18 '15 at 12:52
  • you can customize SimpleCursorAdapter like in my link, what is not clear? – pskink Nov 18 '15 at 12:56
  • @Moudiz Why do you need to super. What if you remove it? – Lay Leangsros Nov 18 '15 at 13:37
  • @LayLeangsros I am used to use super, I am not sure but ill get an error if I remove it. – Moudiz Nov 18 '15 at 13:41
  • take a look at the link i posted: they show two fields per one row like you, but they use 1/5 lines of code compared to your solution – pskink Nov 18 '15 at 15:03
  • @pskink this is your [link](https://thinkandroid.wordpress.com/2010/01/09/simplecursoradapters-and-listviews/) correct ? there is no custom adapter, I want to use picasso , there is simpleadapter only – Moudiz Nov 18 '15 at 15:06

3 Answers3

2

Try to look at the difference between what have you done and what you need to do.

public class DBadapter extends ArrayAdapter<Objects> {

    List<Object> modelItems = null;
    Context context;    
    public Resources res;


    public DBadapter (Context context,List<Object> resource) {
        super(context,R.layout.grid_item_layout,resource);
        // TODO Auto-generated constructor stub
        this.context = context;
        this.modelItems = resource;             
    }

Hope it will help you.

JorgeAmVF
  • 1,392
  • 3
  • 14
  • 28
KishuDroid
  • 5,858
  • 4
  • 28
  • 45
1

try this:

// Changed Listitem to Objects
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.text);
            holder.imageView = (ImageView) row.findViewById(R.id.imageView);
            row.setTag(holder);
        } else {
            holder = (ViewHolder) row.getTag();
        }
        // Added (ListItem) cast
        Listitem item = (Listitem) getItem(position); // here you should define your getItem(int) function in Objects class
        System.out.println("item.getUrl() ");
        System.out.println(item.getUrl());
        Picasso.with(mcontext).setIndicatorsEnabled(true);
        holder.imageTitle.setText(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)  ; //This line raises error


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


        return row;
    }

    static class ViewHolder {
        TextView imageTitle;
        ImageView imageView;
    }


}
The Badak
  • 1,912
  • 1
  • 12
  • 25
  • ok ill test it but can you please explain why you change it to Object ? my setter/getter is Objects – Moudiz Nov 18 '15 at 09:28
  • this code in my maint activity what should I replace with `objects` because I am getting error DBadapter adapter = new DBadapter(getApplicationContext(), R.layout.grid_item_layout, Objects); – Moudiz Nov 18 '15 at 09:46
  • I tried this but didnt work Listitem item = (Listitem) getItem(object.getId()); what should I add ? – Moudiz Nov 18 '15 at 11:03
  • can you post your `Objects` class? – The Badak Nov 18 '15 at 11:38
  • I dont have acess to the laptop, but its setter and getter , setId and getId. the error on the android studio said : expected listobject something like that – Moudiz Nov 18 '15 at 11:43
1

I suggest you update your classes like the following:

Objects.java:

public class Objects {
    private int id;
    private String name;
    private String url;

    public Objects() {
    }

    public Objects(int id, String name, String url) {
        this.id = id;
        this.name = name;
        this.url = url;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }
}

DBAdapter.java:

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.textView);
            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());
        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;
    }
}
BNK
  • 22,674
  • 8
  • 69
  • 81
  • Tomorrow morning OK? I am busy now – BNK Nov 21 '15 at 12:53
  • yes no problem when i ask it ill post it here .. thanks – Moudiz Nov 21 '15 at 13:03
  • hi man can you help me in this http://stackoverflow.com/questions/33843875/passing-arraylist-to-asnother-activity-cannot-resolve-putextra? – Moudiz Nov 22 '15 at 11:57
  • @Moudiz I have not tried sending arraylist to other activity yet, tomorrow if your issue is not fixed and if I have free time, I will check for you :). IMO you can read some links http://stackoverflow.com/questions/15790499/passing-arraylist-of-objects-between-activities and http://stackoverflow.com/questions/21250339/how-to-pass-arraylistcustomeobject-from-one-activity-to-another and http://stackoverflow.com/questions/26619027/pass-arraylist-of-objects-in-another-activity – BNK Nov 22 '15 at 13:54
  • hi man I did read the links , they are all using parcelable but the problem is with intent. It needs to start activity. and i only to need to pass it to another activity, not to start a new activity. – Moudiz Nov 22 '15 at 22:56
  • @Moudiz, IMO, `pass to another activity` = `start a new activity` – BNK Nov 23 '15 at 03:57
  • i posted a question here explaining detailed my problem http://stackoverflow.com/questions/33866314/when-cicked-on-grid-view-how-to-send-arralistposition-to-another-actvity – Moudiz Nov 23 '15 at 08:18
  • I have answered, however, why don't you want to go to another activity? If so, where does the arraylist will come to and how to process it in the second activity? – BNK Nov 23 '15 at 08:58