首页
编程语言
数据库
网络开发
Algorithm算法
移动开发
系统相关
金融统计
人工智能
其他
首页
>
> 详细
辅导 COMP5216 Mobile Computing 、辅导andrio app 、辅导app编程 辅导Database|解析C/C++编程
School of IT Page 1 of 8
LabW02 – Handling Interactions
Objectives:
1. Understand the basics of Model-View-Controller
2. Use click event listener for ListView
3. Use UI element dialog
4. Use Activity navigation
5. Experience Cross-platform app development [Optional]
Tasks:
1. Handle long click event for an item in ListView
2. Pop up a dialog
3. Handle item clicking in ListView with Intent and another Activity
4. Develop Cross-platform app with Xamarin [Optional]
Homework 1
? To develop a basic ToDoList app
? Worth 5 marks
? Due in the lab of Week 05 with project files zipped and submitted in
CanvasCOMP5216 Mobile Computing LabW02
School of IT Page 2 of 8
Good apps provide useful functionality and an easy to use interface. The user interface is
made of various Graphical User Interface (GUI) components and typically waits for user
interaction.
In this tutorial, we will learn about various GUI components, their associated events, and
how to handle events on those components. You should finish LabW01 before proceeding
with the following tasks:
Task 1: Handling item long clicking in ListView
Currently nothing happens when an item is clicked in the ListView. We will first implement
“deleting an item” by long clicking it.
1. Add the following method into the MainActivity.
It sets two listeners for LongClick and Click events.
2. Call this method at the end of the onCreate() method
3. Run it. Click and long click a ToDoItem in the list view.
4. Now update the listener for onItemLongClickListener.
Add two lines of code to remove an item from the ArrayList and notify the ListView
adapter to update the list. Run it.
private void setupListViewListener() {
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView> parent, View view, final int
position, long rowId)
{
Log.i("MainActivity", "Long Clicked item " + position);
return true;
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view, int position, long
id) {
String updateItem = (String) itemsAdapter.getItem(position);
Log.i("MainActivity", "Clicked item " + position + ": " + updateItem);
}
});
}
@Override
protected void onCreate(Bundle savedInstanceState) {
XXXXXX
XXXXXX
// Setup listView listeners
setupListViewListener();
}COMP5216 Mobile Computing LabW02
School of IT Page 3 of 8
Run your code, and long click a ToDoItem, you should able to see the following line
printed on the Android Studio Logcat:
Task 2: Popping up a dialog
1. Add a dialog to let user confirm the delete operation. Replace the existing
OnItemLongClick method.
You will also need to add the following String resources into “res/values/strings.xml”
Run it and long click an item
Log.i("MainActivity", "Long Clicked item " + position);
items.remove(position); // Remove item from the ArrayList
itemsAdapter.notifyDataSetChanged(); // Notify listView adapter to update list
return true;
comp5216.sydney.edu.au.todolist I/MainActivity: Long Clicked item 1
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView> parent, View view, final int
position, long rowId)
{
Log.i("MainActivity", "Long Clicked item " + position);
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle(R.string.dialog_delete_title)
.setMessage(R.string.dialog_delete_msg)
.setPositiveButton(R.string.delete, new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int i) {
items.remove(position); // Remove item from the ArrayList
itemsAdapter.notifyDataSetChanged(); // Notify listView adapter
to update the list
}
})
.setNegativeButton(R.string.cancel, new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int i) {
// User cancelled the dialog
// Nothing happens
}
});
builder.create().show();
return true;
}
});
Delete an item
Do you want to delete this item?
Delete
Cancel
Edit
COMP5216 Mobile Computing LabW02
School of IT Page 4 of 8
2. In order to further customise a dialog, such as adding a third button, or
using your own layout, please refer to the following tutorial:
https://developer.android.com/guide/topics/ui/dialogs
If you do the above steps right, you should able to see the following screenshot once
you long click a ToDoItem:
Task 3: Handling item clicking in ListView with Intent and another
Activity
When clicking an item (NOT long click), we should open another Activity to edit the item.
1. Copy the EditToDoItemActivity.java to the same “src” folder as the
MainActivity.
This Activity receives the data from the MainActivity when an item is clicked, and send
back the updated content to the MainActivity. Read the inline comments for details.
2. Copy activity_edit_item.xml to the “res/layout” folder. This is the layout
for EditToDoItemActivityCOMP5216 Mobile Computing LabW02
School of IT Page 5 of 8
3. Update the onItemClick method in MainActivity to open the
EditToDoItemActivity
When an item is clicked, it creates an Intent to start another activity and wait for its
result. You need to define a variable to remember the request code in the MainActivity.
4. When the EditToDoItemActivity finish, MainActivity will receive the result
by checking the request code first, and then uses its result.
Add the following method into MainActivity:
To learn more about using intents to create flows, please read:
https://github.com/codepath/android_guides/wiki/Using-Intents-to-Create-Flows
You may also notice the use of Toast. It is a simple pop-up notification method in
Android. Learn more at:
https://developer.android.com/guide/topics/ui/notifiers/toasts
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id) {
String updateItem = (String) itemsAdapter.getItem(position);
Log.i("MainActivity", "Clicked item " + position + ": " + updateItem);
Intent intent = new Intent(MainActivity.this, EditToDoItemActivity.class);
if (intent != null) {
// put "extras" into the bundle for access in the edit activity
intent.putExtra("item", updateItem);
intent.putExtra("position", position);
// brings up the second activity
startActivityForResult(intent, EDIT_ITEM_REQUEST_CODE);
itemsAdapter.notifyDataSetChanged();
}
}
public final int EDIT_ITEM_REQUEST_CODE = 647;
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == EDIT_ITEM_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// Extract name value from result extras
String editedItem = data.getExtras().getString("item");
int position = data.getIntExtra("position", -1);
items.set(position, editedItem);
Log.i("Updated Item in list:", editedItem + ",position:"
+ position);
Toast.makeText(this, "updated:" + editedItem, Toast.LENGTH_SHORT).show();
itemsAdapter.notifyDataSetChanged();
}
}
}COMP5216 Mobile Computing LabW02
School of IT Page 6 of 8
5. Lastly, the application manifest must include the new
EditToDoItemActivity, otherwise this Activity cannot be used.
Open AndroidManifest.xml and add the following code inside the
tag.
6. Run it. Test the above steps. If all the steps are correct, your app now are
able to handle the click and long click events:
Task 4: Using Xamarin Studio [Optional]
Xamarin is a tool for cross-platform app development. It is free for students at:
https://xamarin.com/student
In the Windows platform, Xamarin is part of the latest Visual Studio. In the Mac platform,
Xamarin Studio is the development environment.
1. Windows Visual Studio for Xamarin:
https://developer.xamarin.com/guides/android/getting_started/hello,android/
android:name=".EditToDoItemActivity"
android:label="@string/app_name" >
COMP5216 Mobile Computing LabW02
School of IT Page 7 of 8
2. Xamarin Studio in Mac:
https://developer.xamarin.com/guides/mac/getting_started/hello,_mac/
Please refer to the following URL for more details on Xamarin:
http://developer.xamarin.com/
Homework 1 [5 marks, due in the lab Week 05]
In this homework, you need to design an app which contains at least two
views.
1. The Main view should contain [1 mark]:
? [0.5 mark] A ListView which displays all the saved ToDoItems, each ToDoItem
consists of ToDoItem title and the creation/last edited datetime. Clicking a
ToDoItem will switch to the “Edit/Add Item” view.
? [0.5 mark] An “ADD NEW” button. Once this button is clicked, the app will switch to
the “Edit/Add Item” view.
2. The “Edit/Add Item” view should contain [2 marks]:
? [0.5 mark] A Text field which allows user to type or edit the title of a ToDoItem to
add or update the ListView.
? A “Save” button used for adding new, or updating the title and datetime of
ToDoItem in the ListView:
o [0.5 mark] If adding a new item, capture both the item and creation datetime
of the ToDoItem. The creation datetime is the current system datetime.
o [0.5 mark] If updating an existing item, display both the item and creation/last
edited datetime of the ToDoItem. Upon saving, update the item and datetime
with the current system datetime.
? [0.5 mark] A “Cancel” button next to the “Save” button, used to close the Activity
without updating the ToDoItem. When this button is clicked, the app will pop up a
dialog that asks user: ”Are you sure to give up this edit? Your unsaved edit will be
discarded if you click YES”.
Hint: You should customise the ListView and the adapter. Read the following tutorial, and
replace the current ArrayAdapter with your own-defined Adapter class. Also replace the list
item layout “android.R.layout.simple_list_item_1” with your own layout.
https://github.com/codepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListViewCOMP5216 Mobile Computing LabW02
School of IT Page 8 of 8
Your app should also be able to handle the following data persistence tasks
[2 marks]:
? [0.5 mark] Every time user launches this app, the app loads the ToDoList from the
local Database.
? [0.5 mark] The ToDoList should be sorted and displayed based on the most recent
creation/last edited datetime i.e. the most recent ToDoItem shown at the top
? [0.5 mark] When clicking the “Save” button in the “Edit/Add Item” view, the app
should add or update the ToDoItem to both the ListView and local Database.
? [0.5 mark] Add a long click event to delete a ToDoItem from the ListView. When
user tries to delete the selected ToDoItem, the app will pop up a message that asks
user: ”Do you want to delete this item?” If the user clicks “YES”, this ToDoItem will
be deleted from both the ListView and local Database.
联系我们
QQ:99515681
邮箱:99515681@qq.com
工作时间:8:00-21:00
微信:codinghelp
热点文章
更多
辅导 comm2000 creating socia...
2026-01-08
讲解 isen1000 – introductio...
2026-01-08
讲解 cme213 radix sort讲解 c...
2026-01-08
辅导 csc370 database讲解 迭代
2026-01-08
讲解 ca2401 a list of colleg...
2026-01-08
讲解 nfe2140 midi scale play...
2026-01-08
讲解 ca2401 the universal li...
2026-01-08
辅导 engg7302 advanced compu...
2026-01-08
辅导 comp331/557 – class te...
2026-01-08
讲解 soft2412 comp9412 exam辅...
2026-01-08
讲解 scenario # 1 honesty讲解...
2026-01-08
讲解 002499 accounting infor...
2026-01-08
讲解 comp9313 2021t3 project...
2026-01-08
讲解 stat1201 analysis of sc...
2026-01-08
辅导 stat5611: statistical m...
2026-01-08
辅导 mth2010-mth2015 - multi...
2026-01-08
辅导 eeet2387 switched mode ...
2026-01-08
讲解 an online payment servi...
2026-01-08
讲解 textfilter辅导 r语言
2026-01-08
讲解 rutgers ece 434 linux o...
2026-01-08
热点标签
mktg2509
csci 2600
38170
lng302
csse3010
phas3226
77938
arch1162
engn4536/engn6536
acx5903
comp151101
phl245
cse12
comp9312
stat3016/6016
phas0038
comp2140
6qqmb312
xjco3011
rest0005
ematm0051
5qqmn219
lubs5062m
eee8155
cege0100
eap033
artd1109
mat246
etc3430
ecmm462
mis102
inft6800
ddes9903
comp6521
comp9517
comp3331/9331
comp4337
comp6008
comp9414
bu.231.790.81
man00150m
csb352h
math1041
eengm4100
isys1002
08
6057cem
mktg3504
mthm036
mtrx1701
mth3241
eeee3086
cmp-7038b
cmp-7000a
ints4010
econ2151
infs5710
fins5516
fin3309
fins5510
gsoe9340
math2007
math2036
soee5010
mark3088
infs3605
elec9714
comp2271
ma214
comp2211
infs3604
600426
sit254
acct3091
bbt405
msin0116
com107/com113
mark5826
sit120
comp9021
eco2101
eeen40700
cs253
ece3114
ecmm447
chns3000
math377
itd102
comp9444
comp(2041|9044)
econ0060
econ7230
mgt001371
ecs-323
cs6250
mgdi60012
mdia2012
comm221001
comm5000
ma1008
engl642
econ241
com333
math367
mis201
nbs-7041x
meek16104
econ2003
comm1190
mbas902
comp-1027
dpst1091
comp7315
eppd1033
m06
ee3025
msci231
bb113/bbs1063
fc709
comp3425
comp9417
econ42915
cb9101
math1102e
chme0017
fc307
mkt60104
5522usst
litr1-uc6201.200
ee1102
cosc2803
math39512
omp9727
int2067/int5051
bsb151
mgt253
fc021
babs2202
mis2002s
phya21
18-213
cege0012
mdia1002
math38032
mech5125
07
cisc102
mgx3110
cs240
11175
fin3020s
eco3420
ictten622
comp9727
cpt111
de114102d
mgm320h5s
bafi1019
math21112
efim20036
mn-3503
fins5568
110.807
bcpm000028
info6030
bma0092
bcpm0054
math20212
ce335
cs365
cenv6141
ftec5580
math2010
ec3450
comm1170
ecmt1010
csci-ua.0480-003
econ12-200
ib3960
ectb60h3f
cs247—assignment
tk3163
ics3u
ib3j80
comp20008
comp9334
eppd1063
acct2343
cct109
isys1055/3412
math350-real
math2014
eec180
stat141b
econ2101
msinm014/msing014/msing014b
fit2004
comp643
bu1002
cm2030
联系我们
- QQ: 99515681 微信:codinghelp
© 2024
www.7daixie.com
站长地图
程序辅导网!