第一,你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;
}
}
}
}
}