2014-02-13 103 views
0

我需要創建一個可以管理備忘錄的程序。基於組件的Android

  1. 創建「MyMemoInfoList」程序。

    • 通過MyMeMoEditor程序以列表形式打印內容。 (備忘錄,標題,日期)
    • 當您在選擇某個項目後點擊編輯按鈕時,它應該在MyMemoEditor屏幕上顯示詳細內容,編輯完內容後,更新的內容將反映在列表中。
    • 當你選擇一個項目,然後點擊刪除按鈕,它會從列表中刪除該項目。
    • 如果您按下添加按鈕,它將彈出MyMemoEditor屏幕,您可以在其中輸入內容並通過按保存按鈕進行保存。此外,它應該顯示在列表中。
  2. 創建MyMemoEditor程序。

    • 當您在列表上的主屏幕上註冊新備忘錄時,將會保存新備忘錄。
    • 當您執行編輯菜單時,它將更新原始備忘錄。 MyMemoMain/MyMemoInfoList類

      • MyMemoMain類的事件
    • 實施應在匿名內部類的形式來實現。

    • MyMemoInfoList類的事件應通過繼承偵聽器來實現。

我認爲我需要應用的方法是服務,廣播接收器,Android的UI,Android的UI事件,適配器視圖。

我很難通過不同的活動進行切換。

在此先感謝。

回答

0

第一,你MyMemoInfoList.xml看起來就像這樣:

<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:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MyMemoInfoList" 
    android:orientation="vertical" 
    > 

    <ListView 
     android:id="@+id/infolist" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     > 
    </ListView> 


</LinearLayout> 

============================ ================================================== == 接下來,你的編輯器。xml看起來像這樣(xml):

<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" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MyMemoEditor" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" > 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="MemoID" /> 

     <EditText 
      android:id="@+id/memoId" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" /> 
    </LinearLayout> 



    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" > 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Todo" /> 

     <EditText 
      android:id="@+id/title" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" /> 
    </LinearLayout> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="detail" /> 

    <EditText 
     android:id="@+id/content" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:lines="3" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="date(2014-02-XX)" /> 

    <EditText 
     android:id="@+id/date" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" /> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" > 

     <Button 
      android:id="@+id/btnSave" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="save" 
      android:layout_weight="1" 
      /> 


     <Button 
      android:id="@+id/btnQuit" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="quit" 
      android:layout_weight="1" /> 
    </LinearLayout> 

</LinearLayout> 

================================== =============================================== 現在,讓我們走上MyMemoInfoList(活動):

package com.example.mymemomain2; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.ListView; 
import android.widget.Toast; 

