我想将数据从corsor转换为String []数组 因为我可以将此静态类更改为动态 我想用my cols代替imageResIds和imageTitles 它是静态类
public class StaticImageData {
private static ImageItem[] mImageItemArray;
private static int[] imageResIds = {R.drawable.android,
R.drawable.hamburger, R.drawable.globe, R.drawable.notepad,
R.drawable.play, R.drawable.sun, R.drawable.tux, R.drawable.yawn};
private static String[] imageTitles = {"Android", "Burger", "Globe",
"Note Pad", "Play", "Sun", "Tux", "Yahh!"};
/**
* Default image to show
*/
public static final int DEFAULT_IMAGE_INDEX = 0;
/**
* @return static instance of the image item array
*/
public static ImageItem[] getImageItemArrayInstance() {
if (mImageItemArray == null) {
mImageItemArray = new ImageItem[imageResIds.length];
for (int i = 0; i < mImageItemArray.length; i++) {
ImageItem imageItem = new ImageItem(imageTitles[i], imageResIds[i]);
mImageItemArray[i] = imageItem;
}
}
return mImageItemArray;
}
}
它来自我的db
的getall()public VerbItem[] getAllVerbs() {
final SQLiteDatabase db = MyDbHelper.getReadableDatabase();
Cursor cursor = null;
ArrayList<VerbItem> myverbs = new ArrayList<VerbItem>();
try {
cursor = db.query(MyTables.TABLE_VERBS,
VerbsQuery.PROJECTION,
null, null, null, null, null);
if (cursor.moveToFirst()) {
do {
VerbItem page = new VerbItem(
cursor.getInt(VerbsQuery.TAG_ID_VERB_COL),
cursor.getInt(VerbsQuery.TAG_ID_PAGE_COL),
cursor.getString(VerbsQuery.TAG_VERB_COL),
cursor.getString(VerbsQuery.TAG_TEMP1_COL),
cursor.getString(VerbsQuery.TAG_TEMP2_COL),
cursor.getString(VerbsQuery.TAG_TEMP3_COL),
cursor.getString(VerbsQuery.TAG_TRAD_COL),
cursor.getString(VerbsQuery.TAG_EXEMPL_COL),
cursor.getString(VerbsQuery.TAG_V_IMAG_COL),
cursor.getString(VerbsQuery.TAG_VAR1_COL));
myverbs.add(page);
} while (cursor.moveToNext());
}
VerbItem.setCount(cursor.getCount());
Log.i(TAG, "c: "+ cursor.getCount());
return myverbs.toArray(new VerbItem[myverbs.size()]);
} finally {
if (cursor != null) {
cursor.close();
}
}
}
public interface VerbsQuery {
int _TOKEN = 0x4;
String TBN = MyTables.TABLE_VERBS;
String[] PROJECTION = {
MyTables.ID_VERB_COL,
MyTables.ID_PAGE_COL,
MyTables.VERB_COL,
MyTables.TEMP1_COL,
MyTables.TEMP2_COL,
MyTables.TEMP3_COL,
MyTables.TRAD_COL,
MyTables.EXEMPL_COL,
MyTables.V_IMAG_COL,
MyTables.VAR1_COL,
};
int TAG_ID_VERB_COL = 0;
int TAG_ID_PAGE_COL = 1;
int TAG_VERB_COL = 2;
int TAG_TEMP1_COL = 3;
int TAG_TEMP2_COL = 4;
int TAG_TEMP3_COL = 5;
int TAG_TRAD_COL = 6;
int TAG_EXEMPL_COL = 7;
int TAG_V_IMAG_COL =8;
int TAG_VAR1_COL =9;
}
}
我已经尝试了但是总是java.lang.NullPointerException
public List<FeedPage> getAllPagess() {
SQLiteDatabase db = mDatabaseHelper.getReadableDatabase();
Cursor cursor = null;
List<FeedPage> feedpages = new ArrayList<FeedPage>();
cursor = db.query(FeedContract.Entry.TABLE_NAME,
new String[] {FeedContract.Entry.COLUMN_NAME_ENTRY_ID, FeedContract.Entry.COLUMN_NAME_TITLE, FeedContract.Entry.COLUMN_NAME_LINK, FeedContract.Entry.COLUMN_IMAG_LINK,FeedContract.Entry.COLUMN_TEXT_ENTRY,FeedContract.Entry.COLUMN_NAME_PUBLISHED},
null, null, null, null, FeedContract.Entry.COLUMN_NAME_PUBLISHED + " desc");
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
FeedPage feedpage = new FeedPage();
feedpage.setId(cursor.getInt(TAG_ID));
feedpage.setTitle(cursor.getString(TAG_TITLE).toString());
feedpage.setLink(cursor.getString(TAG_LINK).toString());
feedpage.setImaglink(cursor.getString(TAG_IMAG_LINK).toString());
feedpage.setTextentry(cursor.getString(TAG_TEXT_ENTRY).toString());
feedpage.setPublished(cursor.getLong(TAG_PUBLISHED));
Log.d(TAG, "get comment = " + cursorToComment(cursor).toString());
feedpages.add(feedpage);
cursor.moveToNext();
}
// Make sure to close the cursor
cursor.close();
return feedpages;
}
答案 0 :(得分:0)
您可以尝试从资源中的文件名加载文件,如下所示:
public Drawable getDrawableFromString(String myFaleName){
Resources res = getResources();
int resID = res.getIdentifier(myFaleName, "drawable", getPackageName());
Drawable drawable = res.getDrawable(resID );
return drawable;
}