Professional Documents
Culture Documents
المحاضرة 8
المحاضرة 8
• we can only show a single Activity on the screen at one given point of time so
we were not able to divide the screen and control different parts separately.
With the help of Fragment’s we can divide the screens in different parts and
controls different parts separately.
• By using Fragments we can comprise multiple Fragments in a single Activity.
Fragments have their own events, layouts and complete life cycle. It provide
flexibility and also removed the limitation of single Activity on the screen at a
time.
Fragments offer an escape solution.
• يمكننا فقط عرض نشاط واحد على الشاشة في وقت معين لذلك لم نتمكن من تقسيم الشاشة
يمكننا تقسيم،Fragment’s بمساعدة.والتحكم في أجزاء مختلفة بشكل منفصل
.الشاشات إلى أجزاء مختلفة والتحكم في أجزاء مختلفة بشكل منفصل
األجزاء لها أحداثها. يمكننا تكوين أجزاء متعددة في نشاط واحد، • باستخدام األجزاء
إنه يوفر المرونة ويزيل أيضًا قيود نشاط واحد على.وتخطيطاتها ودورة حياتها الكاملة
.الشاشة في كل مرة
) (من هذه المشكلة.• شظايا تقدم حال للهروب
• Fragments must exist within the boundaries of an Activity that acts as a
‘home-base’ or host.
• We can add , replace or remove Fragment’s in an Activity while the activity
is running.
• Fragments has its own layout and its own behavior with its own life cycle
callbacks.
• Fragment can be used in multiple activities.
• We can also combine multiple Fragments in a single activity.
.يجب أن توجد الشظايا داخل حدود نشاط يعمل بمثابة "قاعدة منزلية" أو مضيف •
.يمكننا إضافة أو استبدال أو إزالة جزء في نشاط أثناء تشغيل النشاط •
األجزاء لها تخطيطها الخاص وسلوكها الخاص مع عمليات االسترجاعات لدورة الحياة •
.الخاصة بها
.يمكن استخدام جزء في أنشطة متعددة •
.يمكننا أيضًا دمج أجزاء متعددة في نشاط واحد •
Fragments could access ‘global data’ held in the main activity to which
they belong. Likewise, they could send values of their own to the main
activity for potential dissemination ) (النشر محتملto other fragments.
Fragments have their own particular Life-Cycle, in which the
onCreateView method does most of the work needed to make them.
• يمكن لألجزاء الوصول إلى "البيانات العالمية" الموجودة في النشاط الرئيسي الذي تنتمي
يمكنهم إرسال القيم الخاصة بهم إلى النشاط الرئيسي للنشر المحتمل (النشر، وبالمثل.إليه
.محتمل) إلى أجزاء أخرى
حيث يكون ملف، • لألجزاء دورة حياة خاصة بها
.بمعظم العمل المطلوب إلنجازهاonCreateView • تقوم طريقة
Fragments: Modularity
• On larger screens: the app should display a static navigation drawer and a
list in a grid layout.
• On smaller screens; the app should display a bottom navigation
bar and a list in a linear layout.
—Separating the navigation elements from the content can make this process
more manageable. The activity is then responsible for displaying the
correct navigation UI while the fragment displays the list with the proper
layout.
يجب أن يعرض التطبيق درج تنقل ثاب ًتا وقائمة في تخطيط:• على الشاشات األكبر حجمًا
.الشبكة
.• على الشاشات األصغر ؛ يجب أن يعرض التطبيق شريط تنقل سفلي وقائمة بتنسيق خطي
• يمكن أن يؤدي فصل عناصر التنقل عن المحتوى إلى جعل هذه العملية أكثر قابلية
يكون النشاط بعد ذلك مسؤوالً عن عرض واجهة مستخدم التنقل الصحيحة بينما.لإلدارة
.يعرض الجزء القائمة بالتخطيط المناسب
Fragment:
• We can create Fragments by :
1. extending Fragment class: For creating a Fragment firstly we extend
the Fragment class, then we must override key lifecycle methods
onCreateView() to define the layout and in order to run a Fragment.
import android.os.Bundle;
import
android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.ViewGroup;
public class FirstFragment extends
Fragment { @Override
return a View component from this method that is the root of the fragment’s layout.
:يمكننا إنشاء شظايا من خالل
ثم يجب علينا تجاوز،Fragment نقوم بتوسيع فئة، ًإلنشاء جزء أوالFragment: توسيع فئة.1
لتعريف التخطيط ومن أجل تشغيل جزءonCreateView () طرق دورة حياة المفتاح
إرجاع مكون عرض من هذه الطريقة التي تمثل جذر تخطيط الجزء
• We can create Fragments by :
2. or by inserting a Fragment into our Activity layout: by declaring
the Fragment in the activity’s layout file, as a <fragment> element.
We can manipulate each Fragment independently, such as add or
remove them.
Basic Fragment Code In XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" :يمكننا إنشاء شظايا من خالل •
android:layout_width="fill_parent" أو عن طريق إدراج جزء في تخطيط.2 •
android:layout_height="fill_parent"
tools:context=".MainActivity"> عن طريق إعالن الجزء في ملف:النشاط لدينا
<fragment fragment>.< كعنصر، تخطيط النشاط
android:id="@+id/fragments“android:lay مثل، يمكننا معالجة كل جزء بشكل مستقل •
out_width="match_parent" اإلضافة أو الحذف
android:layout_height="match_parent" />
</LinearLayout>
– We can manipulate each Fragment independently, such as add or remove them.
Fragment’s lifecycle:
The list of methods which you can to override in your Fragment class:
Fragment’s lifecycle:
1. onAttach(): The fragment instance is
associated with an activity instance.
This method is called first, even before
onCreate() method.
This method let us know that our Fragment has
been attached to an activity.
@Override
public void onAttach(Activity activity) {
super. onAttach(activity);
// add your code here which executes when fragment
//instance is associated}
تسمى هذه. يرتبط مثيل الجزء بنسخة النشاط:)( onAttach .
تتيح لنا هذه.)( onCreate حتى قبل طريقة، ًالطريقة أوال
.الطريقة معرفة أن الجزء الخاص بنا قد تم إرفاقه بنشاط ما
2.onCreate(): This will be called when
creating the fragment
It means when a new fragment instance
initializes which always happens after it
attaches to the host
@Override
public void onCreate(Bundle savedlnstanceState سيتم استدعاء هذا عند إنشاء الجزء.2
super.onCreate(savedlnstanceState); هذا يعني أنه عند تهيئة مثيل جزء جديد والذي
// add your code here which executes when fragment’s يحدث دائمًا بعد إرفاقه بالمضيف
//instance initializes
3. onCreateView(): Most of the work is done here.
The will be called when it’s time for the fragment to draw its
UI(user interface) for the first time.
To draw a UI for our fragment we must return a View component
from this method that is the root of our fragment’s layout.
We can also return null if the fragment does not provide a UI.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup
container, Bundle savedlnstanceState)
{View v =
inflater. inflate(R. lay out. fragment_test, container, false); //
add your code here to draw the UI for the first
// time means in this method we can get the //reference of
// the views which are created in our xml file
return v; }
inflatefint resource, MewGroup root, boolean attachToRoot)
Inflate a new view hierarchy from the specified xml resource.
.يتم تنفيذ معظم العمل هنا.3
.سيتم استدعاء الجزء عندما يحين وقت رسم الجزء لواجهة المستخدم (واجهة المستخدم) ألول مرة
يجب أن نعيد مكون العرض من هذه الطريقة التي تمثل جذر، لرسم واجهة مستخدم للجزء الخاص بنا
.تخطيط الجزء الخاص بنا
ً يمكننا أي
.ضا إرجاع قيمة خالية إذا كان الجزء ال يوفر واجهة مستخدم
Fragment’s lifecycle:
5.onActivityCreated(): This method is called after
the onCreateView() method when the host activity is
created.
This method indicates that the activity's onCreate
has completed
@Override
public void onActivityCreated(Bundle
savedlnstanceState
{ super.onActivityCreated(savedlnstanceState);
// add your code here which executes when the host activity
//is created.
onCreateView تستدعى هذه الطريقة بعد طريقة.5 •
.)(عند إنشاء نشاط المضيف
onCreate تشير هذه الطريقة إلى اكتمال نشاط •
يتم استدعاء هذه الطريقة عند إزالة العرض والموارد األخرى ذات الصلة التي تم.10
. من التسلسل الهرمي لعرض النشاط وإتالفهاonCreateView () إنشاؤها في طريقة
Fragment’s lifecycle:
11. onDestroy(): This method is called to do
final clean up of the Fragment’s state but
Not guaranteed to be called by the Android
platform.
This method called after onDestroyView()
method.
@Override
public void onDestroy() {
super.onDestroy(); // add your code here which
//executes when the final clean up for the
//Fragment's state is needed.
} يُطلق على هذه الطريقة إجراء التنظيف النهائي.11
لحالة الجزء ولكن ليس مضمو ًنا أن يتم استدعاؤها
تسمى هذه. األساسيAndroid بواسطة نظام
.)( onDestroyView الطريقة بعد طريقة
12.onDetach(): This method called after
onDestroy() method to notify that the fragment
has been disassociated from its hosting activity
means Fragment is detached from its host
Activity تستدعى هذه الطريقة بعد طريقة.12
@Override لإلعالم بأن الجزء قد تم فصلهonDestroy ()
public void onDetach() { عن نشاط االستضافة الخاص به مما يعني فصل
super.onDetach(); الجزء عن نشاط المضيف الخاص به
// add your code here which executes when
fragment has been //disassociated from its
hosting activity
}
Android SQLite: Prosperities
• Zero-Configuration:
•There is no server process that needs to be started, stopped, or configured.
•Nothing needs to be done to tell the system that SQLite is running.
• Serverless:
Most SQL database engines are implemented as a separate server process.
Programs that want to access the database communicate with the server using
some kind of interprocess communication (typically TCP/IP) to send requests to
the server and to receive back results. SQLite does not work this way.
With SQLite, the process that wants to access the database reads and writes
directly from the database files on disk. There is no intermediary server
process.
• Single Database File:
a single ordinary disk file can easily be copied onto a USB memory stick or
emailed for sharing.
• Stable Cross-Platform Database File:
A database file written on one machine can be copied to and used on a different
machine with a different architecture. Newer versions of SQLite can read and
write older database files.
• Compact:
the whole SQLite library with everything enabled is less than 500KiB in size.
:التكوين الصفري •
.ال توجد عملية خادم تحتاج إلى أن تبدأ أو تتوقف أو تهيئ •
.يعملSQLite ال يلزم فعل أي شيء إلخبار النظام بأن •
:بدون خادم •
تتواصل البرامج التي.كعملية خادم منفصلةSQL يتم تنفيذ معظم محركات قاعدة بيانات •
ترغب في الوصول إلى قاعدة البيانات مع الخادم باستخدام نوع من االتصال بين العمليات
سكليتي ال.إلرسال الطلبات إلى الخادم واستالم النتائج مرة أخرىTCP / IP) (عاد ًة
.يعمل بهذه الطريقة
فإن العملية التي تريد الوصول إلى قاعدة البيانات تقرأ وتكتب،SQLite باستخدام •
. ال توجد عملية خادم وسيط.مباشرة من ملفات قاعدة البيانات الموجودة على القرص
:ملف قاعدة بيانات واحد •
أو إرسالها بالبريدUSB يمكن بسهولة نسخ ملف قرص عادي واحد على ذاكرة •
.اإللكتروني للمشاركة
:ملف قاعدة بيانات مستقر عبر األنظمة األساسية •
يمكن نسخ ملف قاعدة البيانات المكتوب على جهاز واحد واستخدامه على جهاز مختلف •
قراءة ملفات قاعدة البياناتSQLite يمكن لإلصدارات األحدث من.بهندسة مختلفة
.القديمة وكتابتها
:المدمج •
. كيلوبايت500 بأكملها مع تمكين كل شيء أقل منSQLite حجم مكتبة •
Android SQLite: Prosperities
• manifest typing:
the datatype is a property of the value itself, not of the column in which the
value is stored. SQLite thus allows the user to store any value of any datatype
into any column regardless of the declared type of that column. (There are
some exceptions to this rule: An INTEGER PRIMARY KEY column may
only store integers.
• Variable-length records:
SQLite use only the amount of disk space actually needed to store the
information in a row. If you store a single character in a VARCHAR(IOO)
column, then only a single byte of disk space is consumed.
• lightweight database:
in Android there is Database Management System (DBMS) called SQLite
which is a very lightweight database included with Android Architecture ,
SQLite can be used to:
1. Create a database,
2. Define: (SQLite tables, queries, ...).
3. (Insert, Delete, change ) rows.
Android SQLite combines a clean SQL interface with a very small memory
footprint and decent speed.
:الكتابة الواضحة •
وبالتالي. وليس للعمود الذي يتم تخزين القيمة فيه، نوع البيانات هو خاصية للقيمة نفسها •
للمستخدم بتخزين أي قيمة من أي نوع بيانات في أي عمود بغض النظرSQLite يسمح
قد يخزن عمود: (هناك بعض االستثناءات لهذه القاعدة.عن النوع المعلن لذلك العمود
ً
.أعدادا صحيحة فقط INTEGER PRIMARY KEY
:سجالت متغيرة الطول •
فقط مقدار مساحة القرص الالزمة فعليًا لتخزين المعلومات في صفSQLite يستخدم •
فسيتم استهالك،VARCHAR (IOO) إذا قمت بتخزين حرف واحد في عمود.واحد
.بايت واحد فقط من مساحة القرص
:قاعدة بيانات خفيفة الوزن •
وهيSQLite يسمىDBMS) ( نظام إدارة قواعد البياناتAndroid يوجد في نظام •
ويمكن،Android Architecture قاعدة بيانات خفيفة الوزن ج ًدا متضمنة في
:من أجلSQLite استخدام
، إنشاء قاعدة بيانات •
.)... ، استعالمات،SQLite (جداول:حدد •
.) تغيير، حذف، صفوف (إدراج •
النظيفة مع مساحة ذاكرة صغيرة ج ًداSQL بين واجهةAndroid SQLite يجمع •
.وسرعة مناسبة
Android SQLite: SQLiteOpenHelper
SQLite is a typical relational database, containing tables (which consists
of rows and columns), indexes etc. We can
create our own tables to hold the data accordingly. This structure is
referred to as a schema.
Android has features available to handle changing database schemas, which
mostly depend on using the SQLiteOpenHelper class.
SQLiteOpenHelper is designed to get rid of two very common
problems.
1. When the application runs the first time - At this point, we do not yet have a
database. So we will have to create the tables, indexes, starter data, and so on.
2. When the application is upgraded to a newer schema - Our database will still be
on the old schema from the older edition of the app. We will have option to alter
the database schema to match the needs of the rest of the app.
، ) تحتوي على جداول (تتكون من صفوف وأعمدة، هي قاعدة بيانات عالئقية نموذجيةSQLite •
يمكننا ذلك. إلخ، وفهارس
. يشار إلى هذا الهيكل على أنه مخطط.إنشاء الجداول الخاصة بنا لالحتفاظ بالبيانات وف ًقا لذلك •
والتي تعتمد، على ميزات متاحة للتعامل مع مخططات قواعد البيانات المتغيرةAndroid يحتوي •
SQLiteOpenHelper. في الغالب على استخدام فئة
.للتخلص من مشكلتين شائعتين ج ًداSQLiteOpenHelper تم تصميم •
لذلك سيتعين. ليس لدينا قاعدة بيانات بعد، في هذه المرحلة- عندما يتم تشغيل التطبيق ألول مرة •
.علينا إنشاء الجداول والفهارس وبيانات البداية وما إلى ذلك
ستظل قاعدة البيانات الخاصة بنا على المخطط القديم من- عندما تتم ترقية التطبيق إلى مخطط أحدث •
سيكون لدينا خيار لتغيير مخطط قاعدة البيانات لتتناسب مع احتياجات.اإلصدار األقدم من التطبيق
.بقية التطبيق
- Con text. MOD EPRIVATE indicates that the database hie can only be
accessed by the calling application or all applications sharing the same user ID.
- The third parameter is a Cursor factory object which can be left null if not
required.
يشير إلى أنه ال يمكن الوصول إلى قاعدة البيانات إال عن طريق تطبيق االتصال أو
.جميع التطبيقات التي تشترك في نفس معرف المستخدم
. المعلمة الثالثة هي كائن مصنع المؤشر الذي يمكن تركه فار ًغا إذا لم يكن مطلوبًا-