Android Topics: Fradments, contentObserver etc...,
Fragments
•
A fragment represents a
behavior or a portion of user interface in aactivity.we can combine multiple
fragments in a activity to build a multi pane UI.
•
While the activity is
running we can manipulate each fragment separately.
Types of fragments:
•
DialogFragment
:Displays a floating dialog. Using this class to create a dialog is a good
alternative to using the dialog helper methods in the Activity
class, because you can incorporate a fragment dialog into the back stack of
fragments managed by the activity, allowing the user to return to a dismissed
fragment.
•
ListFragment : Displays
a list of items that are managed by an adapter (such as a SimpleCursorAdapter),
similar to ListActivity.
It provides several methods for managing a list view, such as the onListItemClick()
callback to handle click events.
•
PrefernceFragment
:Displays a hierarchy of Preference
objects as a list, similar to PreferenceActivity.
This is useful when creating a "settings" activity for your
application.
Adding fragment:
•
A fragment is usually
used as part of an activity's user interface and contributes its own layout to
the activity.
•
To provide a layout for
a fragment, you must implement the onCreateView()
callback method, which the Android system calls when it's time for the fragment
to draw its layout. Your implementation of this method must return a View
that is the root of your fragment's layout.
•
Note:
If your fragment is a subclass of ListFragment,
the default implementation returns a ListView
from onCreateView(),
so you don't need to implement it.
Example:
public static class ExampleFragment
extends Fragment {
@Override
public View
onCreateView(LayoutInflaterinflater, ViewGroup container, Bundle
savedInstanceState) {
// Inflate the layout for this fragment
return
inflater.inflate(R.layout.example_fragment,
container, false);
}
}
•
The resource ID of the
layout you want to inflate.
•
The ViewGroup
to be the parent of the inflated layout. Passing the container is important in
order for the system to apply layout parameters to the root view of the
inflated layout, specified by the parent view in which it's going.
•
A boolean indicating
whether the inflated layout should be attached to the ViewGroup
(the second parameter) during inflation. (In this case, this is false because
the system is already inserting the inflated layout into the container—passing
true would create a redundant view group in the final layout.)
Adding fragment in Manifest:
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:name="com.example.news.ArticleListFragment"
android:id="@+id/list" android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<fragment android:name="com.example.news.ArticleReaderFragment"
android:id="@+id/viewer" android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
Adding fragment programmatically:
FragmentManagerfragmentManager = getFragmentManager()FragmentTransactionfragmentTransaction
= fragmentManager.beginTransaction();
ExampleFragment fragment = new
ExampleFragment(); fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();
// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction =
getFragmentManager().beginTransaction();
// Replace whatever is in the
fragment_container view with this fragment,
// and add the transaction to the back
stack transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null); // Commit the transaction
transaction.commit();
Adaptor
• An Adapter object acts as a bridge
between an AdapterView and the underlying data for that view. The Adapter
provides access to the data items. The Adapter is also responsible for making a
View for each item in the data set.
• registerDataSetObserver(DataSetObserver
observer) Register an observer that is called when changes happen to the data
used by this adapter.
• unregisterDataSetObserver(DataSetObserver
observer),Unregister an observer that has previously been registered with this
adapter via registerDataSetObserver(DataSetObserver).
ArraryAdapter
•
A concrete BaseAdapter
that is backed by an array of arbitrary objects.
•
By default this class
expects that the provided resource id references a single TextView.
•
TextView is referenced,
it will be filled with the toString() of each object in the array. we can add
lists or arrays of custom objects. Override the toString() method of our
objects to determine what text will be displayed for the item in the list.
Example:
public class ArrayAdapterDemo extends
ListActivity {
TextView selection;
String[] items = { "this", "is", "a", "really",
"silly", "list" };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setListAdapter(new ArrayAdapter<String>(
this,
android.R.layout.simple_expandable_list_item_1,
items));
selection=(TextView)findViewById(R.id.selection);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
String text = " position:" + position + " " + items[position];
selection.setText(text);
}
}
TextView selection;
String[] items = { "this", "is", "a", "really",
"silly", "list" };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setListAdapter(new ArrayAdapter<String>(
this,
android.R.layout.simple_expandable_list_item_1,
items));
selection=(TextView)findViewById(R.id.selection);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
String text = " position:" + position + " " + items[position];
selection.setText(text);
}
}
Cursor
Adapter
•
Adapter that exposes
data from a Cursor
to a ListView
widget. The Cursor must include a column named "_id" or this class
will not work.
•
public class
MessageAdapter extends CursorAdapter {
private Cursor mCursor;
private Context mContext;
private final LayoutInflatermInflater;
public MessageAdapter(Context context, Cursor c) {
super(context, c);
mInflater=LayoutInflater.from(context);
mContext=context;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextViewmobileNo=(TextView)view.findViewById(R.id.mobileNolistitem);
mobileNo.setText(cursor.getString(cursor.getColumnIndex(TextMeDBAdapter.KEY_MOBILENO)));
TextView frequency=(TextView)view.findViewById(R.id.frequencylistitem);
frequency.setText(cursor.getString(cursor.getColumnIndex(TextMeDBAdapter.KEY_FREQUENCY)));
TextViewrowid=(TextView)view.findViewById(R.id.rowidlistitem);
rowid.setText(cursor.getString(cursor.getColumnIndex(TextMeDBAdapter.KEY_ID)));
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final View view=mInflater.inflate(R.layout.message_list_item,parent,false);
return view;
}
}
private Cursor mCursor;
private Context mContext;
private final LayoutInflatermInflater;
public MessageAdapter(Context context, Cursor c) {
super(context, c);
mInflater=LayoutInflater.from(context);
mContext=context;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextViewmobileNo=(TextView)view.findViewById(R.id.mobileNolistitem);
mobileNo.setText(cursor.getString(cursor.getColumnIndex(TextMeDBAdapter.KEY_MOBILENO)));
TextView frequency=(TextView)view.findViewById(R.id.frequencylistitem);
frequency.setText(cursor.getString(cursor.getColumnIndex(TextMeDBAdapter.KEY_FREQUENCY)));
TextViewrowid=(TextView)view.findViewById(R.id.rowidlistitem);
rowid.setText(cursor.getString(cursor.getColumnIndex(TextMeDBAdapter.KEY_ID)));
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final View view=mInflater.inflate(R.layout.message_list_item,parent,false);
return view;
}
}
SimpleCursorAdapter
•
An easy adapter to map
columns from a cursor to TextViews or ImageViews defined in an XML file. we can
specify which columns we want, which views we want to display the columns, and
the XML file that defines the appearance of these views.
Exampe:
•
public class
ContentUserDemo extends Activity {
•
private static final
String TAG = "ContentUserDemo";
•
•
/** Called when the activity is first
created. */
•
@Override
•
public void
onCreate(Bundle savedInstanceState) {
•
super.onCreate(savedInstanceState);
•
setContentView(R.layout.main);
•
•
// Get content provider and cursor
•
ContentResolvercr =
getContentResolver();
•
Cursor cursor = cr.query(Settings.System.CONTENT_URI,
null, null, null, null);
•
•
// Let activity manage the cursor
•
startManagingCursor(cursor);
•
Log.d(TAG,
"cursor.getCount()=" + cursor.getCount());
•
•
// Get the list view
•
ListViewlistView =
(ListView) findViewById(R.id.listView);
•
String[] from = { Settings.System.NAME,
Settings.System.VALUE };
•
int[] to = { R.id.textName,
R.id.textValue };
•
SimpleCursorAdapter
adapter = new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);
•
listView.setAdapter(adapter);
•
}
•
}
ContentObserver
•
Receives call backs for
changes to content. Must be implemented by objects which are added to a ContentObservable.
Example
private class MyContentObserver extends
ContentObserver {
public MyContentObserver() {
super(null);
}
@Override
public void onChange(booleanselfChange) {
super.onChange(selfChange);
System.out.println (" Calling onChange" );
}
}
MyContentObservercontentObserver = new MyContentObserver();
context.getContentResolver().registerContentObserver (People.CONTENT_URI, true, contentObserver);
public MyContentObserver() {
super(null);
}
@Override
public void onChange(booleanselfChange) {
super.onChange(selfChange);
System.out.println (" Calling onChange" );
}
}
MyContentObservercontentObserver = new MyContentObserver();
context.getContentResolver().registerContentObserver (People.CONTENT_URI, true, contentObserver);
Comments
Post a Comment
Please post comments here:-)