我有一个使用SQLite
来存储和检索数据的Android应用程序。
在我的项目中,我有可绘制文件夹中的图像,我将其名称存储在SQLite
数据库中,我想要的是根据ListView
中的名称显示这些图像。
我知道我需要在代码中添加字段名称,但是如何将图像链接到其名称?
这是我的代码:
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 GROUP = "group";
public static final String[] ALL_KEYS = new String[] { PLACE_ID, STAD_NAME,
TEAM1, TEAM2, STAGE, MATCH_DATE, GROUP };
// *****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, GROUP};
int[] toViewFields = new int[] { R.id.txtDate, R.id.textName1, R.id.textName2, R.id.textLocation, R.id.textGroup};
// 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 :(得分:2)
试试这个:
String imgName = "ic_launcher"; // specify here your image name fetched from db
String uri = "drawable/" + imgName;
int icon = getResources().getIdentifier(uri, "drawable", getPackageName());
imageView.setImageResource(icon);