Android 获取位置适配器中的空值
我需要修复这个代码Android 获取位置适配器中的空值,android,sqlite,Android,Sqlite,我需要修复这个代码Objects item=getItem(position) 你能告诉我为什么我得到null 这里显示的结果为空 System.out﹕ item.getUrl() System.out﹕ null 请注意,数据库sqlite正在返回数据 www.i I/System.out﹕ www.i.Objects@c880a09, com.justedhak.www.i.Objects@14f1340e, www.i.Objects@10f6dc3c, com.justedhak.w
Objects item=getItem(position)代码>
你能告诉我为什么我得到null
这里显示的结果为空
System.out﹕ item.getUrl()
System.out﹕ null
请注意,数据库sqlite
正在返回数据
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, ]
我试图显示从sqlite到网格视图适配器的值
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);
这是完整的代码适配器
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;
}
}
GetAllObject
// 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;
}
//获取所有联系人
公共列表getAllObjects(){
List Objectslist=新建ArrayList();
//选择所有查询
String selectQuery=“SELECT*FROM”+表对象;
SQLiteDatabase db=this.getWritableDatabase();
Cursor Cursor=db.rawQuery(selectQuery,null);
//循环遍历所有行并添加到列表
if(cursor.moveToFirst()){
做{
Objects对象=新对象();
setId(Integer.parseInt(cursor.getString(0));
object.setName(cursor.getString(1));
setUrl(cursor.getString(2));
//将联系人添加到列表中
Objectslist.add(对象);
}while(cursor.moveToNext());
}
//返回联系人列表
返回对象列表;
}
而不是
object.setId(Integer.parseInt(cursor.getString(0)));
object.setName(cursor.getString(1));
object.setUrl(cursor.getString(2));
使用
重写Objects类中的toString方法并打印Objects.toString()以检查其内容。我怀疑getUrl是否返回null。或者可能只是在从数据库获取数据时将log放入getAllObjects方法中检查url是否为null?@DhavalPatel我确实在getAllObjects中添加了log…List Objects=db.GetAllObject();Log.d(“he”,“tttttt”);System.out.println(对象)代码>和我在问题中提到的输出是www.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,]这只是打印对象哈希代码,重写toString方法以查看对象类的内容。如果您使用的是android studio,请按Alt+Insert并选择对象类中的toString,IDE将为您生成toString方法。c
指的是什么?确定。。但是仍然会得到null[Objects{id=10,name='H',url='null',type='null',category='null'},
为什么您认为我的url会得到null,而我正在为它设置值?值。put(OBJECT\u url,“http://api.androidhive.info/images/sample.jpg“”;//对象URL
检查db.insert的值(表\u对象,null,值);,插入数据时。它将返回新插入行的行ID,如果发生错误,则返回-1。以前是否尝试插入没有url的相同数据。
// 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;
}
object.setId(Integer.parseInt(cursor.getString(0)));
object.setName(cursor.getString(1));
object.setUrl(cursor.getString(2));
object.setId(cursor.getInt(cursor.getColumnIndex(OBJECT_ID)));
object.setName(cursor.getString(cursor.getColumnIndex(OBJECT_NAME)));
object.setUrl(cursor.getString(cursor.getColumnIndex(OBJECT_URL)));