0

嗨,我有一个自ListView定义ImageViewTextView. 这是应该显示对象的类

package mk.mk.mk;

public class Artist {

private String artistName;
private String artistImage;
private String artistNameLatin;
 public Artist() {
    // TODO Auto-generated constructor stub
}
 public String getArtistImage() {
    return artistImage;
}
  public String getArtistName() {
    return artistName;
}
 public String getArtistNameLatin() {
    return artistNameLatin;
}
public void setArtistNameLatin(String artistNameLatin) {
this.artistNameLatin = artistNameLatin;
}



 public void setArtistImage(String artistImage) {
    this.artistImage = artistImage;
}
  public void setArtistName(String artistName) {
    this.artistName = artistName;
}
public Artist (String _artistName,String _artistImage,String _artistNameLatin)
{
    artistName=_artistName;
    artistImage=_artistImage;
    artistNameLatin=_artistNameLatin;

}

虽然自定义适配器的代码是这个

package mk.mk.mk;



import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.style.RelativeSizeSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ArtistAdapter extends ArrayAdapter<Artist>{

int resource;
private static final String ASSETS_DIR="images/";

public ArtistAdapter(Context _context,int _resource,List<Artist> objects)
{
    super(_context,_resource,objects);
    resource=_resource;
}

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    RelativeLayout artistLayoutView;
    Artist a=getItem(position);
    String artistName=a.getArtistName();

    if(convertView==null)
    {
        artistLayoutView=new RelativeLayout(getContext());
        String inflater=Context.LAYOUT_INFLATER_SERVICE;
        LayoutInflater vi=(LayoutInflater)getContext().getSystemService(inflater);
        vi.inflate(resource, artistLayoutView,true);
    }
    else
    {
        artistLayoutView=(RelativeLayout)convertView;
    }


    TextView artistTxt=(TextView)artistLayoutView.findViewById(R.id.artistName);
    ImageView resourceIdView=(ImageView)artistLayoutView.findViewById(R.id.artist_icon);
    artistTxt.setText(artistName);
    String imgFilePath=ASSETS_DIR+a.getArtistImage();

    try
    {
        Drawable d=Drawable.createFromStream(getContext().getAssets().open(imgFilePath), null);
        resourceIdView.setImageDrawable(d);
    }
    catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }
    return artistLayoutView;
}

}

应该填充项目的代码是这个

package mk.mk.mk;

import java.util.ArrayList;

import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.ListView;

public class ArtistList extends Activity{

ArrayList <String> artistNameList=new ArrayList<String>();
ArrayList <String> artistNameLatinList=new ArrayList<String>();
ArrayList <String> artistImageUrlList=new ArrayList<String>();
ArrayList<Artist> artistList=new ArrayList<Artist>();

private ArtistAdapter aa;
private ListView listViewArtists;

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.artist_list);
    Resources r=getResources();

    String [] artistNameListArray=r.getStringArray(R.array.artists);
    String [] artistImageUrlListArray=r.getStringArray(R.array.artists_url);
    String [] artistLatinList=r.getStringArray(R.array.artistsLatin);


    for(int j=0;j<artistNameListArray.length;j++)
    {
        artistList.add(new Artist(artistNameListArray[j],artistImageUrlListArray[j],artistLatinList[j]));
    }

    listViewArtists=(ListView)findViewById(R.id.listArtists);
    aa=new ArtistAdapter(ArtistList.this, R.layout.artist_listitem, artistList);
    listViewArtists.setAdapter(aa);

}

}

listView 的布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<ListView
    android:id="@+id/listArtists"
    android:layout_width="wrap_content"
    android:layout_height="370dp"
    android:layout_marginTop="50dp"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:layout_centerVertical="true">

</ListView>

</RelativeLayout>

以及列表项的布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<ImageView
    android:id="@+id/artist_icon"
    android:layout_width="40dp"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_gravity="left" />

<TextView
    android:id="@+id/artistName"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_gravity="center"
    android:layout_toRightOf="@+id/artist_icon"
    android:fadingEdge="vertical"
    android:padding="10dp"
    android:scrollbars="vertical"
    android:textSize="25sp" />



</RelativeLayout>

但是,当我运行我的应用程序时,图像没有显示,我也没有收到任何错误。图像位于应用程序文件夹中的文件夹图像中。

4

2 回答 2

1

你的问题是显示来自 android assets 文件夹的图像。

请点击链接

谢谢

于 2012-10-14T15:43:52.593 回答
0

上面的代码没问题,问题是图像文件夹不在资产文件夹中。现在它对我来说工作正常。

于 2012-10-14T16:01:24.287 回答