ListView 使用自訂 Adapter

1 篇文章 / 0 new
author
ListView 使用自訂 Adapter

使用自訂 Adapter 基本上需要實作的項目如下

public class ExtendWordShowAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<HashMap<String, Object>> dt;
    private LayoutInflater layoutInflater;

    public ExtendWordShowAdapter(Context context, ArrayList<HashMap<String, Object>> dt) {
        this.context = context;
        this.dt = dt; //儲存要顯示的資訊
        layoutInflater = LayoutInflater.from(this.context);
    }

    @Override
    public int getCount() { return dt.size(); }

    @Override
    public Object getItem(int position) { return dt.get(position); }

    @Override
    public long getItemId(int position) { return position; } //若有指定的 ID 資訊,則傳回所要的 ID

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.extendword_row, null); //載入 Layout
        }
        //取出元件
        TextView sName = (TextView) convertView.findViewById(R.id.ewrWordName);
        TextView sData = (TextView) convertView.findViewById(R.id.ewrPhoneData);
        //填入資料
        sName.setText(dt.get(position).get("Name") + "");
        sData.setText(dt.get(position).get(("Data") + "");

        return convertView;
    }
}

Free Web Hosting