2013年2月28日 星期四

Android點選ListView中的Item並呈現選取狀態

執行結果:
















參考資料:
ListView之一:Adapter介紹與使用 (請先參考此篇並進行實作)
[Android]ListView item被選擇時可以變換背景(在此篇有提到在觸碰模式下(Touch Mode)下是沒有selection state)

程式碼:
...........省略,請先參考參考資料中的資料...........
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener(){
        View view2; //保存點選的View
        int select_item=-1; //一開始未選擇任何一個item所以為-1
        public void onItemClick(AdapterView<?> parent, View view,int position, long id){
                                         
                //======================
                //點選某個item並呈現被選取的狀態
                if ((select_item == -1) || (select_item==position)){
                        view.setBackgroundColor(Color.YELLOW); //View加上選取效果
                }else{
                        view2.setBackgroundDrawable(null); //將上一次點選的View保存在view2
                        view.setBackgroundColor(Color.YELLOW); //View加上選取效果
                }
                view2=view; //保存點選的View
                select_item=position;//保存目前的View位置
                //======================
        }
                       
}
);
...........省略,請先參考參考資料中的資料...........


2013年2月1日 星期五

Java 實作Vector動態陣列(插入、移除)

  本篇用意是想在某些情況下,隨時將資料暫時儲存在陣列中,方便以後做處理用,這時不能用固定的陣列來儲存資料,因為資料隨時可能會儲存到陣列中,這時必須使用動態陣列來儲存資料。
PS.有興趣者自行將程式碼複製下來實際run過一次

前置作業:
新建 test_vector.java 檔

程式碼如下:
test_vector.java
import java.util.*;
import java.io.*;
public class test_vector {
public static Vector Arr_v=new Vector();
    public test_vector() {
    }
    public static void main(String[] args){
    try {
    System.out.println("實作Vector動態陣列(插入、移除)...");
    BufferedReader keyin;
   keyin=new BufferedReader(new InputStreamReader(System.in));
   boolean BStop=true;
   System.out.println("指令:close(關閉結束);length(印出陣列長度);out data(輸出陣列資料);remove(移除);\nPS.指令輸入英文即可");
 
   while(BStop){
    System.out.print("請輸入資料或輸入指令:");
       String str_data=keyin.readLine();
if (str_data.equals("length")){
System.out.println("Arr_v長度:"+Arr_v.size());
}else if (str_data.equals("close")){
BStop=false;
System.out.println("End...");
}else if (str_data.equals("out data")){
System.out.println("================");
System.out.println("輸出資料...");
for (int y=0;y<Arr_v.size();y++){
//((Vector)Arr_v.elementAt(y)).elementAt(1).toString() //印出(輸出)二維陣列的資料
//印出(輸出)一維陣列的資料
System.out.println("Arr_v["+y+"]="+Arr_v.elementAt(y).toString());
}
System.out.println("================");
}else if (str_data.equals("remove")){
synchronized(Arr_v){
try{
System.out.print("請輸入要移除的陣列(從0開始):");
//Integer.parseInt 將輸入的值轉換為int(整數),並丟入x_index變數中
int x_index= Integer.parseInt(keyin.readLine());
//remove 移除所指定的陣列,並將移除的值強制轉換為String(字串),
String str_temp=(String)Arr_v.remove(x_index);
System.out.println("移除Arr_v["+x_index+"]="+str_temp);
}catch(Exception e) {
System.out.println(e.toString());
}
}
}else{
synchronized(Arr_v){
Arr_v.add(Arr_v.size(),str_data); //將資料放入到Vector中
}
}
   }
    }catch(IOException e){
    System.out.println("Error IOException:"+e.toString());
    }catch(Exception e) {
    System.out.println("Error Exception:"+e.toString());
    }
    }
}

輸出結果如下: