listview(1、BaseAdapter)

listview是APP开发中最常用的UI之一,在android中采用适配器的设计模式,关联UI界面及数据。例子如下:

1、定义listview中每个list的界面,创建item_list.xml:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="horizontal" android:layout_width="fill_parent"

    android:layout_height="fill_parent">

 

 

    <ImageView android:id="@+id/image" 

        android:layout_width="wrap_content"

        android:layout_height="wrap_content" 

        android:layout_margin="5px"/>

 

    <LinearLayout android:orientation="vertical"

        android:layout_width="wrap_content" 

        android:layout_height="wrap_content">

 

        <TextView android:id="@+id/title" 

            android:layout_width="wrap_content"

            android:layout_height="wrap_content" 

            android:textColor="#FFFFFFFF"

            android:textSize="22px" />

        <TextView android:id="@+id/content" 

            android:layout_width="wrap_content"

            android:layout_height="wrap_content" 

            android:textColor="#FFFFFFFF"

            android:textSize="13px" />

 

    </LinearLayout>

</LinearLayout>

2. 定义适配器,继承BaseAdapter

public class Adapter1 extends BaseAdapter {



    private LayoutInflater mInflater = null;

    

    public Adapter1(Context context) {

        super(); 

        mInflater = (LayoutInflater) context  

                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);  

        

    }



    @Override

    public int getCount() {

        return 100;

    }



    @Override

    public Object getItem(int position) {

        // 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) {

        ViewHolder holder = null;    

        if (convertView == null) {

            Log.i("tag", "convertView == null");

            holder = new ViewHolder();    

            convertView = mInflater.inflate(R.layout.item_list, null);  

            holder.image   = (ImageView) convertView.findViewById(R.id.image);    

            holder.title   = (TextView) convertView.findViewById(R.id.title);    

            holder.content = (TextView) convertView.findViewById(R.id.content);     

            

            //捆绑已经创建的对象

            convertView.setTag(holder);    

        } else {

            Log.i("tag", "convertView != null");

            holder = (ViewHolder) convertView.getTag();    

        }  

  

        holder.image.setImageResource(R.drawable.ic_launcher);  

        holder.title.setText("title");  

        holder.content.setText("content" + position); 

        return convertView;

    }

}

3. 在activity中定义此适配器,本例中采用listActivity

Adapter1 adapter = new Adapter1(this.getApplicationContext());

setListAdapter(adapter);

 

你可能感兴趣的:(BaseAdapter)