我正在尝试在android中构建ListView
,其中ListView
的每个项目由一个ImgeView
和4 TexView
组成。我的班级getView
的{{1}}定义如下:
BaseAdapter
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
ViewHolder holder;
if(vi == null) {
LayoutInflater inflater = (LayoutInflater) Activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
vi = inflater.inflate(R.layout.item_lista_restaurantes, null);
holder = new ViewHolder();
holder.nombreRestaurante= (TextView) vi.findViewById(R.id.etiquetaNombreResItemRes);
holder.direccionRestaurante = (TextView) vi.findViewById(R.id.etiquetaDireccionResItemRes);
holder.ciudadRestaurante = (TextView) vi.findViewById(R.id.etiquetaCiudadResItemRes);
holder.telefonoRestaurante = (TextView) vi.findViewById(R.id.etiquetaTelResItemRes);
holder.lineaLogo = (TextView) vi.findViewById(R.id.etiquetaLineaLogo);
vi.setTag(holder);
}
else
{
holder = (ViewHolder)vi.getTag();
}
itemRestaurante item= this.itemR.get(position);
ImageView image = (ImageView) vi.findViewById(R.id.imageLogoItemRestaurante);
int imageResource = this.Activity.getResources().getIdentifier(item.getRutaImaLogo(), null, this.Activity.getPackageName());
image.setImageDrawable(this.Activity.getResources().getDrawable(imageResource));
holder.nombreRestaurante.setText(item.getNombreR());
holder.direccionRestaurante.setText(item.getDireccionR());
holder.ciudadRestaurante.setText(item.getCiudadR());
holder.telefonoRestaurante.setText(item.getTelR());
holder.lineaLogo.setText(item.getDireccionR());
return vi;
}
上的代码行:
getView
这是itemRestaurantes类,我返回rutaImagenLogo值
ImageView image = (ImageView) vi.findViewById(R.id.imageLogoItemRestaurante);
int imageResource = this.Activity.getResources().getIdentifier(item.getRutaImaLogo(), null, this.Activity.getPackageName());
image.setImageDrawable(this.Activity.getResources().getDrawable(imageResource));
这是活动类,其中我使用ListView的每个项目的信息填充ArrayList
package clasesExtras;
public class itemRestaurante {
private long idRestaurante;
private String nombreRestaurante;
private String direccionRestaurante;
private String ciudadRestaurante;
private String telefonoRestaurante;
private String rutaImagenLogo;
//private String rutaImagenGo;
//private String rutaImagenLineaLogo;
/*Constructor*/
public itemRestaurante(long id, String nombre, String direccion, String ciudad,
String telefono, String rutaLogo){
this.idRestaurante= id;
this.nombreRestaurante= nombre;
this.direccionRestaurante= direccion;
this.ciudadRestaurante= ciudad;
this.telefonoRestaurante= telefono;
this.rutaImagenLogo= rutaLogo;
//this.rutaImagenGo= rutaGo;
//this.rutaImagenLineaLogo = rutaLineaLogo;
}
public long getId(){
return this.idRestaurante;
}
public void setId(long id){
this.idRestaurante= id;
}
public String getNombreR(){
return this.nombreRestaurante;
}
public void setNombreR(String nombre){
this.nombreRestaurante=nombre;
}
public String getDireccionR(){
return this.direccionRestaurante;
}
public void setDireccionR(String direccion){
this.direccionRestaurante=direccion;
}
public String getCiudadR(){
return this.ciudadRestaurante;
}
public void setCiudadR(String ciudad){
this.ciudadRestaurante= ciudad;
}
public String getTelR(){
return this.telefonoRestaurante;
}
public void setTelR(String telefono){
this.telefonoRestaurante= telefono;
}
public String getRutaImaLogo(){
return this.rutaImagenLogo;
}
public void setRutaImaLogo(String imagenLogo){
this.rutaImagenLogo= imagenLogo;
}
上面显示的代码引用了存储它的图像路径(可绘制文件夹),我想在listview的每个项目中显示此图像,但是这会跳转到以下错误:
package appetite.apptitud;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Spinner;
import clasesExtras.ArrayAdapterRestaurantes;
import clasesExtras.itemRestaurante;
public class ListaRestaurantes extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.lista_restaurantes);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); //forza a android a ocultar el teclado virtual
ListView lista = (ListView)findViewById(R.id.listaRestaurantes);
ArrayList<itemRestaurante> itemRestaurante = obtenerItems();
ArrayAdapterRestaurantes adapter = new ArrayAdapterRestaurantes(this, itemRestaurante);
lista.setAdapter(adapter);
}
//Método para llenar la lista de los restaurantes.
private ArrayList<itemRestaurante> obtenerItems(){
ArrayList<itemRestaurante> items = new ArrayList<itemRestaurante>();
items.add(new itemRestaurante(1, "Frisby", "Cra. 7a 24-74", "Pereira", "3168899", "drawable/frisby_logo"));
items.add(new itemRestaurante(2, "Big Pollo", "Cra. 5a 34-12", "Pereira", "3147152", "drawable/bigpollo"));
items.add(new itemRestaurante(3, "Wingz", "Cra. 8a 18-62", "Pereira", "3391000", "drawale/wingz"));
items.add(new itemRestaurante(4, "Sir Pollo", "Cra. 7a 20-04", "Pereira", "3357913", "drawable/sirpollo"));
return items;
}
有些人告诉我 FATAL EXCEPTION: main
android.content.res.Resources$NotFoundException:Resource ID #0x0
at android.content.res.Resources.getValue(Resources.java:1014)
at android.content.res.Resources.getDrawable(Resources.java:659)
at clasesExtras.ArrayAdapterRestaurantes.getView(ArrayAdapterRestaurantes.java:56)
值为0. 0不是有效的资源ID。
我该怎么做才能解决这个问题呢?
请帮帮我,我不明白
答案 0 :(得分:0)
我认为imageResource为0.检查此值。
getIdentifier返回int关联的资源标识符。如果未找到此类资源,则返回0。 (0不是有效的资源ID。)
答案 1 :(得分:0)
您必须为列表视图
创建自定义适配器这将是Restaurant.java
public class Restaurant{
public int icon;
public String someText;
public String moreText;
public String someMoreText;
public String evenMoreText;
public Restaurant(int icon, String someText, String moreText, String somMoreText, String evenMoreText)
super.();
this.someText = someText;
this.moreText = moreText;
this.someMoreText = someMoreText;
this.evenMoreText = evenMoreText;
this.icon = icon;
}
这是自定义适配器RestaurantAdapter.java
public class RestaurantAdapter extends<Restaurant> }
Context context;
int layoutResourceId;
Restaurant data[] = null;
public RestaurantAdapter(Context context, int layoutResourceId, Restaurant[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
RestaurantHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new RestaurantHolder();
holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
holder.txtID1 = (TextView)row.findViewById(R.id.txtID1);
holder.txtID2 = (TextView)row.findViewById(R.id.txtID2);
holder.txtID3 = (TextView)row.findViewById(R.id.txtID3);
holder.txtID4 = (TextView)row.findViewById(R.id.txtID4);
row.setTag(holder);
}
else
{
holder = (RestauantHolder)row.getTag();
}
Restaurant restaurant = data[position];
holder.txtID1.setText(restaurant.someText);
holder.txtID1.setText(restaurant.moreText);
holder.txtID1.setText(restaurant.someMoreText);
holder.txtID1.setText(restaurant.evenMoreText);
holder.imgIcon.setImageResource(restarant.icon);
return row;
}
}
static class RestaurantHolder
{
ImageView imgIcon;
TextView txtID1;
TextView txtID2;
TextView txtID3;
TextView txtID4;
}
现在,在你的活动中,这将是一个像这样的电话
Restaurant restaurant_data[] = new Restaurant[]
{
new Restaurant(R.drawable.pic1, "some text", "more text", "some more text". "even more text"),
new Restaurant(R.drawable.pic2, "text", "more", "some more", "blah"),
new Restaurant(R.drawable.pic3, "blah1", "blah2", "blah3" "blah3"),
new Restaurant(R.drawable.pic4, "you get the idea", "blah", "blah", "blah"),
};
RestaurantAdapter adapter = new RestaurantAdapter(this,
R.layout.listview_item_row, restaurant_data);
listView1 = (ListView)findViewById(R.id.listView1);
信用转到http://www.ezzylearning.com/tutorial.aspx?tid=1763429 你只需改变一些名字。