public class MyMemoInfoList extends Activity { 
    ArrayAdapter<MemoItem> adt = null; 
    ListView list = null; 
    MemoItem currentItem = null; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_my_memo_info_list); 

     MemoItemMgr.getInstance().open(this); 

     adt = new ArrayAdapter<MemoItem>(this, 
       android.R.layout.simple_list_item_single_choice, 
       MemoItemMgr.getInstance().list); 
     list = (ListView) findViewById(R.id.infolist); 
     list.setAdapter(adt); 
     list.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 

     list.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> listview, View textview, 
        int position, long arg3) { 
       ArrayAdapter adapter = (ArrayAdapter) listview.getAdapter(); 

       currentItem = (MemoItem) adapter.getItem(position); 

      } 

     }); 
    } 

    protected void onResume() { 
     super.onResume(); 
     adt.notifyDataSetChanged(); 
    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
     MemoItemMgr.getInstance().save(this); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     menu.add(0, #, 0, "add"); 
     menu.add(0, #, 0, "delete"); 
     menu.add(0, #, 0, "edit"); 
     menu.add(0, #, 0, "save"); 
     menu.add(0, #, 0, "send"); 
//# = consecutive integers starting from 1 
     return super.onCreateOptionsMenu(menu); 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     if (item.getItemId() == #) { 
      add(); 
     } else if (item.getItemId() == #) { 
      delete(); 
     } else if (item.getItemId() == #) { 
      goModify(); 
     } else if (item.getItemId() == #) { 
      MemoItemMgr.getInstance().save(this); 
     } else if (item.getItemId() == #) { 
      MemoClient client = new MemoClient(MemoItemMgr.getInstance() 
        .getList()); 
      client.start(); 
     } 

     return super.onOptionsItemSelected(item); 
    } 

    public void goHome(View v) { 
     finish(); 

    } 

    public void add() { 
     Intent i = new Intent(MyMemoInfoList.this, MyMemoEditor.class); 
     i.putExtra("mode", 0); 
     startActivity(i); 
    } 

    public void delete() { 
     int position = list.getCheckedItemPosition(); 
     if (position != -1) { 
      MemoItem item = adt.getItem(position); 
      adt.remove(item); 
     } else { 
      Toast.makeText(this, "Please make a selection to delete", Toast.LENGTH_SHORT).show(); 
     } 

     // if (currentItem != null) { 
     // adt.remove(currentItem); 
     // }else { 
     // Toast.makeText(this, "Please make a selection to delete", Toast.LENGTH_SHORT).show(); 
     // } 
    } 

    public void goModify() { 
     if (currentItem != null) { 

      Intent intent = new Intent(MyMemoInfoList.this, MyMemoEditor.class); 

      intent.putExtra("mode", 1); 
      intent.putExtra("id", currentItem.getMemoId()); 
      startActivity(intent); 
      // finish(); 
     } else { 
      Toast.makeText(this, "Please make a selection to edit", Toast.LENGTH_SHORT).show(); 
     } 
    } 
} 

=================================== ==================================== 現在我們來看看一個經理類(class):

package com.example.mymemomain2; 

import java.io.EOFException; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.io.ObjectInputStream; 
import java.io.ObjectOutputStream; 
import java.io.StreamCorruptedException; 
import java.util.ArrayList; 
import java.util.DuplicateFormatFlagsException; 
import java.util.Iterator; 

import android.content.Context; 

public class MemoItemMgr { 
    private static String fileName = "memo.data"; 
    private static MemoItemMgr instance = new MemoItemMgr(); 

    public static MemoItemMgr getInstance(){ 
     return MemoItemMgr.instance; 
    } 

    int index = 0; 
    ArrayList<MemoItem> list = new ArrayList<MemoItem>(); 
    private MemoItemMgr(){ 

    } 

    public void open(Context ctx){ 
     File file = new File(ctx.getFilesDir() , fileName); 
     list.clear(); 
     if(file.exists()){ 
      ObjectInputStream in = null; 
      try { 
       in = new ObjectInputStream(ctx.openFileInput(fileName)); 
       while(true){ 
        try{ 
        MemoItem item = (MemoItem)in.readObject(); 
        add(item); 
        }catch(EOFException e){ 
        break; 
        } catch(DuplicateException e){ 

        } 
       } 
      } catch (StreamCorruptedException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } catch (FileNotFoundException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } catch (IOException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      }catch (ClassNotFoundException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      }finally{ 
       try { 
        in.close(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 
     }else{ 
      try { 
       add(new MemoItem(1, "Conference call", "Conf. call with international subsidiary", "2014-01-15")); 
       add(new MemoItem(2, "Dev Meeting", "weekly updates", "2014-02-15")); 
       add(new MemoItem(3, "blind date", "hosting a blind date", "2014-03-15")); 
       add(new MemoItem(4, "alumni meeting", "Harvard alumni night", "2014-04-15")); 
       add(new MemoItem(5, "colleague meeting", "MS meeting", "2014-05-15")); 
       add(new MemoItem(6, "boys' night out", "Yale friends", "2014-06-15")); 
      } catch (DuplicateException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

     } 

    } 

    public void save(Context ctx){ 
     ObjectOutputStream out = null; 
     try{ 
     out = new ObjectOutputStream(ctx.openFileOutput(fileName, Context.MODE_WORLD_WRITEABLE)); 
     for(int i = 0 ; i < list.size(); i++){ 
       out.writeObject(list.get(i)); 
     } 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      }finally{ 
       try{ 
        out.close(); 
       }catch(IOException e){ 
        e.printStackTrace(); 
       } 
      } 
     } 



    public void add(MemoItem m)throws DuplicateException{ 
     if(search(m.getMemoId()) == null){ 
     list.add(m);  
     }else{ 
      throw new DuplicateException(); 
     } 

    } 

// public void add(String title, String content, String date){ 
//  MemoItem item = new MemoItem(index++, title, content,date); 
//  add(item); 
//  
// } 

    public void update(int id, String title, String content, String date){ 
     MemoItem item = search(id); 
     if(item != null){ 
      item.setTitle(title); 
      item.setContent(content); 
      item.setDate(date); 
     } 

    } 


    public MemoItem search(int id){ 
     Iterator<MemoItem> iter = list.iterator(); 
     while(iter.hasNext()){ 
      MemoItem item = iter.next(); 
      if(id == item.getMemoId()){ 
       return item; 
      } 
     } 
     return null; 

    } 

    public ArrayList<MemoItem> getList(){ 
     return list; 
    } 


} 

=== ================================================== =========================== 接下來,讓我們看看MemoItem(class) 設置你的變量並得到getters/setters/constructor/toString

package com.example.mymemomain2; 

import java.io.Serializable; 

public class MemoItem implements Serializable { 

    private int memoId; 
    private String title; 
    private String content; 
    private String date; 

    public MemoItem(){ 

    } 

    public MemoItem(int memoId, String title, String content, String date) { 
     super(); 
     this.setMemoId(memoId); 
     this.setTitle(title); 
     this.setContent(content); 
     this.setDate(date); 
    } 

    public int getMemoId() { 
     return memoId; 
    } 

    public void setMemoId(int memoId) { 
     this.memoId = memoId; 
    } 

    public String getTitle() { 
     return title; 
    } 

    public void setTitle(String title) { 
     this.title = title; 
    } 

    public String getContent() { 
     return content; 
    } 

    public void setContent(String content) { 
     this.content = content; 
    } 

    public String getDate() { 
     return date; 
    } 

    public void setDate(String date) { 
     this.date = date; 
    } 


    @Override 
    public String toString() { 
     return "ID:" + getMemoId() + "," 
       + getTitle() + ":" +getDate(); 
    } 





} 

=========================================== ======================================= 現在,讓我們進入MyMemoEditor(activity):

package com.example.mymemomain2; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

public class MyMemoEditor extends Activity { 

    int mode = 0; 
    int id = 0; 

    EditText edt1=null; 
    EditText edt2=null; 
    EditText edt3=null; 
    EditText edt4=null; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_my_memo_editor); 

     edt1 = (EditText) findViewById(R.id.memoId); 
     edt2 = (EditText) findViewById(R.id.title); 
     edt3 = (EditText) findViewById(R.id.content); 
     edt4 = (EditText) findViewById(R.id.date); 

     Intent intent = getIntent(); 
     mode = intent.getIntExtra("mode", 0); 
     if (mode == 1) { 
      id = intent.getIntExtra("id", -1); //write random number that doesn't make any sense 
      if(id!= -1){ 
       MemoItem item = MemoItemMgr.getInstance().search(id); 
       edt1.setText(item.getMemoId() + ""); 
       edt1.setEnabled(false); //this will prevent you from editing 
       edt2.setText(item.getTitle()); 
       edt3.setText(item.getContent()); 
       edt4.setText(item.getDate()); 
      } 
     } 

     Button btnSave = (Button) findViewById(R.id.btnSave); 
     Button btnQuit = (Button) findViewById(R.id.btnQuit); 

     btnQuit.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       quit(v); 
      } 
     }); 

     btnSave.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       save(v); 
      } 
     }); 

    } 

    public void save(View v) { 

     int memoId = Integer.parseInt(edt1.getText().toString()); 

     String title = edt2.getText().toString(); 

     String content = edt3.getText().toString(); 

     String date = edt4.getText().toString(); 

     if (mode == 0) { 
      try { 
       MemoItemMgr.getInstance().add(new MemoItem(memoId, title, content, date)); 
       finish(); 
      } catch (DuplicateException e) { 
       Toast.makeText(this, "Duplicative ID. Please write a different ID", Toast.LENGTH_LONG).show(); 
       edt1.setText(""); 
       edt1.requestFocus(); 
      } 
     } else if (mode == 1) { 
      MemoItemMgr.getInstance().update(id, title, content, date); 
      finish(); 
     } 



    } 

    public void quit(View v) { 
     finish(); 
    } 

} 

