我有一个使用sqlite数据库的Android应用程序。我有一个可绘制文件夹中的图像,我需要在列表中显示。
我的问题是如何使用SQLite数据库中的文件名从drawable文件夹中检索图像?
这是我用来将图像添加到列表视图的代码:
package com.devleb.expandablelistdemo3;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class MatchScheduleList extends Activity {
private static final String DB_NAME = "world_cup.db";
// *****Tables name**************************//
private static final String TABLE_NAME = "match_list";
// *****Tbale name******************************//
public static final String PLACE_ID = "_id";
public static final String STAD_NAME = "stad_name";
public static final String TEAM1 = "team1";
public static final String TEAM2 = "team2";
public static final String STAGE = "stage";
public static final String MATCH_DATE = "match_date";
public static final String[] ALL_KEYS = new String[] { PLACE_ID, STAD_NAME,
TEAM1, TEAM2, STAGE, MATCH_DATE };
// *****Tbale name******************************//
private SQLiteDatabase database;
private ListView listView;
private ArrayList<String> matchList;
private ExternalDbOpenHelper extDB;
int[] img = { R.drawable.brazil_flag, R.drawable.croatian_flag,
R.drawable.mexico_flag, R.drawable.cameroon_flag, R.drawable.spain,
R.drawable.netherlands_flag, R.drawable.czech_republic_flag,
R.drawable.australia, R.drawable.colombia_flag, R.drawable.gress,
R.drawable.cote_divoire_flag, R.drawable.japan,
R.drawable.uruguay_flag, R.drawable.costa_rica_flag,
R.drawable.england_flag, R.drawable.italy_flag,
R.drawable.switzerland, R.drawable.ecuador_flag,
R.drawable.france_flag, R.drawable.honduras_flag,
R.drawable.argentina_flag, R.drawable.bousna, R.drawable.iran_flag,
R.drawable.nigeria_flag, R.drawable.germany_flag,
R.drawable.portugal, R.drawable.ghana_flag,
R.drawable.united_states_flag, R.drawable.belgium_flag,
R.drawable.algeria_flag, R.drawable.russia_flag,
R.drawable.korea_flag };
int nextImageIndex = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_match_schedule_list);
ExternalDbOpenHelper extDB = new ExternalDbOpenHelper(this, DB_NAME);
database = extDB.openDataBase();
int imgid = img[nextImageIndex];
nextImageIndex = (nextImageIndex + 1) % img.length;
populateLitsFromDB();
}
public Cursor getAllRows() {
String where = null;
Cursor c = database.query(true, TABLE_NAME, ALL_KEYS, where, null,
null, null, null, null);
if (c != null) {
c.moveToFirst();
}
return c;
}
private void populateLitsFromDB() {
// TODO Auto-generated method stub
Cursor cursor = getAllRows();
// allo0w activity to manage life cicle of the cursor
startManagingCursor(cursor);
// setup mapping from cursor to view fields
String[] fromFieldNames = new String[] { MATCH_DATE, TEAM1, TEAM2,STAD_NAME};
int[] toViewFields = new int[] { R.id.txtDate, R.id.textName1, R.id.textName2, R.id.textLocation};
// create adapter to map columns of the DB INTO ELEMENT IN THE LIST
SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(this,
R.layout.row_list_match_schedule, cursor, fromFieldNames,
toViewFields);
Log.d("in the getAllRows", myCursorAdapter.toString());
// set the adapter for te listView
ListView myList = (ListView) findViewById(R.id.list);
myList.setAdapter(myCursorAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.match_schedule_list, menu);
return true;
}
}
答案 0 :(得分:1)
好了,因为你知道所有可绘制的ID和团队名称是什么,为什么不尝试使用枚举?
例如
public enum TeamInfo {
BRAZIL("brazil", R.drawable.brazil_flag),
CROATIA("croatia", R.drawable.croatian_flag),
MEXICO("mexico", R.drawable.mexican_flag);
// Continue here
private String name;
private int resId;
TeamInfo(int name, int resId){
this.name = name;
this.resId = resId;
}
public String getName(){
return name;
}
public int getResId(){
return resId;
}
public TeamInfo getTeamInfoByName(String nameToCompare){
for(TeamInfo i : TeamInfo.values()){
if(i.getName().equalsIgnoreCase(nameToCompare)) return i;
}
return null;
}
}
然后,您可以将名称映射到各自的资源ID,并返回正确的TeamInfo和资源ID,如下所示:
TeamInfo team = TeamInfo.getTeamInfoByName(name_of_the_team);
int teamFlagResourceId = team.getResId();