我想从中检索数据SQLite
并显示在 中GridView
,但它显示错误。我希望有人可以帮助我。
提前致谢。
数据库处理程序.java
// Getting All icons
public List<Icon> getAllIcons() {
List<Icon> iconList = new ArrayList<Icon>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_CATALOG;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Icon icon = new Icon();
icon.setID(Integer.parseInt(cursor.getString(0)));
icon.setName(cursor.getString(1));
icon.setIconpath(cursor.getString(2));
// Adding contact to list
iconList.add(icon);
} while (cursor.moveToNext());
}
// return contact list
return iconList;
}
IconListAdapter.java
public class IconListAdapter extends BaseAdapter {
Context context;
//List<Icon> iconList;
List<Icon> iconList = new ArrayList<Icon>();
public IconListAdapter(Context context, List<Icon> list) {
this.context = context;
iconList = list;
}
@Override
public int getCount() {
return iconList.size();
}
@Override
public Object getItem(int position) {
return iconList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup arg2) {
Icon icon = iconList.get(position);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.screen_list, null);
}
TextView iconname = (TextView) convertView.findViewById(R.id.iconname);
iconname.setText(icon.getName());
TextView id = (TextView) convertView.findViewById(R.id.id);
id.setText(icon.getID());
return convertView;
}
}
显示数据库活动.java
public class DisplayDatabaseActivity extends Activity {
DatabaseHandler databasehandler;
GridView gridView;
//ArrayList<Icon> iconList = new ArrayList<Icon>();
// List<Icon> iconList = new ArrayList<Icon>();
IconListAdapter iconlistadapter;
//SimpleAdapter simpleadapter;
//ArrayList<HashMap<String, String>> iconsList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_database);
databasehandler = new DatabaseHandler(this);
//databasehandler.getAllIcons();
//List<Icon> iconList = new ArrayList<Icon>();
// List <Icon> iconList = databasehandler.getAllIcons();
//databasehandler.getAllIcons();
Log.d("Reading: ", "Reading all contacts..");
List <Icon> iconList = databasehandler.getAllIcons();
for (Icon cn : iconList) {
String log = "ID:" + cn.getID() + " Name: " + cn.getName() + " ,Image: " + cn.getIconpath();
// Writing Contacts to log
Log.d("Result: ", log);
}
gridView = (GridView) findViewById(R.id.gridView1);
iconlistadapter=new IconListAdapter(DisplayDatabaseActivity.this,iconList);
gridView.setAdapter(iconlistadapter);
}
}
日志错误:
FATAL EXCEPTION: main
android.content.res.Resources$NotFoundException: String resource ID #0x1
at android.content.res.Resources.getText(Resources.java:247)
at android.widget.TextView.setText(TextView.java:3428)
atcom.megamobile.digital_catalog.IconListAdapter.getView(IconListAdapter.java:56)
at android.widget.AbsListView.obtainView(AbsListView.java:2033)
at android.widget.GridView.onMeasure(GridView.java:1026)
at android.view.View.measure(View.java:12603)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4677)