總網頁瀏覽量

關於我自己

我的相片
人生的必修課是接受無常,人生的選修課是放下執著。

2012年1月8日 星期日

How to use GridView








一列3欄
點選某筆資料, 顯示於TextView

GridViewActivity.java
package com.tstos.GridView;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.TextView;

public class GridViewActivity extends Activity 
{
 private TextView textview1;
 private GridView gridview1;
 private String[] android_version;
 private ArrayAdapter adapter;
 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      
      textview1 = (TextView)findViewById(R.id.TextView1);
      gridview1 = (GridView)findViewById(R.id.GridView1);
      //載入default陣列
      android_version = getResources().getStringArray(R.array.griview_array);
      //設定gridview的欄位數為3                                          
      gridview1.setNumColumns(3);
      adapter = new ArrayAdapter(GridViewActivity.this, R.layout.simple_list_item_1_small, android_version);
      gridview1.setAdapter(adapter);   
      
      //定義GridView的點擊行為
      gridview1.setOnItemClickListener(new GridView.OnItemClickListener()
      {
        public void onItemClick(AdapterView parent, View v, int position, long arg3)
        {
         textview1.setText(android_version[position]);
        }
      });
    }
}//

在/res/layout/自定simple_list_item_1_small.xml
simple_list_item_1_small.xml

沒有留言:

張貼留言