android 自定义adapter什么时候用
2016-04-01 · 知道合伙人数码行家
知道合伙人数码行家
向TA提问 私信TA
Adapter是用来帮助填充数据的中间桥梁,比如通过它将数据填充到ListView, GridView, Gallery等,而android的adapter又有很多种(ArrayAdapter, BaseAdapter, CursorAdapter, HeaderViewListAdapter, ListAdapter, ResourceCursorAdapter, SimpleAdapter, SimpleCursorAdapter, SpinnerAdapter, WrapperListAdap
1用BaseAdapter实现如下效果:
Item 的activity_main.xml文件:
[java] view plain copy
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ImageView
android:id="@+id/im_greens"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/safe"
android:layout_marginTop="3px"
android:text="@string/hello_world" />
<TextView
android:layout_below="@id/im_greens"
android:id="@+id/tv_greens"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="剁椒鱼头"
android:textSize="16sp"
/>
<TextView
android:id="@+id/tv_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/im_greens"
android:layout_marginLeft="35px"
android:layout_marginTop="30px"
android:text="价格:"
android:textSize="22sp"
/>
<TextView
android:id="@+id/tv_price_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30px"
android:layout_toRightOf="@id/tv_price"
android:layout_marginLeft="55px"
android:textColor="#E00703"
android:text="58元"
android:textSize="22sp"
/>
</RelativeLayout>
greens_listview.xml文件:
[java] view plain copy
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dip"
android:gravity="center_vertical|center_horizontal"
android:background="#619854"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#E8F2FE"
android:text="菜谱"
/>
</LinearLayout>
<ListView
android:id="@+id/listview_greens"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:horizontalSpacing="10dip"
android:verticalSpacing="10dip"
></ListView>
</LinearLayout>
Activity代码:
[java] view plain copy
package com.example.adapter;
import android.app.Activity;
import android.os.Bundle;
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 {
private ListView lsitview_greens;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.greens_listview);
MyApater adapter = new MyApater();
lsitview_greens = (ListView) findViewById(R.id.listview_greens);
lsitview_greens.setAdapter(adapter);
}
//自定义Adapter
class MyApater extends BaseAdapter{
//用于显示菜品的图片
int[] images = {R.drawable.safe,R.drawable.safe,R.drawable.safe,R.drawable.safe,R.drawable.safe,R.drawable.safe,R.drawable.safe,R.drawable.safe,R.drawable.safe};
//用于显示菜名
String[] name = new String[]{
"宫保鸡丁","糖醋排骨","麻婆豆腐","红烧鱼","回锅肉","辣椒炒肉","醋溜土豆丝","大蒜腊牛肉","红烧排骨"
};
//用于显示价格
String[] price = new String[]{
"32¥","22¥","15¥","94¥","45¥","15¥","20¥","45¥","85¥"
};
@Override
public int getCount() {
// TODO Auto-generated method stub
return name.length;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
//根据布局文件获取View返回值
convertView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.activity_main, null);
}
ImageView imageview = (ImageView) convertView.findViewById(R.id.im_greens);
TextView greens_name = (TextView) convertView.findViewById(R.id.tv_greens);
TextView greens_price = (TextView) convertView.findViewById(R.id.tv_price_2);
imageview.setImageResource(images[position]);
greens_name.setText(name[position]);
greens_price.setText(price[position]);
return convertView;
}
}
}