此篇為進階篇,可先看前一篇條列式選單後再看這篇。

http://style77125tech.pixnet.net/blog/post/12140625

java檔

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View.OnClickListener;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class TextExam003 extends ListActivity {
/** Called when the activity is first created. */
//設定選單要顯示的內容
String[] showData = {"第一個選項", "第二個選項", "第三個選項", "第四個選項", "第五個選項", "第六個選項", "第七個選項"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new MyListAdapter(this));
}
class MyListAdapter extends BaseAdapter{

/* LayoutInflater的作用類似於 findViewById()
(1)LayoutInflater是用來找layout下xml布局文件,並且實例化。
(2)findViewById()是找具體xml下的具體 widget物件(如:Button)。 */

LayoutInflater myInflater;
public MyListAdapter(Context c){
myInflater = LayoutInflater.from(c);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
//顯示要回傳的資料數量
return showData.length;
}

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final int index = position;
TextView dataInfo;
if(convertView == null){
convertView = myInflater.inflate(R.layout.main, null);
dataInfo = (TextView) convertView.findViewById(R.id.dataInfo);
convertView.setTag(dataInfo);
}else{
dataInfo = (TextView)convertView.getTag();
}
dataInfo.setText(showData[index]);
return convertView;
}
}
}

main.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"
>
<TextView
android:id="@+id/dataInfo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="25sp"
android:fadingEdge="vertical"
/>
</LinearLayout>

ListMenu02.jpg  

arrow
arrow
    文章標籤
    baseAdapter ListAdapter ListMenu
    全站熱搜

    S 發表在 痞客邦 留言(0) 人氣()