2012年3月12日

String to Drawable



String 轉 drawable:

e.g. 1


private void displayImage() {

String uri = "drawable/imagefile01"; 
 // int imageResource = R.drawable.icon; 
int imageResource = getResources().getIdentifier(uri, null, getPackageName()); 
ImageView imageView = (ImageView) findViewById(R.id.myImageView); 
Drawable image = getResources().getDrawable(imageResource); imageView.setImageDrawable(image);
 }

***********************************************

e.g.2 應用在 database + ListAdapter
假設圖檔放在 res/drawable/ 下,其檔名則是以字串方式存在 database 裡。可以下列方式將之顯現出來:



Cursor c = getData();
c.moveToFirst();

String [] imgStrings = new String [c.getCount()]; //將存放從db來的圖檔名
int [] imgResources = new int [c.getCount()];//準備將字串轉成整數格式 
Object [] imgObj = new Object [c.getCount()];//準備接收成 Drawable 的物件

for (int i = 0; i < str1.length; i++){
  imgStrings [i] = c.getString(0);//圖形檔名後接有 .png
 //將存在db的圖檔字串檔名轉成drawable
 String uri = "drawable/" + str2[i].substring(0, str2[i].length()-4).toString(); 
 // 刪掉後頭的 .png (四個位元)
 imgResources[i] = getResources().getIdentifier(uri, null, getPackageName());
 imgObj [i] = getResources().getDrawable(imgResources[i]);
c.moveToNext();
}
c.close(); 
/CustomAdapter. 將得到的drawable 物件陣列,傳入自己設定的adapter 
ArrayAdapter<String> adapter = new CustomAdapter(this, imgObj); 
setListAdapter(adapter);


&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&


CustomAdapter.java 自己設定的 Adapter:

public class CustomAdapter extends ArrayAdapter<String>{

 private LayoutInflater inflater;
 private final Context context;

private final Object [] img; //來接收傳進來的drawable 物件陣列

public EventAdapter(Context context, Object [] imgObj) { //Constructor
 super(context, R.layout.eventlayout, event);
 this.context = context; 
 this.img = imgObj;
}
@Override

public View getView(int position, View convertView, ViewGroup parent) {

   ViewHolder holder = new ViewHolder(); 
   inflater = LayoutInflater.from(context); 
   View rowview = inflater.inflate(R.layout.eventlayout, parent, false);
   holder.imageview = (ImageView)rowview.findViewById(R.id.ImageView01); 
   Object drawable = img[position]; //將Object 型態轉為 Drawable
   holder.imageview.setImageDrawable((Drawable) drawable);
   return rowview;
   } 

    static class ViewHolder
    ImageView imageview;
    }

}

去字串的字元

如果刪除字串的某些字元:



設定某一字串:
String aStr = love;

str = aStr.substring(0, str.length()-0).toString(); // ==> love
str = aStr.substring(0, str.length()-1).toString(); // ==> lov
str = aStr.substring(0, str.length()-2).toString(); // ==> lo
str = aStr.substring(0, str.length()-3).toString(); // ==> l

OR   
str = aStr.substring(0, 1).toString();//l
str = aStr.substring(0, 2).toString();//lo
str = aStr.substring(0, 3).toString();//lov
str = aStr.substring(0, 4).toString();//love






只要取 "crazy" (紅色的部分─12個字元不要):
String aStr = "irrevocably-crazy";

str = aStr.substring(12, aStr.length());   第一個參數是從第幾個字元開始;第二個參數是到哪個字元結束


如果
aStr = "love." 為了要去掉最後的句點  str = aStr.substring (0, aStr.length()-1).toString();

2012年2月28日

android-- change display color (A)

靜態改變顯示顏色的方法:

           




  1、在 res/values 的目錄下,再建立一個 color.xml 檔,編輯會用到的顏色如下:
                     
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="red">#FF0000</color>
<color name="cyan">#00FFFF</color>
<color name="blue">#0000FF</color>
<color name="darkBlue">#0000A0</color>
<color name="lightBlue">#ADD8E6</color>
<color name="purple">#800080</color>
<color name="yellow">#FFFF00</color>
<color name="lime">#00FF00</color>
<color name="fuchsia">#FF00FF</color>
<color name="white">#FFFFFF</color>
<color name="silver">#C0C0C0</color>
<color name="gray">#808080</color>
<color name="black">#000000</color>
<color name="orange">#FFA500</color>
<color name="brown">#A52A2A</color>
<color name="maroon">#800000</color>
<color name="green">#008000</color>
<color name="olive">#808000</color>

</resources>


   2、在 main.xml 檔裡,加上:

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


                 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:orientation="vertical"
                    android:background="@color/purple">  <!-- 改變背景色 -->

                       <TextView
                         android:layout_width="fill_parent"
                         android:layout_height="wrap_content"
                         android:text="我是黃色  " 
                         android:textColor="@color/yellow"  <!-- 改變文字欄位顏色 -->
                         />

                       <TextView
                         android:layout_width="fill_parent"
                         android:layout_height="wrap_content"
                         android:text="我是亮綠色  " 
                         android:textColor="@color/lime" <!-- 改變文字欄位顏色 -->
                         />

                 </LinearLayout>






2012年2月27日

