1

我正在尝试ListView在 android 中构建一个,其中的每个项目ListView都由 oneImgeView和 4组成TexView。我的getView类扩展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

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));

这是我返回 rutaImagenLogo 值的 itemRestaurantes 类

    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 的每个项目的信息填充 ArrayList 的活动类

 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)

有人告诉我该imageResource值为 0。而 0 不是有效的资源 ID。我该如何解决这个值?请帮帮我,我不明白

4

2 回答 2

0

我认为 imageResource 是 0。检查这个值。

getIdentifier返回 int 关联的资源标识符。如果没有找到这样的资源,则返回 0。(0 不是有效的资源 ID。)

于 2013-02-08T16:36:08.513 回答
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 您只需要更改一些名称。

于 2013-02-08T17:59:37.520 回答