展会信息港展会大全

重写ArrayAdapter android开发代码
来源:互联网   发布日期:2015-10-02 16:13:36   浏览:2790次  

导读:package org.yang.android.adapter.util;import org.yang.android.adapter.demo.R;import org.yang.android.adapter.model.Book;import android.content.Context;import android.view.LayoutInflater;impor......

package org.yang.android.adapter.util;

import org.yang.android.adapter.demo.R;

import org.yang.android.adapter.model.Book;

import android.content.Context;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.ArrayAdapter;

import android.widget.ImageView;

import android.widget.TextView;

public class IAdapter extends ArrayAdapter<Object> {

private int resources;

private LayoutInflater inflater;

public IAdapter(Context context, int resources, Book[] books)

{

super(context, resources, books);

this.resources = resources;

this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

public View getView(int position, View convertView, ViewGroup parent) {

convertView = inflater.inflate(resources, null);

Book bi = (Book) this.getItem(position);

if (bi == null) {

return null;

}

TextView nameView = (TextView)convertView.findViewById(R.id.bname);

nameView.setText(bi.getName());

ImageView bicon = (ImageView)convertView.findViewById(R.id.bimage);

bicon.setBackgroundDrawable(bi.getIcon());

TextView bdescView = (TextView)convertView.findViewById(R.id.bdesc);

bdescView.setText(bi.getDesc());

TextView bpress = (TextView)convertView.findViewById(R.id.bpress);

bpress.setText(bi.getPress());

TextView bprices = (TextView)convertView.findViewById(R.id.bprices);

bprices.setText(bi.getPrices());

return convertView;

}

}

package org.yang.android.adapter.demo;

import org.yang.android.adapter.model.Book;

import org.yang.android.adapter.util.IAdapter;

import android.app.Activity;

import android.content.res.AssetManager;

import android.graphics.drawable.Drawable;

import android.os.Bundle;

import android.util.Log;

import android.widget.ListView;

public class AdapterDemoActivity extends Activity {

/** Called when the activity is first created. */

ListView lv;

Book[] books = new Book[5];

AssetManager assets;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

lv = (ListView)this.findViewById(R.id.lv);

assets = this.getAssets();

try {

books[0] = new Book("linux", Drawable.createFromStream(assets.open("linux.jpg"), null), "desc", "人民", "36.6");

books[1] = new Book("linux", Drawable.createFromStream(assets.open("linux.jpg"), null), "desc", "人民", "36.6");

books[2] = new Book("linux", Drawable.createFromStream(assets.open("linux.jpg"), null), "desc", "人民", "36.6");

books[3] = new Book("linux", Drawable.createFromStream(assets.open("linux.jpg"), null), "desc", "人民", "36.6");

books[4] = new Book("linux", Drawable.createFromStream(assets.open("linux.jpg"), null), "desc", "人民", "36.6");

} catch (Exception e) {

// TODO: handle exception

Log.d("app 读到文件出错!", e.toString());

}

IAdapter adapter = new IAdapter(this, R.layout.book, books);

lv.setAdapter(adapter);

}

}

赞助本站

人工智能实验室

相关热词: 重写 ArrayAdapter

AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港