=========================================== ======================== 現在讓我們來看看客戶端(class):

package com.example.mymemomain2; 

import java.io.IOException; 
import java.io.ObjectOutputStream; 
import java.io.OutputStream; 
import java.net.Socket; 
import java.net.UnknownHostException; 
import java.util.ArrayList; 

public class MemoClient extends Thread { 


    ArrayList list = null; 
    public MemoClient(ArrayList list){ 
     this.list = list; 
    } 




    @Override 
    public void run() { 
     super.run(); 
     ObjectOutputStream objOut = null; 
     Socket socket = null; 
     try { 
      socket = new Socket("70.12.109.92", 7777); 
      OutputStream out = socket.getOutputStream(); 
      objOut = new ObjectOutputStream(out); 
      for(int i = 0; i < list.size(); i++){ 
       objOut.writeObject(list.get(i)); 
     } 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     }finally{ 
      try { 
       objOut.close(); 
       socket.close(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 


     } 
    } 

} 

=========================================== ========================================== 最後,新建一個項目( JAVA),使一臺服務器(類):

import java.io.EOFException; 
import java.io.InputStream; 
import java.io.ObjectInputStream; 
import java.net.ServerSocket; 
import java.net.Socket; 

import com.example.mymemomain2.MemoItem; 

public class MemoServer { 

    public static void main(String[] args) throws Exception { 
     ServerSocket server = new ServerSocket(7777); 

     while (true) { 
      System.out.println("waiting..."); 
      Socket socket = server.accept(); 
      InputStream in = socket.getInputStream(); 
      ObjectInputStream objIn = new ObjectInputStream(in); 
      while (true) { 
       try { 
        MemoItem item = (MemoItem) objIn.readObject(); 
        System.out.println(item); 
       } catch (EOFException e) { 
        break; 
       } 
      } 
     } 
    } 
} 
0
*******************************************************************************  
********************Just FYI, here is another example.************************** 
******************************************************************************* 

============================== ==========================================

拳頭,讓我們編輯xml文件。

============================================== ===========================

這裏是主要頁面的主要xml (確保您根據需要更改佈局格式給出的問題)

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".NotePadActivity" > 

    <ListView 
     android:id="@+id/listview" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     /> 

</RelativeLayout> 

====================================== ============================================== 接下來,讓我們移動到注意編輯xml文件。

<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:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".NoteEditActivity" 
    android:orientation="vertical"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="ID" /> 
    <EditText 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/edtId" 
     /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Title" /> 
    <EditText 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/edtTitle" 
     /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Content" /> 
    <EditText 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/edtContent" 
     /> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     > 
     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="Save" 
      android:id="@+id/btnSave" 
      /> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="Cancel" 
      android:id="@+id/btnCancel" 
      /> 

    </LinearLayout> 

</LinearLayout> 

=========================================== ====================================

現在,讓我們的主要活動工作(NotePadActivity )

package com.example.notepad; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.widget.Toast; 

public class NotePadActivity extends Activity { 

    public static final int MODE_ADD = 0; 
    public static final int MODE_EDIT = 1; 

    public static final int MENU_ADD = 1; 
    public static final int MENU_EDIT = 2; 
    public static final int MENU_DELETE = 3; 
    public static final int MENU_SEND = 4; 
    public static final int MENU_EXIT = 5; 

    ListView listview = null; 
    ArrayAdapter<Note> adt = null; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     NoteManager.getInstance().open(this); 

     adt = new ArrayAdapter<Note>(this, 
       android.R.layout.simple_list_item_single_choice, NoteManager 
         .getInstance().getList()); 
     listview = (ListView) findViewById(R.id.listview); 
     listview.setAdapter(adt); 
     listview.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     menu.add(0, MENU_ADD, 0, "Add"); 
     menu.add(0, MENU_EDIT, 0, "Edit"); 
     menu.add(0, MENU_DELETE, 0, "Delete"); 
     menu.add(0, MENU_SEND, 0, "Send"); 
     menu.add(0, MENU_EXIT, 0, "Exit"); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     if (item.getItemId() == MENU_ADD) { 
      add(); 
     } else if (item.getItemId() == MENU_EDIT) { 
      edit(); 
     } else if (item.getItemId() == MENU_DELETE) { 
      delete(); 
     } else if (item.getItemId() == MENU_SEND) { 
      send(); 
     } else if (item.getItemId() == MENU_EXIT) { 
      exit(); 
     } 

     return super.onOptionsItemSelected(item); 
    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
     NoteManager.getInstance().close(this); 
    } 

    private void exit() { 
     finish(); 
    } 

    private void send() { 
     NoteClient client = new NoteClient(NoteManager.getInstance().getList()); 
     client.start(); 

    } 

    private void delete() { 
     int position = listview.getCheckedItemPosition(); 
     if (position != -1) { 
      Note note = adt.getItem(position); 
      adt.remove(note); 
     } else { 
      Toast.makeText(this, "Please select the one that you want to delete", Toast.LENGTH_LONG).show(); 
     } 

    } 

    private void edit() { 
     int position = listview.getCheckedItemPosition(); 
     if (position != -1) { 
      Note note = adt.getItem(position); 

      int id = note.getId(); 

      Intent intent = new Intent(this, NoteEditActivity.class); 
      intent.putExtra("mode", MODE_EDIT); 
      intent.putExtra("id", id); 
      startActivity(intent); 
     } else { 
      Toast.makeText(this, "Please select the one that you want to edit", Toast.LENGTH_LONG).show(); 
     } 
    } 

    private void add() { 
     Intent intent = new Intent(this, NoteEditActivity.class); 
     intent.putExtra("mode", MODE_ADD); 
     startActivity(intent); 

    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     adt.notifyDataSetChanged(); 
    } 

} 

========================================== ===============================================

現在讓我們創建一個註釋(類):

package com.example.notepad; 

import java.io.Serializable; 

public class Note implements Serializable { 
    private int id; 
    private String title; 
    private String content; 



