Android控件ListView簡易使用(實現BaseAdapter抽象類)

一、內部類實現BaseAdapter抽象類 java

(實際上須要的是實現ListAdapter接口BaseAdapter是系統api實現的一個簡易使用的抽象類 android

佈局文件: sql

<?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" >
    <ListView 
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        ></ListView>

</LinearLayout>

 

<?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="60dp"
    android:gravity="center_vertical"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/tv_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:text="@string/lab_id"
        android:textColor="#ff0000"
        android:textSize="18sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/tv_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/lab_name"
            android:textColor="#000000"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/tv_phone"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/lab_tel"
            android:textColor="#88000000"
            android:textSize="16sp" />
    </LinearLayout>

</LinearLayout>

二、Activity api

package com.pas.sqlite;

import java.util.List;

import com.pas.db.dao.PersonDao;
import com.pas.domain.Person;
import com.pas.sqllitehelper.PersonSQLiteOpenHelper;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity
{
	PersonDao dao;
	List<Person> persons;
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.listview);
		PersonSQLiteOpenHelper helper=new PersonSQLiteOpenHelper(this);
		helper.getReadableDatabase();
		dao=new PersonDao(this);
		persons=dao.findAll();
		dao.close();
		/*
		 * 
		 * ListView方式
		 */
		ListView lv=(ListView)findViewById(R.id.lv);
		lv.setAdapter(new DefaultAdapter());
	}

	//默認實現類
	private class DefaultAdapter extends BaseAdapter
	{

		@Override
		public int getCount()
		{
			//條目個數==集合size
			return persons.size();
		}

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

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

		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{			
			/*
			 * 用xml文件
			 */
			View view=View.inflate(getApplicationContext(), R.layout.listitem, null);
			Person person=persons.get(position);
			//在view對象中獲取子view
			TextView tvid=(TextView)view.findViewById(R.id.tv_id);
			TextView tvname=(TextView)view.findViewById(R.id.tv_name);
			TextView tvphone=(TextView)view.findViewById(R.id.tv_phone);
			tvid.setText("ID:"+person.getId());
			tvname.setText("姓名:"+person.getName());
			tvphone.setText("電話:"+person.getNumber());
			return view;
		}
		
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	protected void onDestroy()
	{
		super.onDestroy();
		dao.close();
	}
}
相關文章
相關標籤/搜索