2013年10月15日 星期二

GPS 範例

首先要先將權限打開
 <uses-permission  android:name="android.permission.INTERNET"/>
 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

在Layout 中放入4個TextView

 <TextView    
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/longitude" />
 
  <TextView    
    android:id="@+id/longitude"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
     />
 
  <TextView    
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/latitude" />
 
  <TextView    
    android:id="@+id/latitude"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
     />
    
在主程式中 繼承了Activity 並實作LocationListener 
public class MainActivity extends Activity implements LocationListener ,在LocationListener 中有幾個方法需實作。

@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
getLocation(location);
}

@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}

@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}

在onCreate 中呼叫Init 函數來判斷GPS 或網路是否被打開,如果沒被打開則應用
Settings.ACTION_LOCATION_SOURCE_SETTINGS 來把介面用intent帶到android的設定頁面。
如果GPS 或網路被打開應用
Location location=lms.getLastKnownLocation(GPS_PROVIDER); 來抓取目前位置
Double longitude=location.getLongitude();
Double latitude=location.getLatitude();
longtitudeTv.setText(String.valueOf(longitude));
latitudeTv.setText(String.valueOf(latitude));





2013年10月8日 星期二

BaseAdapter 範例



使用ListView 搭配 BaseAdapter 類別,創造出來的功能

習慣性先作介面的部分。Layout 中設計一個list.xml

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"        
    tools:context=".myList" >

<ListView
   android:id="@android:id/list"    
   android:layout_width="fill_parent"
   android:layout_height="fill_parent" >
   
</ListView>
    
</LinearLayout>

adapter.xml 中設計了一個ImageView,一個TextView與一個CheckBox

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="fill_parent"
>
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10px"
/>
   
<TextView 
android:id="@+id/tv" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content"
android:layout_marginLeft="15px"
android:layout_toRightOf="@id/iv"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceLarge"
android:minHeight="?android:attr/listPreferredItemHeight"
  />
 
  <CheckBox android:id="@+id/cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content" 
android:layout_marginRight="15px"
android:layout_alignParentRight="true"
android:minHeight="?android:attr/listPreferredItemHeight"
android:focusable="false"
android:clickable="false"
/>
</RelativeLayout>


在主程式listView中繼承一個ListActivity,在onCreate事件中,先使用
getResources().getStringArray(R.array.books)來讀取string.xml中的books陣列。使用setListAdapter函數來為listView  提供資料。MyAdapter為一個繼承BaseAdapter的自訂類別。



protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
CharSequence[] list=getResources().getStringArray(R.array.books);
setListAdapter(new MyAdapter(this,list));
}

MyAdapter為一個繼承BaseAdapter的自訂類別。 可自訂一個建構子 MyAdapter(Context ctxt,CharSequence[] list),
並需實作getCount()、getItem(int position)、getItemId(int position)與 
getView(int position, View convertView, ViewGroup parent)。


其中 MyAdapter可透過LayoutInflater 取得一個View

public MyAdapter(Context ctxt,CharSequence[] list)
{
// Obtains the LayoutInflater from the given context.
// myInflater=LayoutInflater.from(ctxt);
myInflater=LayoutInflater)ctxt.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.list=list;

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

public Object getItem(int position) {return list[position];}


public long getItemId(int position) {return position;}

getView 可透過 ViewTag 取得與adapter.xml的連結

public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub

ViewTag viewTag;
if(convertView==null)
{
//Inflate a new view hierarchy from the specified xml resource.
//Throws InflateException if there is an error.
convertView=myInflater.inflate(R.layout.adapter,null);
viewTag = new ViewTag(
(ImageView)convertView.findViewById(R.id.iv),
(TextView) convertView.findViewById(R.id.tv),
(CheckBox) convertView.findViewById(R.id.cb)
);
convertView.setTag(viewTag);
}
else{
viewTag = (ViewTag) convertView.getTag();
}
switch(position)
{
case Jobs: 
viewTag.iv.setBackgroundResource(R.drawable.jobs);
break;
case Swan: viewTag.iv.setBackgroundResource(R.drawable.blackswan);
break;
case Thinking: viewTag.iv.setBackgroundResource(R.drawable.thinking);
break;
}

viewTag.tv.setText(list[position]);
return convertView;

}


public class ViewTag {
ImageView iv;
TextView tv;
CheckBox cb;
public ViewTag(ImageView iv, TextView tv, CheckBox cb) {
// TODO Auto-generated constructor stub
this.iv=iv;
this.tv=tv;
this.cb=cb;

}


}
程式下載


SimpleAdapter 範例



使用ListView 與SimpleAdapter 來完成。

Layout 中使用一個<ImageView> 、 一個<TextView > 與一個<CheckBox>。

在程式中繼承ListActivity,
宣告一個字串陣列 private String[] books={"賈伯斯","黑天鵝","快思慢想"}; 與一個整數陣列
private int[] img={R.drawable.jobs,R.drawable.blackswan,R.drawable.thinking}; 其中jobs、blackswan 與thinking 為圖檔,置放在drawable* 的資料夾中,一個ListView 的變數  lview。
使用 getListView 來得到目前的listview。
宣告一個字串陣列 對應到ListView 所對應的陳列的<ImageView>、<TextView >、<CheckBox>。
一個整數陣列對應<ImageView>、<TextView >、<CheckBox>的id
private String[] from={"ivv","tvV","cb"};
private int[] to={R.id.iv,R.id.tv,R.id.cb};

宣告一個LinkedList 變數data,並以for 迴圈給值
  LinkedList<HashMap<String, Object>> data=new LinkedList<HashMap<String,Object>>();

for(int i=0;i<books.length;i++)
{
HashMap<String, Object> temp=new HashMap<String, Object>();
temp.put(from[0], img[i]);
temp.put(from[1], books[i]);
data.add(temp);
}
取得一個SimpleAdapter 的物件
adapter=new SimpleAdapter(this, data, R.layout.adapter, from, to);
lview.setAdapter(adapter);