    public Note(int id, String title, String content) { 
     super(); 
     this.id = id; 
     this.title = title; 
     this.content = content; 
    } 
    public int getId() { 
     return id; 
    } 
    public void setId(int id) { 
     this.id = id; 
    } 
    public String getTitle() { 
     return title; 
    } 
    public void setTitle(String title) { 
     this.title = title; 
    } 
    public String getContent() { 
     return content; 
    } 
    public void setContent(String content) { 
     this.content = content; 
    } 
    @Override 
    public String toString() { 
     return "[" + id + "]" + title; 
    } 




} 

============================================= ===================================== 我們將創建的下一個類是NoteManager(class):

package com.example.notepad; 

import java.io.EOFException; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.ObjectInputStream; 
import java.io.ObjectOutputStream; 
import java.util.ArrayList; 

import android.content.Context; 

public class NoteManager { 
    // need to use singleton 

    private static NoteManager instance = new NoteManager(); 

    public static NoteManager getInstance(){ 
     return instance; 
    } 

    private String fileName = "note.ser"; 
    private ArrayList<Note> list = new ArrayList<Note>(); 

    private NoteManager(){ 

    } 

    public void open(Context ctx){ 
     File file = new File(ctx.getFilesDir(), fileName); 
     if(file.exists()){ 

        //use FileStream to read the file and add by using add() 
      ObjectInputStream objIn = null; 
      try { 
       FileInputStream fin = ctx.openFileInput(fileName); 
       objIn = new ObjectInputStream(fin); 
       while(true){ 
        try{ 
        Note note = (Note)objIn.readObject(); 
        add(note); 
        }catch(EOFException e){ 
         break; 
        } 
       } 
      } catch (Exception e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      }finally{ 
       try { 
        objIn.close(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 

     }else{ 
      //force add the content by using add() 
      try { 
       add(new Note(123, "Ethical quotes", "Content blah blah")); 
       add(new Note(124, "Todo", "bla she bla she")); 
       add(new Note(1, "Grocery", "hohoho")); 
       add(new Note(13, "Bus route", "hong hong hong")); 

      } catch (DuplicateException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
    } 

    public void close(Context ctx){ 
     ObjectOutputStream objOut = null; 

     try { 
      FileOutputStream fin = ctx.openFileOutput(fileName, Context.MODE_WORLD_WRITEABLE); 
      objOut = new ObjectOutputStream(fin); 

      for(int i = 0; i < list.size(); i++){ 
       Note note = list.get(i); 
       objOut.writeObject(note); 
      } 

     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     }finally{ 
      try { 
       objOut.close(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
    } 

    public void add(Note note) throws DuplicateException{ 
     //repetition check 
     if(search(note.getId()) == null){ 
     list.add(note); 
     }else{ 
      throw new DuplicateException(); 
     } 
    } 

    public Note search(int id){ 
     for (int i = 0; i < list.size(); i++) { 
      Note note = list.get(i); 
      if(id == note.getId()){ 
       return note; 
      } 
     } 
     return null; 
    } 

    public ArrayList<Note> getList(){ 
     return list; 

    } 

    public void update(int id, String title, String content){ 
     Note note = search(id); 
     note.setTitle(title); 
     note.setContent(content); 
    } 

    public boolean remove(int id){ 
     Note note = search(id); 
     return list.remove(note); 
    } 





} 

=========================================== ========================================

現在,我們做的另一項活動(NoteEditActivity):

package com.example.notepad; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

public class NoteEditActivity extends Activity { 
    int mode = 0; 

    EditText edtId; 
    EditText edtTitle; 
    EditText edtContent; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_note_edit); 

     edtId = (EditText) findViewById(R.id.edtId); 
     edtTitle = (EditText) findViewById(R.id.edtTitle); 
     edtContent = (EditText) findViewById(R.id.edtContent); 

     Intent intent = getIntent(); 
     mode = intent.getIntExtra("mode", NotePadActivity.MODE_ADD); 
     if (mode == NotePadActivity.MODE_EDIT) { 
      edtId.setEnabled(false); 
      int id = intent.getIntExtra("id", -1); 
      Note note = NoteManager.getInstance().search(id); 
      edtId.setText(id + ""); 
      edtTitle.setText(note.getTitle()); 
      edtContent.setText(note.getContent()); 
     } 

     Button btnSave = (Button) findViewById(R.id.btnSave); 
     Button btnCancel = (Button) findViewById(R.id.btnCancel); 
     btnCancel.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       finish(); 
      } 
     }); 

     btnSave.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       int id = Integer.parseInt(edtId.getText().toString()); 
       String title = edtTitle.getText().toString(); 
       String content = edtContent.getText().toString(); 

       if (mode == NotePadActivity.MODE_ADD) { 
        try { 
         NoteManager.getInstance().add(
           new Note(id, title, content)); 
         finish(); 
        } catch (DuplicateException e) { 
         Toast.makeText(NoteEditActivity.this, 
           "Duplicate ID, please use different ID", Toast.LENGTH_LONG) 
           .show(); 

        } 
       } else if (mode == NotePadActivity.MODE_EDIT) { 
        NoteManager.getInstance().update(id, title, content); 
        finish(); 
       } 

      } 
     }); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.note_edit, menu); 
     return true; 
    } 

} 

