我正在创建一个使用sqlite和drawable图像的Android应用程序。
我需要的是根据sqlite数据库中的名称显示drawable文件夹中存在的图像,但系统显示错误:
方法getResources()未定义类型CustomAdapterMatchSchedule
这是我的代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Group"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/txtDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Date"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/textName1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageView1"
android:layout_alignParentLeft="true"
android:text="name1" />
<TextView
android:id="@+id/textName2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageView2"
android:layout_alignParentRight="true"
android:text="Name2" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textLocation"
android:layout_toRightOf="@+id/textName1"
android:adjustViewBounds="true"
android:maxHeight="40dp"
android:maxWidth="40dp"
android:scaleType="fitCenter"
android:src="@drawable/algeria_flag" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/imageView1"
android:layout_toLeftOf="@+id/textName2"
android:adjustViewBounds="true"
android:maxHeight="40dp"
android:maxWidth="40dp"
android:scaleType="fitCenter"
android:src="@drawable/algeria_flag" />
<TextView
android:id="@+id/textLocation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textGroup"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:text="Location" />
</RelativeLayout>
package com.devleb.expandablelistdemo3;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomAdapterMatchSchedule extends BaseAdapter {
ArrayList<ItemDetails> itemdetailsList;
Context context;
public CustomAdapterMatchSchedule(Context context, ArrayList<ItemDetails> list) {
this.context = context;
itemdetailsList = list;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return itemdetailsList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return itemdetailsList.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup arg2) {
// TODO Auto-generated method stub
ItemDetails itemdetail = itemdetailsList.get(position);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row_list_match_schedule, null);
}
//Stad_name
TextView txtStadName = (TextView)convertView.findViewById(R.id.textLocation);
txtStadName.setText(itemdetail.getStad_name());
//team1
TextView txtTeam1 = (TextView)convertView.findViewById(R.id.textName1);
txtTeam1.setText(itemdetail.getTeam1());
//team2
TextView txtTeam2 = (TextView)convertView.findViewById(R.id.textName2);
txtTeam2.setText(itemdetail.getTeam2());
//List of images
//****get the Image from drwable folder according to their names in the sqlite database******//
//
// ImageView imgflag1 = (ImageView)convertView.findViewById(R.id.imageView1);
// imgflag1.
int imageid = getResources().getIdentifier("com.devleb.expandablelistdemo3:drawable/brazil_flag", null, null);
ImageView imagenow = (ImageView)convertView.findViewById(R.id.imageView1);
imagenow.setImageResource(imageid);
return null;
}
}
所以如何从这里继续任何人都可以帮助我???
答案 0 :(得分:1)
getIdentifier没问题,但你应该这样使用它:
int imageid = getResources().getIdentifier("brazil_flag", "drawable", context.getPackageName());
第一个参数是您要查找的资源的名称,第二个参数是资源的类型,第三个参数是要查找的包。