我有ListActivity
,我需要在每一行显示图片和文字。
event.xml
布局如下所示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<ImageView android:layout_height="wrap_content"
android:layout_width="30px"
android:layout_marginTop="2px" android:layout_marginRight="2px"
android:layout_marginLeft="2px" android:id="@+id/logoImage">
</ImageView>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/title"
android:textSize="15px"></TextView>
</LinearLayout>
活动如下:
public class Activity extends ListActivity{
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
dba=new DatabaseAdapter(this);
dba.open();
View header = getLayoutInflater().inflate(R.layout.header, null);
ListView lv = getListView();
lv.addHeaderView(header);
fillOnStart();
}
private void fillOnStart(){
ArrayList<Title> temp = dba.returnTitle(); // this works
ArrAdapter notes = new ArrAdapter(this, temp);
Toast.makeText(this, "Size: " +notes.getCount(), Toast.LENGTH_LONG).show();
//because this show good size
setListAdapter(notes);
}
我的适配器看起来像这样:
private class ArrAdapter extends BaseAdapter{
private Context mContext;
private ArrayList<Title> lista;
public ArrAdapter(Context c, ArrayList<Title> list){
mContext = c;
this.lista =list;
}
public int getCount() {
// TODO Auto-generated method stub
return lista.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// ViewHolder vh =null;
Title title= lista.get(position);
LayoutInflater inflater = (LayoutInflater) mContext .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.event, parent, false);
ImageView imlogo = (ImageView)v.findViewById(R.id.logoImage);
TextView textTitle = (TextView)v.findViewById(R.id.title);
imlogo.setImageResource(title.getLogo());
textTitle.setText(title.getTit());
return v;
}
}
课程标题如下:
public class Title {
public int id;
public String tit;
public Bitmap logo;
public Bitmap getLogo() {
return logo;
}
... and other setter and getter
}
和方法返回Arraylist(在dba中),其标题如下:
public ArrayList<Title> returnTitle(){
Title t = new Title(1,"aaaaaaaaaa", BitmapFactory.decodeFile("C:\\Users\\hormon\\Desktop\\favicon.ico"));
Title t1 = new Title(2,"assdsdsdsdaa", BitmapFactory.decodeFile("C:\\Users\\hormon\\Desktop\\icon.ico"));
ArrayList<Title> art = new ArrayList<Title>(2);
art.add(t);
art.add(t1);
return art;
}
我的列表活动仅显示文字。我必须改变什么才能看到带有文字的图像?
答案 0 :(得分:0)
检查一下,这是你需要的一个完整而简单的例子。
答案 1 :(得分:0)
问题可能在于您尝试分配Title徽标属性的方式。 BitmapFactory.decodeFile
方法不会使用本地路径到您的计算机,而是从Android设备本身获取路径。但是,最常见的是使用应用程序可绘制文件夹中的图像资源。
public ArrayList<Title> returnTitle(Context ctx){
Title t1 = new Title(1, "aaaaaaaaaa", BitmapFactory.decodeResource(ctx.getResources(), R.drawable.favicon));
Title t2 = new Title(2, "assdsdsdsdaa", BitmapFactory.decodeResource(ctx.getResources(), R.drawable.icon));
ArrayList<Title> art = new ArrayList<Title>(2);
art.add(t1);
art.add(t2);
return art;
}
然后从您的活动中调用该方法看起来像
private void fillOnStart(){
ArrayList<Title> temp = dba.returnTitle(this);
// ...
}
此外,我不知道您是否注意到,但在布局中,文本字段的ID为title
,而在适配器中,它被引用为tytul
。