Android開發案例:SQLite資料庫和ListView列表結合
SQLite是Android系統內建的輕量級的關係型資料庫,執行速度非常快;ListView是Android中最常用的的控制元件之一,幾乎所有的應用程式都會用到它。本文通過一個例項介紹如何通過ListView列表展示資料以及實現SQLite資料庫的增刪改查,並且用到了登入、動畫等功能。
首先,新建Android應用程式FileDemo2。程式碼如下:
1、src資料夾內,com.demo.adapter包內FileNameAdapter.java檔案:
public class FileNameAdapter extends ArrayAdapter<FileName> { private int resourceId; public FileNameAdapter(Context context, int resource, List<FileName> objects) { super(context, resource, objects); resourceId=resource; } @Override public View getView(int position, View convertView, ViewGroup parent) { FileName fileName=getItem(position); View view; ViewHolder viewHolder; if (convertView==null) { view=LayoutInflater.from(getContext()).inflate(resourceId, null); viewHolder=new ViewHolder(); viewHolder.fileName=(TextView)view.findViewById(R.id.file_name); viewHolder.fileAuthor=(TextView)view.findViewById(R.id.file_author); viewHolder.id=0; view.setTag(viewHolder); } else { view=convertView; viewHolder=(ViewHolder)view.getTag(); } viewHolder.fileName.setText(fileName.getName()); viewHolder.fileAuthor.setText(fileName.getAuthor()); viewHolder.id=fileName.getId(); return view; } class ViewHolder{ TextView fileName; TextView fileAuthor; int id; } }
2、src資料夾內,com.demo. beans包內FileName.java檔案:
public class FileName { private int id; private String name; private String author; public FileName(int id,String name, String author) { this.id=id; this.name = name; this.author = author; } public int getId() { return id; } public String getName() { return name; } public String getAuthor() { return author; } public void setName(String name) { this.name = name; } public void setAuthor(String author) { this.author = author; } }
3、src資料夾內,com.demo.data包內MyDatabaseHelper.java檔案(建立資料庫):
public class MyDatabaseHelper extends SQLiteOpenHelper { public static final String CREATE_MENUNAME="create table MenuName (" +"id integer primary key autoincrement, " +"menuname text, " +"isChosen boolean)"; public static final String CREATE_FILE="create table FileInfo(" +"id integer primary key autoincrement, " +"title text, " +"subtitle text, " +"author text, " +"partone text, " +"parttwo text, " +"partthree text, " +"partfour text)"; private Context mContext; public MyDatabaseHelper(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); mContext=context; } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(CREATE_MENUNAME); db.execSQL("insert into MenuName (menuname,isChosen) values (?,?)",new String[]{"標題","1"}); db.execSQL("insert into MenuName (menuname,isChosen) values (?,?)",new String[]{"副標題","1"}); db.execSQL("insert into MenuName (menuname,isChosen) values (?,?)",new String[]{"作者","1"}); db.execSQL("insert into MenuName (menuname,isChosen) values (?,?)",new String[]{"版塊一","1"}); db.execSQL("insert into MenuName (menuname,isChosen) values (?,?)",new String[]{"版塊二","1"}); db.execSQL("insert into MenuName (menuname,isChosen) values (?,?)",new String[]{"版塊三","1"}); db.execSQL("insert into MenuName (menuname,isChosen) values (?,?)",new String[]{"版塊四","1"}); db.execSQL(CREATE_FILE); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub } }
4、src資料夾內,com.demo.filedemo2包內ActivityCollector.java檔案:
public class ActivityCollector {
public static List<Activity> activities = new ArrayList<Activity>();
//新增activity
public static void addActivity(Activity activity){
activities.add(activity);
}
//移除activity
public static void removeActivity(Activity activity){
activities.remove(activity);
}
//退出
public static void finishAll(){
for(Activity activity:activities){
if (!activity.isFinishing()) {
activity.finish();
}
}
}
}
5、src資料夾內,com.demo.filedemo2包內BaseActivity.java檔案:
public class BaseActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityCollector.addActivity(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
ActivityCollector.removeActivity(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.item_show:
Intent intent=new Intent(BaseActivity.this, ShowFileNameActivity.class);
startActivity(intent);
break;
case R.id.exit:
ActivityCollector.finishAll();
break;
default:
break;
}
return true;
}
}
6、src資料夾內,com.demo.filedemo2包內FileItemActivity.java檔案:
public class FileItemActivity extends BaseActivity {
private EditText titleText;
private EditText subtitleText;
private EditText authorText;
private EditText partoneText;
private EditText parttwoText;
private EditText partthreeText;
private EditText partfourText;
private Button updateBtn;
private MyDatabaseHelper dbHelper;
public static void actionStart(Context context,String fileid){
Intent intent =new Intent(context,FileItemActivity.class);
intent.putExtra("id", fileid);
context.startActivity(intent);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fileitem);
final String fileid=getIntent().getStringExtra("id");
titleText=(EditText)findViewById(R.id.title);
subtitleText=(EditText)findViewById(R.id.subtitle);
authorText=(EditText)findViewById(R.id.author);
partoneText=(EditText)findViewById(R.id.partone);
parttwoText=(EditText)findViewById(R.id.parttwo);
partthreeText=(EditText)findViewById(R.id.partthree);
partfourText=(EditText)findViewById(R.id.partfour);
updateBtn=(Button)findViewById(R.id.update);
dbHelper = new MyDatabaseHelper(this, "FileDemo.db", null, 1);
final SQLiteDatabase db = dbHelper.getWritableDatabase();
Cursor cursor=db.rawQuery("select * from FileInfo where id=?", new String[]{fileid});
if (cursor.moveToFirst()) {
String title=cursor.getString(cursor.getColumnIndex("title"));
String subtitle=cursor.getString(cursor.getColumnIndex("subtitle"));
String author=cursor.getString(cursor.getColumnIndex("author"));
String partone=cursor.getString(cursor.getColumnIndex("partone"));
String parttwo=cursor.getString(cursor.getColumnIndex("parttwo"));
String partthree=cursor.getString(cursor.getColumnIndex("partthree"));
String partfour=cursor.getString(cursor.getColumnIndex("partfour"));
titleText.setText(title);
subtitleText.setText(subtitle);
authorText.setText(author);
partoneText.setText(partone);
parttwoText.setText(parttwo);
partthreeText.setText(partthree);
partfourText.setText(partfour);
}
cursor.close();
updateBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String utitle=titleText.getText().toString();
String usubtitle=subtitleText.getText().toString();
String uauthor=authorText.getText().toString();
String upartone=partoneText.getText().toString();
String uparttwo=parttwoText.getText().toString();
String upartthree=partthreeText.getText().toString();
String upartfour=partfourText.getText().toString();
db.execSQL("update FileInfo set title=? , subtitle =? , author=? , partone=? , parttwo=? , partthree=? , partfour =? where id=?",
new String[]{utitle,usubtitle,uauthor,upartone,uparttwo,upartthree,upartfour,fileid});
Toast.makeText(FileItemActivity.this, "更新成功", Toast.LENGTH_SHORT).show();
Intent intent=new Intent(FileItemActivity.this,ShowFileNameActivity.class);
startActivity(intent);
finish();
}
});
}
@Override
public void onBackPressed() {
Intent intent=new Intent(FileItemActivity.this,ShowFileNameActivity.class);
startActivity(intent);
finish();
}
}
7、src資料夾內,com.demo.filedemo2包內LoginActivity.java檔案:
public class LoginActivity extends BaseActivity {
private SharedPreferences pref;
private SharedPreferences.Editor editor;
private EditText accountEdit;
private EditText passwordEdit;
private Button login;
private CheckBox rememberPass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
pref=PreferenceManager.getDefaultSharedPreferences(this);
accountEdit=(EditText)findViewById(R.id.account);
passwordEdit=(EditText)findViewById(R.id.password);
rememberPass=(CheckBox)findViewById(R.id.remember_pass);
login=(Button)findViewById(R.id.login);
boolean isRemember=pref.getBoolean("remember_password", false);
if (isRemember) {
//將賬戶和密碼都設定到文字框中
String account=pref.getString("account", "");
String password=pref.getString("password", "");
accountEdit.setText(account);
passwordEdit.setText(password);
rememberPass.setChecked(true);
}
login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String account=accountEdit.getText().toString();
String password=passwordEdit.getText().toString();
//預設admin & 123456 登入成功
if (account.equals("admin") && password.equals("123456")) {
editor=pref.edit();
if (rememberPass.isChecked()) {//檢查複選框是否被選中
editor.putBoolean("remember_password", true);
editor.putString("account", account);
editor.putString("password", password);
} else {
editor.clear();
}
editor.commit();
Intent intent=new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
finish();
} else {
Toast.makeText(LoginActivity.this, "account or password is invalid", Toast.LENGTH_SHORT).show();
}
}
});
}
}
8、src資料夾內,com.demo.filedemo2包內MainActivity.java檔案:
public class MainActivity extends BaseActivity {
public static Activity mainActivity=null;
private EditText titleText;
private EditText subtitleText;
private EditText authorText;
private Button openpage02Btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page01);
mainActivity=this;
titleText=(EditText)findViewById(R.id.title);
subtitleText=(EditText)findViewById(R.id.subtitle);
authorText=(EditText)findViewById(R.id.author);
openpage02Btn=(Button)findViewById(R.id.openpage02);
openpage02Btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this, Page02Activity.class);
String title=titleText.getText().toString();
String subtitle=subtitleText.getText().toString();
String author=authorText.getText().toString();
Bundle bundle=new Bundle();
bundle.putString("title", title);
bundle.putString("subtitle", subtitle);
bundle.putString("author", author);
intent.putExtras(bundle);
startActivity(intent);
overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
}
});
}
}
9、src資料夾內,com.demo.filedemo2包內Page02Activity.java檔案:
public class Page02Activity extends BaseActivity {
public static Activity p2Activity=null;
private EditText partoneText;
private Button openpage03Btn;
private Button backBtn;
private String title;
private String subtitle;
private String author;
private String partone;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page02);
p2Activity=this;
Bundle bundle=this.getIntent().getExtras();
title=bundle.getString("title");
subtitle=bundle.getString("subtitle");
author=bundle.getString("author");
openpage03Btn=(Button)findViewById(R.id.openpage03);
backBtn=(Button)findViewById(R.id.backto1);
partoneText=(EditText)findViewById(R.id.partone);
openpage03Btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
partone=partoneText.getText().toString();
Intent intent2=new Intent(Page02Activity.this, Page03Activity.class);
Bundle bundle=new Bundle();
bundle.putString("title", title);
bundle.putString("subtitle", subtitle);
bundle.putString("author", author);
bundle.putString("partone", partone);
intent2.putExtras(bundle);
startActivity(intent2);
overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
}
});
backBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}
10、 src資料夾內,com.demo.filedemo2包內Page03Activity.java檔案:
public class Page03Activity extends BaseActivity {
public static Activity p3Activity=null;
private EditText parttwoText;
private Button openpage04Btn;
private Button backBtn;
private String title;
private String subtitle;
private String author;
private String partone;
private String parttwo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page03);
p3Activity=this;
Bundle bundle=this.getIntent().getExtras();
title=bundle.getString("title");
subtitle=bundle.getString("subtitle");
author=bundle.getString("author");
partone=bundle.getString("partone");
parttwoText=(EditText)findViewById(R.id.parttwo);
openpage04Btn=(Button)findViewById(R.id.openpage04);
backBtn=(Button)findViewById(R.id.backto2);
openpage04Btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
parttwo=parttwoText.getText().toString();
Intent intent2=new Intent(Page03Activity.this, Page04Activity.class);
Bundle bundle=new Bundle();
bundle.putString("title", title);
bundle.putString("subtitle", subtitle);
bundle.putString("author", author);
bundle.putString("partone", partone);
bundle.putString("parttwo", parttwo);
intent2.putExtras(bundle);
startActivity(intent2);
overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
}
});
backBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}
11、 src資料夾內,com.demo.filedemo2包內Page04Activity.java檔案:
public class Page04Activity extends BaseActivity {
public static Activity p4Activity=null;
private EditText partthreeText;
private Button openpage05Btn;
private Button backBtn;
private String title;
private String subtitle;
private String author;
private String partone;
private String parttwo;
private String partthree;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page04);
p4Activity=this;
Bundle bundle=this.getIntent().getExtras();
title=bundle.getString("title");
subtitle=bundle.getString("subtitle");
author=bundle.getString("author");
partone=bundle.getString("partone");
parttwo=bundle.getString("parttwo");
partthreeText=(EditText)findViewById(R.id.partthree);
openpage05Btn=(Button)findViewById(R.id.openpage05);
backBtn=(Button)findViewById(R.id.backto3);
openpage05Btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
partthree=partthreeText.getText().toString();
Intent intent2=new Intent(Page04Activity.this, Page05Activity.class);
Bundle bundle=new Bundle();
bundle.putString("title", title);
bundle.putString("subtitle", subtitle);
bundle.putString("author", author);
bundle.putString("partone", partone);
bundle.putString("parttwo", parttwo);
bundle.putString("partthree", partthree);
intent2.putExtras(bundle);
startActivity(intent2);
overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
}
});
backBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}
12、 src資料夾內,com.demo.filedemo2包內Page05Activity.java檔案:
public class Page05Activity extends BaseActivity {
public static Activity p5Activity=null;
private EditText partfourText;
private Button saveBtn;
private Button backBtn;
private MyDatabaseHelper dbHelper;
private String title;
private String subtitle;
private String author;
private String partone;
private String parttwo;
private String partthree;
private String partfour;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page05);
p5Activity=this;
dbHelper = new MyDatabaseHelper(this, "FileDemo.db", null, 1);
Bundle bundle=this.getIntent().getExtras();
title=bundle.getString("title");
subtitle=bundle.getString("subtitle");
author=bundle.getString("author");
partone=bundle.getString("partone");
parttwo=bundle.getString("parttwo");
partthree=bundle.getString("partthree");
partfourText=(EditText)findViewById(R.id.partfour);
saveBtn=(Button)findViewById(R.id.save);
backBtn=(Button)findViewById(R.id.backto4);
saveBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
partfour=partfourText.getText().toString();
SQLiteDatabase db=dbHelper.getWritableDatabase();
db.execSQL("insert into FileInfo (title,subtitle,author,partone,parttwo,partthree,partfour) values (?,?,?,?,?,?,?)",
new String[]{title,subtitle,author,partone,parttwo,partthree,partfour});
Toast.makeText(Page05Activity.this, "儲存成功", Toast.LENGTH_SHORT).show();
Intent intent=new Intent(Page05Activity.this,ShowFileNameActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
finish();
Page04Activity.p4Activity.finish();
Page03Activity.p3Activity.finish();
Page02Activity.p2Activity.finish();
MainActivity.mainActivity.finish();
}
});
backBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}
13、 src資料夾內,com.demo.filedemo2包內ShowFileNameActivity.java檔案:
public class ShowFileNameActivity extends BaseActivity {
private MyDatabaseHelper dbHelper;
private List<FileName> fileNameList=new ArrayList<FileName>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_showfilename);
dbHelper = new MyDatabaseHelper(this, "FileDemo.db", null, 1);
final SQLiteDatabase db = dbHelper.getWritableDatabase();
initFileNames();
FileNameAdapter adapter=new FileNameAdapter(ShowFileNameActivity.this, R.layout.filename_item, fileNameList);
ListView listView=(ListView)findViewById(R.id.showfilename_list_view);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
int fileid=fileNameList.get(position).getId();
String final_id=Integer.toString(fileid);
FileItemActivity.actionStart(ShowFileNameActivity.this, final_id);
finish();
}
});
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
int fileid=fileNameList.get(position).getId();
final String final_id=Integer.toString(fileid);
AlertDialog.Builder dialog=new AlertDialog.Builder(ShowFileNameActivity.this);
dialog.setTitle("刪除");
dialog.setMessage("確認刪除該條文件資訊?");
dialog.setCancelable(false);
dialog.setPositiveButton("刪除", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
db.execSQL("delete from FileInfo where id=?", new String[]{final_id});
finish();
Intent intent=new Intent(ShowFileNameActivity.this, ShowFileNameActivity.class);
startActivity(intent);
}
});
dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
dialog.show();
return true;
}
});
}
private void initFileNames(){
SQLiteDatabase db = dbHelper.getWritableDatabase();
Cursor cursor=db.rawQuery("select * from FileInfo", null);
if (cursor.moveToFirst()) {
do {
String id=cursor.getString(cursor.getColumnIndex("id"));
int idint=Integer.parseInt(id);
String fileName=cursor.getString(cursor.getColumnIndex("title"));
String fileAuthor=cursor.getString(cursor.getColumnIndex("author"));
FileName fN = new FileName(idint,fileName, fileAuthor);
fileNameList.add(fN);
} while (cursor.moveToNext());
}
cursor.close();
}
@Override
public void onBackPressed() {
if (Page05Activity.p5Activity!=null) {
Page05Activity.p5Activity.finish();
}
if (Page04Activity.p4Activity!=null) {
Page04Activity.p4Activity.finish();
}
if (Page03Activity.p3Activity!=null) {
Page03Activity.p3Activity.finish();
}
if (Page02Activity.p2Activity!=null) {
Page02Activity.p2Activity.finish();
}
if (MainActivity.mainActivity!=null) {
MainActivity.mainActivity.finish();
}
Intent intent=new Intent(ShowFileNameActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
}
14、 res\anim資料夾內,in_from_right.xml檔案:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator">
<translate android:fromXDelta="100%p" android:toXDelta="0%p"
android:duration="500" />
</set>
15、 res\anim資料夾內,out_to_left.xml檔案:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator">
<translate android:fromXDelta="0%p" android:toXDelta="-100%p"
android:duration="500" />
</set>
16、 res\layout資料夾下,activity_fileitem.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:background="#effaf8"
android:orientation="vertical"
>
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="標題:" />
<EditText
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="副標題:" />
<EditText
android:id="@+id/subtitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="作者:" />
<EditText
android:id="@+id/author"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="版塊一:" />
<EditText
android:id="@+id/partone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="版塊二:" />
<EditText
android:id="@+id/parttwo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="版塊三:" />
<EditText
android:id="@+id/partthree"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="版塊四:" />
<EditText
android:id="@+id/partfour"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp" />
</LinearLayout>
</ScrollView>
<Button
android:id="@+id/update"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="更新"
android:textSize="20sp" />
</LinearLayout>
17、 res\layout資料夾下,activity_login.xml檔案:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#effaf8"
android:stretchColumns="1" >
<TableRow >
<TextView
android:layout_height="wrap_content"
android:text="Account:"
/>
<EditText
android:id="@+id/account"
android:layout_height="wrap_content"
android:hint="Input your account"
/>
</TableRow>
<TableRow >
<TextView
android:layout_height="wrap_content"
android:text="Password:"
/>
<EditText
android:id="@+id/password"
android:layout_height="wrap_content"
android:inputType="textPassword"
/>
</TableRow>
<TableRow >
<CheckBox
android:id="@+id/remember_pass"
android:layout_height="wrap_content"/>
<TextView
android:layout_height="wrap_content"
android:text="Remember password"/>
</TableRow>
<TableRow >
<Button
android:id="@+id/login"
android:layout_height="wrap_content"
android:layout_span="2"
android:text="Login"
/>
</TableRow>
</TableLayout>
18、 res\layout資料夾下,activity_main.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:background="#effaf8"
tools:context="com.demo.filedemo2.MainActivity" >
<android.support.v4.view.ViewPager
android:id="@+id/guidePages"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</android.support.v4.view.ViewPager>
</RelativeLayout>
19、 res\layout資料夾下,activity_showfilename.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:background="#effaf8"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/file_name"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:singleLine="true"
android:textSize="20sp"
android:text="標題"
/>
<TextView
android:id="@+id/file_author"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:singleLine="true"
android:textSize="20sp"
android:text="作者"
/>
</LinearLayout>
<ListView
android:id="@+id/showfilename_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
></ListView>
</LinearLayout>
20、 res\layout資料夾下,filename_item.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" >
<TextView
android:id="@+id/file_name"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="end"
android:textSize="20sp"
/>
<TextView
android:id="@+id/file_author"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:ellipsize="end"
android:singleLine="true"
android:textSize="20sp"
/>
</LinearLayout>
21、 res\layout資料夾下,page01.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:background="#effaf8"
android:orientation="vertical" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="標題:" />
<EditText
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="副標題:" />
<EditText
android:id="@+id/subtitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="作者:" />
<EditText
android:id="@+id/author"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp" />
</LinearLayout>
</ScrollView>
<Button android:id="@+id/openpage02"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="下一頁"
android:textSize="20sp"/>
</LinearLayout>
22、 res\layout資料夾下,page02.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:background="#effaf8"
android:orientation="vertical" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="版塊一:" />
<span style="white-space:pre"> </span><EditText
android:id="@+id/partone"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="20sp" />
</LinearLayout>
</ScrollView>
<LinearLayout
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="match_parent"
>
<Button
android:id="@+id/backto1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="上一頁"
android:textSize="20sp"/>
<Button
android:id="@+id/openpage03"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="下一頁"
android:textSize="20sp"
/>
</LinearLayout>
</LinearLayout>
23、 res\layout資料夾下,page03.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:background="#effaf8"
android:orientation="vertical" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="版塊二:" />
<EditText
android:id="@+id/parttwo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp" />
</LinearLayout>
</ScrollView>
<LinearLayout
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="match_parent"
>
<Button
android:id="@+id/backto2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="上一頁"
android:textSize="20sp"/>
<Button
android:id="@+id/openpage04"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="下一頁"
android:textSize="20sp"
/>
</LinearLayout>
</LinearLayout>
24、 res\layout資料夾下,page04.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:background="#effaf8"
android:orientation="vertical" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="版塊三:" />
<EditText
android:id="@+id/partthree"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp" />
</LinearLayout>
</ScrollView>
<LinearLayout
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="match_parent"
>
<Button
android:id="@+id/backto3"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="上一頁"
android:textSize="20sp"/>
<Button
android:id="@+id/openpage05"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="下一頁"
android:textSize="20sp"
/>
</LinearLayout>
</LinearLayout>
25、 res\layout資料夾下,page05.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:background="#effaf8"
android:orientation="vertical" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="版塊四:" />
<EditText
android:id="@+id/partfour"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp" />
</LinearLayout>
</ScrollView>
<LinearLayout
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="match_parent"
>
<Button
android:id="@+id/backto4"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="上一頁"
android:textSize="20sp"/>
<Button
android:id="@+id/save"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="儲存"
android:textSize="20sp"
/>
</LinearLayout>
</LinearLayout>
26、 res\menu資料夾下,main.xml檔案:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.demo.filedemo2.MainActivity" >
<item
android:id="@+id/item_show"
android:title="開啟檔案列表"/>
<item
android:id="@+id/exit"
android:title="退出"/>
</menu>
27、 AndroidManifest.xml檔案:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.demo.filedemo2"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".LoginActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity" android:windowSoftInputMode="adjustPan"></activity>
<activity android:name=".ShowFileNameActivity"></activity>
<activity android:name=".FileItemActivity" android:windowSoftInputMode="adjustPan"></activity>
<activity android:name=".Page02Activity" android:windowSoftInputMode="adjustPan"></activity>
<activity android:name=".Page03Activity" android:windowSoftInputMode="adjustPan"></activity>
<activity android:name=".Page04Activity" android:windowSoftInputMode="adjustPan"></activity>
<activity android:name=".Page05Activity" android:windowSoftInputMode="adjustPan"></activity>
</application>
</manifest>
該demo使用Android自帶的SQLIte資料庫,將所需要儲存的檔案的標題、副標題、作者、具體內容等資訊儲存在資料庫中。
1. 軟體啟動介面,輸入賬號和密碼登入。如果使用者名稱或密碼錯誤會提示“account or password is invalid”。
2. 登入成功後介面如圖左所示,可以在此介面輸入需要儲存的檔案的標題、副標題、作者資訊,如圖右所示。點選下一頁進入到下一介面。
3. 該介面可以用來儲存檔案的其他資訊,如圖左所示。點選上一頁返回,點選下一頁會進入下一介面,下一介面和該介面類似。此類介面在demo中設定了四個,提示標題分別為“版塊一”,“版塊二”,“版塊三”,“版塊四”,均以文字形式儲存內容,此類介面可新增刪減。版塊四介面如圖右所示。點選儲存可以把資訊儲存到資料庫,並自動跳轉到檔案列表介面。
4. 檔案列表介面,該介面顯示檔案的標題和作者,如圖左所示。剛新增的資訊在列表最下方一個。長按某一個item會提示是否刪除該item,如圖右所示。點選某個item進入到詳細資訊介面。
5. 詳細資訊介面,如圖所示,在該介面可以再次進行編輯,然後點選按鈕“更新”進行儲存,並再跳轉到列表介面。
6. 登入成功後,在主介面使用手機上的Menu鍵可以通過點選圖中“開啟檔案列表”直接進入到列表介面,如圖所示。在軟體使用過程中可以通過此操作退出軟體。