============ ================================================== ====================== 最後我們創建客戶端/服務器: 首先,讓我們創建一個服務器(新的Java項目+類的話): 也,不要忘記更改清單文件中的權限設置。

import java.io.EOFException; 
import java.io.ObjectInputStream; 
import java.net.ServerSocket; 
import java.net.Socket; 


public class NoteServer { 

    public static void main(String[] args) throws Exception { 
     ServerSocket server = new ServerSocket(8888); 
     System.out.println("waiting...."); 
     while(true){ 
      Socket socket = server.accept(); 
      ObjectInputStream in = new ObjectInputStream(socket.getInputStream()); 
      while(true){ 
       try{ 
//     Note note = in.readObject(); 
        String note = (String)in.readObject(); //use this if you don't remember. 
        System.out.println(note); 

       }catch(EOFException e){ 
        break; 
       } 
      } 
     } 

    } 

} 

=========================================== ======================================================== 最後,我們將製作我們的新客戶類別:

package com.example.notepad; 

import java.io.IOException; 
import java.io.ObjectOutputStream; 
import java.net.Socket; 
import java.util.ArrayList; 

public class NoteClient extends Thread { 

    ArrayList<Note> list = null; 
    public NoteClient(ArrayList<Note> list){ 
     this.list = list; 
    } 

    @Override 
    public void run() { 
     super.run(); 
     Socket socket = null; 
     ObjectOutputStream out = null; 
     try{ 
      socket = new Socket("70.12.109.92", 8888); 
      out = new ObjectOutputStream(socket.getOutputStream()); 
      for (int i = 0; i < list.size(); i++) { 
       Note note = list.get(i); 
//    out.writeObject(note); 
       out.writeObject(note.toString());    //if used toString in Server 

      } 

     }catch(Exception e){ 
      e.printStackTrace(); 
     }finally{ 
      try { 
       out.close(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      try { 
       socket.close(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
    } 

}