總網頁瀏覽量

關於我自己

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

2012年2月9日 星期四

自訂Adapter ~ 多欄位










ListViewActivity.java
package com.tstos.ListView_Adapter;

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

public class ListViewActivity extends Activity 
{
 TextView textview1;
 TextView textview2;
 TextView listview_title_column1;
 TextView listview_title_column2;
 TextView listview_title_column3;
 GridView gridview1;
 ListView listview1;
 ImageView imageview1;
 String[] android_version;
 String[] android_level;
 String[] android_code;
 Adapter_ListViewActivity 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);
      textview2 = (TextView)findViewById(R.id.TextView2);
      listview_title_column1 = (TextView)findViewById(R.id.ListView_title_column1);
      listview_title_column2 = (TextView)findViewById(R.id.ListView_title_column2);
      listview_title_column3 = (TextView)findViewById(R.id.ListView_title_column3);
      listview1 = (ListView)findViewById(R.id.ListView1);
      imageview1 = (ImageView) findViewById(R.id.ImageView1);
      
      //載入default陣列
      android_version = getResources().getStringArray(R.array.lstview_array_column1);
      android_level = getResources().getStringArray(R.array.lstview_array_column2);
      android_code = getResources().getStringArray(R.array.lstview_array_column3);
      adapter = new Adapter_ListViewActivity(ListViewActivity.this, 
                 R.layout.simple_list_item_1_small, 
                 android_version, 
                 android_level, 
                 android_code);  
      
      listview1.setAdapter(adapter);
      //定義ListView的點擊行為
      listview1.setOnItemClickListener(new GridView.OnItemClickListener()
      {
        public void onItemClick(AdapterView parent, View v, int position, long arg3)
        {
         textview1.setText(android_version[position]);
         switch(position)
         {
         case 0://1.0          
          imageview1.setAlpha(0);//將圖片設為完全透明
          textview2.setText(getResources().getString(R.string.no_code_name));
          break;
         case 1://1.1
          imageview1.setAlpha(0);
          textview2.setText(getResources().getString(R.string.no_code_name));
          break;
         case 2://1.5
          imageview1.setAlpha(255);//將圖片設為不透明
          imageview1.setImageResource(R.drawable.cupcake);
          textview2.setText(getResources().getString(R.string.cupcake));
          break;
         case 3://1.6
          imageview1.setAlpha(255);
          imageview1.setImageResource(R.drawable.donut);
          textview2.setText(getResources().getString(R.string.donut));
          break;
         case 4://2.0
          imageview1.setAlpha(255);
          imageview1.setImageResource(R.drawable.eclair);
          textview2.setText(getResources().getString(R.string.eclair));
          break;
         case 5://2.0.1
          imageview1.setAlpha(255);
          imageview1.setImageResource(R.drawable.eclair);
          textview2.setText(getResources().getString(R.string.eclair));
          break;
         case 6://2.1
          imageview1.setAlpha(255);
          imageview1.setImageResource(R.drawable.eclair);
          textview2.setText(getResources().getString(R.string.eclair));
          break;
         case 7://2.2
          imageview1.setAlpha(255);
          imageview1.setImageResource(R.drawable.froyo);
          textview2.setText(getResources().getString(R.string.froyo));
          break;
         case 8://2.3
          imageview1.setAlpha(255);
          imageview1.setImageResource(R.drawable.gingerbread);
          textview2.setText(getResources().getString(R.string.gingerbread));
          break;
         case 9://2.3.3 - 2.3.4
          imageview1.setAlpha(255);
          imageview1.setImageResource(R.drawable.gingerbread);
          textview2.setText(getResources().getString(R.string.gingerbread));
          break;
         case 10://3.0
          imageview1.setAlpha(255);
          imageview1.setImageResource(R.drawable.honeycomb);
          textview2.setText(getResources().getString(R.string.honeycomb));
          break;
         case 11://3.1
          imageview1.setAlpha(255);
          imageview1.setImageResource(R.drawable.honeycomb);
          textview2.setText(getResources().getString(R.string.honeycomb));
          break;
         }
        }
      });
    }
}


這是一個繼承BaseAdapter的類別, 在此的建構子接收ListViewActivity.java傳入的
(ListViewActivity.this,R.layout.simple_list_item_1_small,android_version,android_level,android_code);

另外Adapter要載入layout, 本例建立的simple_list_item_1_small.xml是一個含有3個TextView的layout.

Adapter_ListViewActivity.java
package com.tstos.ListView_Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class Adapter_ListViewActivity extends BaseAdapter
{
 static final String TAG = "Adapter_ListViewActivity";
 private LayoutInflater mInflater;
 String[] adapter_version;
 String[] adapter_level;
 String[] adapter_code;
 TextView column1;
    TextView column2;
    TextView column3;
    
 public Adapter_ListViewActivity(Context context, 
                      int simple_list_item_single_choice, 
                      String[] android_version, 
                      String[] android_level,
                      String[] android_code) 
 {
  mInflater = LayoutInflater.from(context);
  adapter_version = android_version;
  adapter_level = android_level;
  adapter_code = android_code;
 }

 public int getCount()
 {
  return adapter_version.length;
 }

 public Object getItem(int position)
 {
  return adapter_version.length;
 }
  
 public long getItemId(int position)
 {
  return position;
 }
  
 public View getView(int position,View convertView,ViewGroup parent)
 {  
     convertView = mInflater.inflate(R.layout.simple_list_item_1_small,null);
     column1=(TextView)convertView.findViewById(R.id.column1);
     column2=(TextView)convertView.findViewById(R.id.column2); 
     column3=(TextView)convertView.findViewById(R.id.column3);
     column1.setText(adapter_version[position].toString());
     column2.setText(adapter_level[position].toString());
     column3.setText(adapter_code[position].toString());  
     return convertView;
 }
}


simple_list_item_1_small.xml

 
 
 
 

沒有留言:

張貼留言