您好我正在尝试实现自己的自定义适配器。
我想要的是一个包含以下项目的列表:
[图片文字]
[图片文字]
...
我用SONGNAME1,SONGNAME2等临时项填充了一个列表。然后在这里运行代码是得到的:
它仅显示整个屏幕上列表的最后一个元素。我期待的是对每个列表行都有这种效果。
您可以在下面找到相关的代码。
这是主要活动:
...
protected void onPostExecute(String s) {
super.onPostExecute(s);
List<ListHolder> list = setList();
CustomAdapter adp = new CustomAdapter(list,getApplicationContext());
if (s == null) {
mListView.setAdapter(adp);
}
}
private List<ListHolder> setList() {
// TODO Auto-generated method stub
List<ListHolder> list = new ArrayList<ListHolder>();
ListHolder l = new ListHolder();
setContentView(R.layout.adapter);
l.song_name = (TextView) findViewById(R.id.name);
l.artist_name = (TextView) findViewById(R.id.dist);
l.thumbnail = (ImageView) findViewById(R.id.img);
for (int i = 0; i < 10; i++) {
l.song_name.setText("SONGNAME"+i);
l.artist_name.setText("ARTISTNAME"+i);
l.thumbnail.setImageBitmap(getBitmapFromURL("a URL"));
list.add(l);
}
return list;
}
这是适配器:
public class CustomAdapter extends BaseAdapter{
private List<ListHolder> list;
private Context context;
public CustomAdapter(List<ListHolder> list, Context ctx) {
//super(ctx, R.layout.adapter, list);
this.list = list;
this.context = ctx;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ListHolder holder = new ListHolder();
// First let's verify the convertView is not null
if (convertView == null) {
// This a new view we inflate the new layout
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.adapter, null);
// Now we can fill the layout with the right values
TextView tv = (TextView) v.findViewById(R.id.name);
TextView distView = (TextView) v.findViewById(R.id.dist);
ImageView img = (ImageView) v.findViewById(R.id.img);
holder.song_name = tv;
holder.artist_name = distView;
holder.thumbnail = img;
v.setTag(holder);
}
else{
holder = (ListHolder) v.getTag();
}
holder.song_name.setText("song name");
holder.artist_name.setText("artist name");
holder.thumbnail.setImageBitmap(getBitmapFromURL("a URL"));
//holder.img.setImageResource(p.getIdImg());
v.setOnClickListener(new OnItemClickListener( position ));
return v;
}
这是adapter.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
/>
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/img"
android:layout_toRightOf="@+id/img"
android:textStyle="bold" />
<TextView
android:id="@+id/dist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/name"
android:layout_gravity="center"
android:layout_marginTop="2dip"
android:layout_toRightOf="@id/img"
android:gravity="right"
android:textSize="8dp"
android:textStyle="italic" />
</RelativeLayout>
任何帮助将不胜感激!
答案 0 :(得分:0)
imageview和relativelayout的高度应该是wrap_content。 还可以在setList方法()中删除setContentView()函数。