2011-11-25 63 views
0

我有一個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; 
     } 

我的列表活動展示我的只有文字。我必須改變才能看到帶有文字的圖像?

回答

0

檢查了一下,它是一個你需要的完整和簡單的例子。

ListView Items with Custom ArrayAdapter

+0

除非這對我沒有幫助,因爲他使用資源中的圖像,並且在我的應用程序中,我從服務器獲取此圖像(並保存它),然後在listactivity中顯示textview和imageview。我只是爲了簡化而製作這個BitmapFactory.decodeFile(「C:\\ Users \\ hormon \\ Desktop \\ favicon.ico」),因爲從服務器上的圖像工作正常。 – user1055201

0

這個問題可能與您正試圖分配冠名標識屬性的方式。 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

+0

這個參考不是問題,我忘記了改變。我不能使用BitmapFactory.decodeResource(getResources(),R.draw ....);在我的數據庫適配器中。 – user1055201

+0

啊,通常如果你有一個vanilla的java類,並且你在那個類中需要訪問上下文的功能,你必須通過一個實例變量或者更簡單地作爲參數傳遞給方法。我已經修改了上面的代碼以反映必要的更改。 – bensnider