2015-06-19 45 views
-1

需要使用適配器添加文本和圖像的如下所示,圖像和文本應該從適配器(而不是從XML文件)傳遞,因爲我是新的android請分享代碼。需要使用適配器添加文本

+6

「如下所示」。哪裏? –

+0

你是不是指'TextView'和'ImageView'? – M090009

+0

是的,需要同時添加 –

回答

0

如果您需要添加一個TextViewImageView編程的適配器,你可以做到這一點爲:

public View getView(int position, View convertView, ViewGroup parent) { 
     //create cell layout 
     LinearLayout layout = new LinearLayout(context); 
     layout.setLayoutParams(new AbsListView.LayoutParams(
       AbsListView.LayoutParams.MATCH_PARENT, 
       AbsListView.LayoutParams.MATCH_PARENT)); 
     layout.setOrientation(LinearLayout.HORIZONTAL); 
     //create your TextView 
     TextView textView = new TextView(context); 
     //create your ImageView 
     ImageView imageView = new ImageView(context); 

     //add them to the cell layout 
     layout.addView(textView); 
     layour.addView(imageView); 

    } 

我推薦你使用Viewholder模式,你可以找到here或只是搜索,它通過重複使用已經在屏幕上的視圖來保存內存,而不是每次都創建一個新視圖。

相關問題