Please enable JavaScript.
Coggle requires JavaScript to display documents.
Android programming with Java (Activity = Screen (Save data & objects,…
Android programming with Java
Basic Widgets
EditText
Button
ImageView & load Image From URL:
Let's install com.squareup.picasso in build.graddle
Activity = Screen
Mapping between MainActivity and main_activity.xml:
In onCreate(...):
setContentView(R.layout.main_activity)
Print out a Text to screen:
Toast toast=Toast.makeText(getApplicationContext(),"Hello",Toast.LENGTH_SHORT);
toast.show();
Print log:
Navigate to second Screen with Intent
Send parameters to SecondActivity:
Intent intent = new Intent(this, SeconActivity.class);
intent.putExtra("name", "Hoang");
intent.putExtra("age", 20);
startActivity(intent)
In SecondActivity, onCreate:
Integer age = intent.getIntExtra("age", 0);//default 0
String name = intent.getStringExtra("name");
Send an object from ScreenA to ScreenB:
class User must implement Serializable:
Receive object in ScreenB:
In AndroidManifest.xml:
Save data & objects
Save localObject with SharedPreferences
Save data as key-value:
Read saved data:
Using SQlite
Create an "Entity"
Entity's name and table's name maybe not the same !
Creat a class named DBHelper:
DBHelper must override 3 methods:
And also override 3 constructors:
A Database class implements CRUD operations:
Insert data(using ContentValues)
1 more item...
Firebase, online database from Google
You can login Facebook, Google using Firebase
-Open FirebaseConsole(in Chrome), create new App
-Install firebase-database,firebase-storage, firebase-ui-auth on AndroidApp
Reference object to Firebase DB:
You must download google-services.json and paste to your project's folder.Eg: YouProject/app/google-services.json
When your online database is changed => it automatically calls "onDataChange" OR "onCancelled"
-Get dataSnapshort object, cast to HashMap object(key-value)
-Convert HashMap object to ArrayList
In class Book, write a Factory Method to convert HashMap -> Book:
If you save images to FirebaseStorage,
you should reference to it
1 more item...
MVVM - Model View View Model
Google search "mvvm gradle" for installing
MVVM has 3 packages:
models, repositories, viewmodels
Repositories may contains classes like:
BookRepository, PlaceRepository, ...
Repository class may be "Singleton"
Class "ViewModel" get data through Repository:
On MainActivity, observe changes from ViewModel:
ListView, RecyclerView, TabLayout+ViewPager
RecyclerView
Giả sử hiện danh sách các địa điểm đi chơi(Place).
Trong màn PlacesActivity:
Adapter là "cầu nối" giữa data và RecyclerView
Tạo ra lớp Adapter kế thừa RecyclerView.Adapter
Adapter có tham chiếu đến Activity và Data:
Adapter must implement 2 methods of Adapter
1.UI of each list's item:
Each list's item is a PlaceItemView's object.
PlaceItemView's UI is derived from place_item_view.xml
2 more items...
Data of each list's item
3.How many items are there inside the List
Tạo dữ liệu giả(fake):
Kết nối Adapter:
RecyclerView phải có LayoutManager:
Nếu muốn hiện RecyclerView dạng "ngang":
RecyclerView.HORIZONTAL
Import thư viện RecyclerView, mở build.gradle:
ListView
Cần các đối tượng: listView, adapter, object list:
Dữ liệu Fake là danh sách objects:
Set Adapter và đổ dữ liệu vào ListView:
Adapter là nơi chung chuyển giữa dữ liệu và UI:
Cần thực thi các function trên Adapter
Tổng số phần tử trong ListView:
Từng phần tử một là cái gì?
1 more item...
Giao diện từng phần tử thế nào:
Đổ dữ liệu từng lên từng product_item:
TablLayout+ViewPager2
Làm UI dạng scroll qua nhiều trang
VD: Danh sách sản phẩm theo Category
TabLayout: nằm ở Header, scroll horizontally
ViewPager2 chiếm phần không gian còn lại
Cần 1 Adapter nối giữa:
List of Objects => Fragment => ViewPager
...
public class StudentFragmentStateAdapter extends FragmentStateAdapter {...}
Adapter cần thực thi 3 methods:
-constructor(data's list)
-createFragment
-getItemCount()
Trong Activity:
viewPager2.setAdapter(adapter);
new TabLayoutMediator(tabLayout, viewPager2,
(tab, position) -> tab.setText("page " + (position + 1))).attach();
tabLayout.getTabAt(0)
.setIcon(R.drawable.icon_name);
public class StudentPageFragment extends Fragment {...}
Thực thi onCreateView => Map Data => UI
Send request to Web Service
(call api)
-Use Volley to send request
-Use Gson to read json data
Gửi Request POST:
Trong hàm onResponse:
Convert từ JSON sang object:
Nếu exception:
Use OkHttpClient
Create a "Web service" interface:
An "Activity" may implement it:
Function which calls Web service may be ...a method:
Convert Response -> String -> JSONArray
Convert JSONArray to objects' array
Then inform Activity about final data
This must be run in "mean thread"
What about "error", how to inform ?
...activity.getResponse(null, errorObject)
Call "send request" from Activity:
Get response object:
Name: Nguyen Duc Hoang
Youtube:
https://www.youtube.com/c/nguyenduchoang