Android中的自訂資料配接器

來源:互聯網
上載者:User

應用程式實體類

public class App{

    private int appId;          // 應用程式id

    private String appName;// 應用程式名稱

    private String appIcon;  // 應用程式圖示

 

    public int getAppId(){  return this.appId;  }

    public void setAppId(){   this.appId=value;  }

 

    public int getAppName(){  return this.appName;  }

    public void setAppName(){   this.appId=appName;  }

 

    public int getAppIcon(){  return this.appIcon;  }

    public void setAppIcon(){   this.appId=appIcon;  }

 

}

app_item.xml 檔案

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/imgIcon"
        android:layout_width="50px"
        android:layout_height="50px"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="5dip"
        android:layout_marginTop="2dip"
        android:src="@drawable/portrait" />

    <TextView
        android:id="@+id/txtName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dip"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@id/imgPortrait"
        android:textColor="@android:color/black"
        android:textSize="16dip"
        android:gravity="center"
        android:text="" />

    <Button
        android:id="@+id/btnDel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="10dip"

        android:text="刪除"
        android:textSize="12dip"
        android:focusable="false"
        android:focusableInTouchMode="false" />

        // 注意:當設定  android:focusable="false" 和 android:focusableInTouchMode="false" 時,可避免和ListView的Item點擊事件衝突,導致item的click事件無效。

</RelativeLayout>

自訂資料配接器

public class AppAdapter extends BaseAdapter implements   View.OnClickListener {

    private Context context;
    private List<App> appList;
    private final String inflater = Context.LAYOUT_INFLATER_SERVICE;
    private LayoutInflater layoutInflater;
    private Handler handler;

    private AsyncImageLoader  imageLoader;  // 非同步載入圖片的類 

     // 視圖容器類,屬性對應布局檔案元素

     private class ViewHolder {
          ImageView imgIcon;
          TextView txtName;
          Button btnDel;
     }

 

    // 建構函式

    public AppAdapter (Context c, List<App> list) {
         if (null != list) {
            appList= list;
         } else {
            appList= new ArrayList<App>();
         }
         this.context = c;
         layoutInflater = (LayoutInflater) context.getSystemService(inflater);

         handler = new Handler();

         imageLoader = new AsyncImageLoader();
    }

    // 添加單個項(自訂方法)

    public void addItem(App  item) {
         if (item != null) {
              appList.add(item);
              notifyDataSetChanged();   // 通知適配器資料已改變
         }
    }

    // 添加多個項(自訂方法)

    public void addItem(List<App> list) {
         if (null != list && list.size() > 0) {
              for (int i = 0; i < list.size(); i++) {
                   appList.add(list.get(i));
              }
              notifyDataSetChanged();  // 通知適配器資料已改變

         }
    }

    // 刪除項(自訂方法)

    public void removeItem(int position) {
         if (appList.get(position) != null) {
              appList.remove(position);
              notifyDataSetChanged();  // 通知適配器資料已改變
         }
    }

    // 擷取總數

    public int getCount() {
        return appList.size();
    }

    // 擷取單條資料

    public App getItem(int position) {

        return appList.get(position);
    }

    // 擷取當前位置項的id(自訂方法)

    public int getItemId(int position) {

        return appList.get(position).getAppId();
    }

    // 擷取視圖

    public View getView(int position, View convertView, ViewGroup parent) {
         ViewHolder holder;
         if (null == convertView) {
              // 裝載布局檔案 app_item.xml
              convertView = (RelativeLayout) layoutInflater.inflate(R.layout.app_item, null);
              holder = new ViewHolder();

              holder.imgIcon = (ImageView) convertView.findViewById(R.id.imgIcon );
              holder.txtNick = (TextView) convertView.findViewById(R.id.txtNick );
              holder.btnDel= (Button) convertView.findViewById(R.id.btnDel);
              convertView.setTag(holder);
         } else {
              holder = (ViewHolder) convertView.getTag();
         }
         App app = appList.get(position); // 擷取當前項資料
         if (null != app) {
               holder.txtName.setText(app.getAppName());   

               holder.btnDel.setOnClickListener(this); // 添加按鈕點擊事件

               holder.btnDel.setTag(app.getAppId()); // 設定按鈕"tag"為應用程式的id,便於刪除時使用

               imageLoader.loadImage(app.getAppIcon(), holder.imgIcon); // 非同步載入圖片
         }
         return convertView;
     }

     // 實現 View.OnClickListener 介面方法

     public void onClick(View v) {
          Button btn = (Button) v;

          // 擷取當前刪除項的id
          int id= Integer.parseInt(btn.getTag().toString());

          // 調用刪除方法

          removeItem(id);

     }
 }

 

在Activity類中調用如下:

List<App> list = new List<App>(); // 擷取資料

AppAdapter adapter = new AppAdapter(this, list );

listView.setAdapter(adapter); // listView 為 ListView 對象執行個體

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.