Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 安卓(Android)ListView 顯示圖片文字

安卓(Android)ListView 顯示圖片文字

編輯:關於Android編程

一.代碼實現

1.  “Activity_11\src\yan\activity_11\MainActivity.java”

package yan.activity_11; 
 
import android.os.Bundle; 
import android.app.Activity; 
import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.ImageView; 
import android.widget.ListView; 
import android.widget.TextView; 
 
public class MainActivity extends Activity { 
  ListView listView; 
  String [] titles={"標題1","標題2","標題3","標題4"}; 
  String [] texts={"文本內容A","文本內容B","文本內容C","文本內容D"}; 
  int [] resIds={R.drawable.icon,R.drawable.icon,R.drawable.icon,R.drawable.icon}; 
   
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    this.setTitle("BaseAdapter for ListView"); 
    listView=(ListView)this.findViewById(R.id.MyListView); 
    listView.setAdapter(new ListViewAdapter(titles,texts,resIds)); 
  } 
   
  public class ListViewAdapter extends BaseAdapter{ 
    View [] itemViews; 
     
    public ListViewAdapter(String [] itemTitles, String [] itemTexts, 
        int [] itemImageRes){ 
      itemViews = new View[itemTitles.length]; 
       
      for (int i=0; i<itemViews.length; ++i){ 
        itemViews[i] = makeItemView(itemTitles[i], itemTexts[i], 
            itemImageRes[i]); 
      } 
    } 
     
    public int getCount()  { 
      return itemViews.length; 
    } 
     
    public View getItem(int position)  { 
      return itemViews[position]; 
    } 
     
    public long getItemId(int position) { 
      return position; 
    } 
     
    private View makeItemView(String strTitle, String strText, int resId) { 
      LayoutInflater inflater = (LayoutInflater)MainActivity.this 
          .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       
      // 使用View的對象itemView與R.layout.item關聯 
      View itemView = inflater.inflate(R.layout.listview_item, null); 
       
      // 通過findViewById()方法實例R.layout.item內各組件 
      TextView title = (TextView)itemView.findViewById(R.id.itemTitle); 
      title.setText(strTitle); 
      TextView text = (TextView)itemView.findViewById(R.id.itemText); 
      text.setText(strText); 
      ImageView image = (ImageView)itemView.findViewById(R.id.itemImage); 
      image.setImageResource(resId); 
       
      return itemView; 
    } 
     
    public View getView(int position, View convertView, ViewGroup parent) { 
      if (convertView == null) 
        return itemViews[position]; 
      return convertView; 
    } 
  } 
} 

2.package yan.activity_11;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {
 ListView listView;
 String [] titles={"標題1","標題2","標題3","標題4"};
 String [] texts={"文本內容A","文本內容B","文本內容C","文本內容D"};
 int [] resIds={R.drawable.icon,R.drawable.icon,R.drawable.icon,R.drawable.icon};
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
 this.setTitle("BaseAdapter for ListView");
 listView=(ListView)this.findViewById(R.id.MyListView);
 listView.setAdapter(new ListViewAdapter(titles,texts,resIds));
 }
 
 public class ListViewAdapter extends BaseAdapter{
 View [] itemViews;
 
 public ListViewAdapter(String [] itemTitles, String [] itemTexts,
  int [] itemImageRes){
  itemViews = new View[itemTitles.length];
  
  for (int i=0; i<itemViews.length; ++i){
  itemViews[i] = makeItemView(itemTitles[i], itemTexts[i],
   itemImageRes[i]);
  }
 }
 
 public int getCount() {
  return itemViews.length;
 }
 
 public View getItem(int position) {
  return itemViews[position];
 }
 
 public long getItemId(int position) {
  return position;
 }
 
 private View makeItemView(String strTitle, String strText, int resId) {
  LayoutInflater inflater = (LayoutInflater)MainActivity.this
   .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  
  // 使用View的對象itemView與R.layout.item關聯
  View itemView = inflater.inflate(R.layout.listview_item, null);
  
  // 通過findViewById()方法實例R.layout.item內各組件
  TextView title = (TextView)itemView.findViewById(R.id.itemTitle);
  title.setText(strTitle);
  TextView text = (TextView)itemView.findViewById(R.id.itemText);
  text.setText(strText);
  ImageView image = (ImageView)itemView.findViewById(R.id.itemImage);
  image.setImageResource(resId);
  
  return itemView;
 }
 
 public View getView(int position, View convertView, ViewGroup parent) {
  if (convertView == null)
  return itemViews[position];
  return convertView;
 }
 }
}

二.“Activity_11\res\layout\main.xml”

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout  
    android:id="@+id/LinearLayout01"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    xmlns:android="http://schemas.android.com/apk/res/android">  
      
    <ListView android:layout_width="wrap_content"  
         android:layout_height="wrap_content"  
         android:id="@+id/MyListView">  
    </ListView>  
</LinearLayout> 

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    android:id="@+id/LinearLayout01" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 
     
    <ListView android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:id="@+id/MyListView"> 
    </ListView> 
</LinearLayout> 

三.“Activity_11\res\layout\listview_item.xml”

<?xml version="1.0" encoding="utf-8"?>   
<RelativeLayout   
     android:layout_width="fill_parent"   
     xmlns:android="http://schemas.android.com/apk/res/android"   
     android:layout_height="wrap_content"   
     android:paddingBottom="4dip"   
     android:paddingLeft="12dip">   
     <ImageView   
        android:layout_width="wrap_content"   
        android:id="@+id/itemImage" 
        android:layout_height="fill_parent">   
     </ImageView>   
     <TextView   
        android:text="TextView01"   
        android:layout_height="wrap_content"   
        android:layout_width="fill_parent"   
        android:id="@+id/itemTitle" 
        android:layout_toRightOf="@+id/itemImage" 
        android:textSize="20dip">   
     </TextView>   
     <TextView   
        android:text="TextView02"   
        android:layout_height="wrap_content"   
        android:layout_width="fill_parent"   
        android:id="@+id/itemText" 
        android:layout_toRightOf="@+id/itemImage" 
        android:layout_below="@+id/itemTitle">   
     </TextView>   
</RelativeLayout> 
<?xml version="1.0" encoding="utf-8"?>  
<RelativeLayout  
     android:layout_width="fill_parent"  
     xmlns:android="http://schemas.android.com/apk/res/android"  
     android:layout_height="wrap_content"  
     android:paddingBottom="4dip"  
     android:paddingLeft="12dip">  
     <ImageView  
        android:layout_width="wrap_content"  
        android:id="@+id/itemImage"
        android:layout_height="fill_parent">  
     </ImageView>  
     <TextView  
        android:text="TextView01"  
        android:layout_height="wrap_content"  
        android:layout_width="fill_parent"  
        android:id="@+id/itemTitle"
        android:layout_toRightOf="@+id/itemImage"
        android:textSize="20dip">  
     </TextView>  
     <TextView  
        android:text="TextView02"  
        android:layout_height="wrap_content"  
        android:layout_width="fill_parent"  
        android:id="@+id/itemText"
        android:layout_toRightOf="@+id/itemImage"
        android:layout_below="@+id/itemTitle">  
     </TextView>  
</RelativeLayout>
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved