SQLite get all data and pass it to an Adapter

后端 未结 3 1963
离开以前
离开以前 2021-01-13 17:58

I have this code which gets some data from a database

  public List getAllObjects() {
        List Objectslist = new ArrayList&         


        
相关标签:
3条回答
  • 2021-01-13 18:28

    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;
        }
    }
    
    0 讨论(0)
  • 2021-01-13 18:34

    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;
        }
    
    
    }
    
    0 讨论(0)
  • 2021-01-13 18:35

    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.

    0 讨论(0)
提交回复
热议问题