我有一個ListActivity
,我需要在每一行顯示圖像和文本。Android listactivity,baseadapter - imageView
的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;
}
我的列表活動展示我的只有文字。我必須改變才能看到帶有文字的圖像?
除非這對我沒有幫助,因爲他使用資源中的圖像,並且在我的應用程序中,我從服務器獲取此圖像(並保存它),然後在listactivity中顯示textview和imageview。我只是爲了簡化而製作這個BitmapFactory.decodeFile(「C:\\ Users \\ hormon \\ Desktop \\ favicon.ico」),因爲從服務器上的圖像工作正常。 – user1055201