2014-02-13 11 views
0

메모를 관리 할 수있는 프로그램을 만들어야합니다.구성 요소 기반 Android

  1. "MyMemoInfoList"프로그램을 만듭니다.

    • MyMeMoEditor 프로그램을 통해 콘텐츠를 목록 형식으로 인쇄하십시오. (메모, 제목, 날짜)
    • 항목을 선택한 후 편집 버튼을 클릭하면 MyMemoEditor 화면에 자세한 내용이 표시되고 내용을 편집 한 후 업데이트 된 내용이 목록에 반영됩니다.
    • 항목을 선택하고 삭제 버튼을 클릭하면 해당 항목이 목록에서 제거됩니다.
    • 추가 버튼을 누르면 내용을 입력하고 저장 버튼을 눌러 저장할 수있는 MyMemoEditor 화면이 나타납니다. 또한 목록에 표시되어야합니다.
  2. MyMemoEditor 프로그램을 만듭니다.

    • 새 메모를 목록의 기본 화면에 등록하면 새 메모가 저장됩니다.
    • 편집 메뉴를 실행하면 원본 메모가 업데이트됩니다. MyMemoMain/MyMemoInfoList 클래스

      • MyMemoMain 클래스 '이벤트의
    • 구현은 익명 중첩 클래스 형태로 구현되어야한다.

    • MyMemoInfoList 클래스의 이벤트는 Listener를 상속해야 구현됩니다.

은 제가 적용하는 데 필요한 방법은 서비스, 브로드 캐스트 리시버, 안드로이드 UI, 안드로이드 UI 이벤트 및 어댑터 뷰 AdapterView 생각합니다.

다른 활동으로 전환하는 데 어려움을 겪고 있습니다.

미리 감사드립니다.

답변

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(); 
     } 
    } 
} 

=================================== ==================== 이제 관리자 클래스 (클래스)를 살펴 보겠습니다.

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 (클래스) 당신의 변수를 설정보고 게터/세터/생성자/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 (활동)로 이동하십시오.

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(); 
    } 

} 

=========================================== ============================= 이제 클라이언트 (클래스)를 살펴 보겠습니다.

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(); 
      } 


     } 
    } 

} 

=========================================== ========================== 마지막으로 새 프로젝트를 만듭니다 (자바)와 서버 (클래스)합니다

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 (클래스)입니다.

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); 
    } 





} 

=========================================== 이제 ========================================

, 우리는 또 다른 활동을 (메모 편집 활동) :

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; 
    } 

} 

============ ========================================================================================================== ====================== 는 마지막으로 우리는 클라이언트/서버를 만들 : 는 우선, 서버 (거기에 새로운 자바 프로젝트 + 클래스) 만들어 보자 :도 를, 매니페스트 파일에서 권한 설정을 변경하는 것을 잊지 마세요.

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(); 
      } 
     } 
    } 

} 
관련 문제