Customized ListView Adapter -- 自定 ListView Adapter

Customized ListView Adapter  自定 ListView Adapter:

如果能自訂自己喜歡的ListView樣式,如下圖一樣,那一定是很不錯的感覺:



要達到上圖的效果,基本上只要做三件事,便大功完成:
  1、建立自己的adapter(MyCustomListViewAdapter.java)來連接(2)的xml;
  2、建立自己喜歡的單欄樣版(rowlayout.xml):
  3、在主程式上(Cq_MyListViewExampleActivity),將 main.xml 裡的 listview1 連接上自己製作的 adapter.
                                                     全部需要準備的檔如下:



(一)建立自已的adapter:
MyCustomListViewAdapter.java:

package com.cq.listview.example;

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


public
 class
 MyCustomListViewAdapter extends ArrayAdapter<String>{

private final Context context;
private final CharSequence[] values1; //建立兩個參數陣列來接收主程式傳進來的兩個陣列
private final CharSequence [] values2;

   //建構子
   public MyCustomListViewAdapter(Context context, String [] strValues1, String [] strValues2) {
           super(context, android.R.layout.simple_list_item_1, values1);
          // TODO Auto-generated constructor stub
             this.context = context;
             this.values1 = strValues1;
             this.values2 = strValues2;
   }

   @Override
   public View getView(int position, View convertView, ViewGroup parent) {
      // TODO Auto-generated method stub
          //取得.xml layout檔的資源,用來放入自設的欄位樣式( rowview)
          LayoutInflater inflater =
              (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
             
          View rowview = inflater.inflate(R.layout.exampleLayout, parent, false);

          TextView textview = 
                 (TextView)rowview.findViewById(R.id.textview01);  //連接上自設的樣式元素
          ImageView subtopicImageView = 
                (ImageView)rowview.findViewById(R.id.imageview01);

          String s1 = values1[position].toString(); //依次傳入值
          String s2 = strValues2[position].toString();

           //交錯圖檔
          if ((position + 2)%2 == 0){
                   imageview01.setImageResource(R.drawable.basketball);
                   }else if ((position + 2)%2 == 1){
                   imageview01.setImageResource(R.drawable.americanfootball);
                   }
         
           //將傳入的值顯示在textview上
           textview.setText(s1 + "  " + s2);

           return rowview; //傳出單一整個欄位的設置
           }

}

*********************************************
(二)、建立自己喜歡的單欄樣版:

照自己喜歡的方式,準備自定的樣式layout給自定的 listview 用 (如下: rowlayout.xml)。
請準備兩個圖檔(.png),例如basketball.png, tennisball.png
rowlayout.xml:



<?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="horizontal" >

    <ImageView
        android:id="@+id/imageview01"
        android:layout_width="30px"
        android:layout_height="30px"
        android:layout_marginLeft="4px"
        android:layout_marginRight="10px"
        android:layout_marginTop="4px"
        android:src="@drawable/americanfootball" >
    </ImageView>

    <TextView
        android:id="@+id/textview01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+id/label"
        android:textColor="@color/orange"
        android:textSize="20px" >
    </TextView>

</LinearLayout>


*********************************************************************

(三)、在主程式上,將 main.xml 裡的 listview 連接上自己製作的 adapter.
MainActivity.java (Cq_MyListViewExampleActivity)有兩個自定的String 陣列
(String [] firstStringArray, String [] secondStringArray)的參數,
將用來傳入以下的自設ListView Adapter:



package com.cq.listview.example;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;

public class Cq_MyListViewExampleActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //建立兩個字串陣列
        String [] firstStringArray = {"蘋果", "香吉士", "奇異果", "西洋梨", "西瓜", "哈密瓜"
                                                   "葡萄", "杏子", "琵琶", "甘蔗"};
        String [] secondStringArray = {"apples", "oranges", "kiwi", "pears", "watermelon"
                                                      "cantaloupe", "grapes", "apricots", "loquats", "sugar cane" };
        
        //設ListView參數用以連結.xml的listview
        ListView listview = (ListView)findViewById(R.id.listView1);

        //自設的接口
        MyCustomListViewAdapter adapter = new MyCustomListViewAdapter(this
                             firstStringArray, secondStringArray);

        //將接口連接到ListView的參數
        listview.setAdapter(adapter);
    }
}


***********************************************************************
  附上 main.xml 檔的內容:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="ListView Example 3" />

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>



蘋果快速鍵


蘋果快速鍵

⌘ + Shift + 3
將整個螢幕照下來。

⌘ + Shift + 4
選擇要照的部分照下來。

⌘ + Shift + 4 + SPACE
以相機選取其中一部份,並可以照下來。

2012年2月26日

sqlite--addon

sqlite 為輕量級的資料庫。可藉由firefox的addon新增sqlite管理程式:
https://addons.mozilla.org/en-US/firefox/addon/sqlite-manager/

安裝在Firefox後,打開 Firefox --> 網頁開發者 --> SQLITE MANAGER
便可以開始建立、新增、管理您的資料庫。


2012年2月21日

android: find string length


/*找字串的長度;找出字串的字元數目*/

String aStr = "love";  //設一字串為"love"

int noOfCharacters = aStr.length()// 用length()的方法找出字元數目,然後指向整數變數(四個字元)