我有这个代码从数据库中获取一些数据
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());
}
在我的MainActivity中,我得到的就像这样
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
List<Objects> Objects = db.getAllObjects();
DBadapter adapter = new DBadapter(getApplicationContext(), R.layout.grid_item_layout, Objects);
我的DBadatper中出现错误,此处
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");
}
错误是:Cannot Resolve method..
我猜是因为List对象,但是如何修复它呢?
适配器类
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;}
}
答案 0 :(得分:2)
试着看看你做了什么和你需要做什么之间的区别。
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;
}
希望它会对你有所帮助。
答案 1 :(得分:1)
试试这个:
// 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;
}
}
答案 2 :(得分:1)
我建议你更新你的课程如下:
Objects.java:
my_env_var = ${my_build_var}
DBAdapter.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;
}
}