Reputation: 7377
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;}
}
Upvotes: 4
Views: 2522
Reputation: 5451
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.
Upvotes: 2
Reputation: 24114
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;
}
}
Upvotes: 1
Reputation: 2030
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;
}
}
Upvotes: 1