데이터베이스에서 가져 오는 문자열을 통해 이미지 리소스를 설정하는 각 행의 Imageviews를 사용하여 사용자 지정 목록보기를 만들려고합니다. 데이터베이스에는 이미지의 이름 만 포함되어 있으므로 리소스에서로드해야합니다. 이름으로 리소스를 가져 오는 모든 메서드를 시도했지만 함수는 항상 0을 반환합니다. 내가 무엇을해도 이름을 기반으로 ID를 얻을 수 없습니다. 이것은 내가 사용하고있는 사용자 정의 어댑터 ....내 코드에서 이미지의 리소스 ID를 가져 오지 못했습니다.
공용 클래스 OwnAddptor가 BaseAdapter {
private LayoutInflater mInflater;
String[] names;
String[] detailarray;
String[] adapimage;
int size;
Context mycont;
public OwnAddptor(Context context,String[] poi_names,String[] details,String[] imageadap) {
mInflater = LayoutInflater.from(context);
this.names = poi_names;
this.detailarray = details;
this.adapimage = imageadap;
this.mycont = context;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return names.length;
}
@Override
public Object getItem(int puhj) {
// TODO Auto-generated method stub
return puhj;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
ViewHolder holder;
if (vi == null) {
vi = mInflater.inflate(R.layout.blockview, null);
holder = new ViewHolder();
holder.headline = (TextView) vi.findViewById(R.id.head_line);
holder.detail = (TextView) vi.findViewById(R.id.detail);
holder.image = (ImageView) vi.findViewById(R.id.image);
vi.setTag(holder);
}
else {
holder = (ViewHolder) vi.getTag();
}
holder.headline.setText(names[position]);
holder.detail.setText(detailarray[position]);
// Drawable imgdraw = mycont.getResources().getDrawable(adapimage[position]);
holder.image.setImageDrawable(getAndroidDrawable(adapimage[position], mycont));
System.out.println(adapimage[position]);
return vi;
}
static class ViewHolder {
TextView headline;
TextView detail;
ImageView image;
}
public int getImageId(Context context, String name) {
System.out.println("current name"+name);
return context.getResources().getIdentifier("drawable/"+name, null, context.getPackageName());
}
static public Drawable getAndroidDrawable(String pDrawableName,Context cont){
int resourceId=Resources.getSystem().getIdentifier(pDrawableName, "drawable", cont.getPackageName());
System.out.println("resource ids****"+resourceId);
if(resourceId==0){
return null;
} else {
return Resources.getSystem().getDrawable(resourceId);
}
}
}
을 확장입니다 그리고 이것은 내가 데이터베이스를 쿼리 및 사용자 정의 어댑터 클래스에 배열을 전달 주요 활동 . 텍스트 뷰는 위치에 따라 올바르게 설정됩니다. 유일한 문제는 리소스 폴더에서 이미지의 int ID를 가져 오는 것입니다. 공용 클래스 MainActivity는 활동 {
DataBaseHelper baseHelper ;
SQLiteDatabase database;
OwnAddptor adapcustom;
TextView detail;
ListView view;
TextView headline;
String[] name;
String[] images;
String[] desc;
int[] imgids;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<RowItem> rowItems;
view = (ListView) findViewById(R.id.mylist);
baseHelper = new DataBaseHelper(this);
try
{
baseHelper.createDatabase();
}
catch (IOException ioe)
{
throw new Error("Unable to create database");
}
try
{
baseHelper.openDataBase();
}
catch(SQLException sqle)
{
throw sqle;
}
database = baseHelper.getReadableDatabase();
Cursor cursor= database.rawQuery("Select * from tblArpoi_local", null);
int i = 0;
if (cursor.getCount()>0) {
cursor.moveToFirst();
name = new String[cursor.getCount()];
desc = new String[cursor.getCount()];
images= new String[cursor.getCount()];
imgids = new int[cursor.getCount()];
while (cursor.moveToNext()) {
images[i]= "poi_"+cursor.getString(cursor.getColumnIndex("arPoiImageLink"));
name[i] = cursor.getString(cursor.getColumnIndex("arPoiName"));
desc[i] = cursor.getString(cursor.getColumnIndex("arPoiDescription"));
i++;
}
System.out.println("****"+images.length);
database.close();
cursor.close();
adapcustom = new OwnAddptor(this,name,desc,images);
view.setAdapter(adapcustom);
}
}
public static int getImageId(Context context, String name) {
System.out.println("current name"+name);
return context.getResources().getIdentifier("drawable/"+name, null, context.getPackageName());
}
}
당신이 데이터베이스에서 올바른 이름을 얻고 확신의 호출을 시도 할 수 있습니다? – Luksprog
int resID = getResources(). getIdentifier (pictureName, "drawable", getPackageName()); 이것도 널 null을 반환합니까? – Kanth
아래 주어진 솔루션을 사용해 보셨습니까? –