Baseadapter的使用

1.明白baseadapter----数据适配器,最通用的额额适配器

2.data   适配器  listview/

3.listview 的缓存机制(需要才显示,显示完则被回收)

4.四个方法----getcount()//数据集中数据的个数,getItem(Int position)-------数据集中指定与索引项对应的数据项,

getItemId()----获取指定行的id,    getView()------获取并显示内容。

example-------xml

<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.ex2.MainActivity" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" >

    </ListView>

</RelativeLayout>

example-------xml--item

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

    <ImageView
        android:id="@+id/iv_image"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/iv_title"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:layout_toEndOf="@+id/iv_image"
        android:gravity="center"
        android:text="iv_title"
        android:textSize="25sp" />
    
        <TextView
            android:id="@+id/iv_content"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:layout_below="@+id/iv_title"
            android:layout_toEndOf="@+id/iv_image"
            android:gravity="center_vertical"
            android:text="iv_content"
            android:textSize="20sp" />

</RelativeLayout>

Itembean

package com.example.ex2;

public class ItemBean {
    public int ImageResid;
    public String  ItemTitle;
    public String  ItemContent;
    public ItemBean(int imageResid, String itemTitle, String itemContent) {
        //super();
        ImageResid = imageResid;
        ItemTitle = itemTitle;
        ItemContent = itemContent;
    }
    

}
package com.example.ex2;

import java.util.ArrayList;
import java.util.List;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.TextView;


public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        List<ItemBean> itemBeanlist =new ArrayList<>();
        for(int i=0;i<20;i++){
            itemBeanlist.add(new ItemBean(
                    
                    R.drawable.ic_launcher,
                    "title"+i,
                    "comtent"+i
                    
                    ));
        }
        
        ListView  listView=(ListView)findViewById(R.id.listView1);
        //方法不能卸载外面哦
        listView.setAdapter(new Myadapter(this,itemBeanlist));
    }
package com.example.ex2;

import java.util.List;

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.TextView;

public class Myadapter extends BaseAdapter{
    
    private List<ItemBean> mList;
    private LayoutInflater mInflater;

    public Myadapter(Context context,List<ItemBean>list) {
        mList=list;
        //将当前的Adapter的界面对象mInflater布局装载器对象
        mInflater=LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return mList.get( position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2) {
        // TODO Auto-generated method stub
        View view=mInflater.inflate(R.layout.item, null);
        ImageView imageView=(ImageView) view.findViewById(R.id.iv_image);
        TextView  title=(TextView) view.findViewById(R.id.iv_title);
        TextView  content=(TextView) view.findViewById(R.id.iv_content);
        ItemBean bean=mList.get(arg0);
        imageView.setImageResource(bean.ImageResid);
        title.setText(bean.ImageResid);
        content.setText(bean.ImageResid);
        
        return view;
    }

}


你可能感兴趣的:(Baseadapter的使用)