Tuesday, September 18, 2012

List Views

List Views - Hallo sahabat Google Android Developer Tutorial, Pada Artikel yang anda baca kali ini dengan judul List Views, kami telah mempersiapkan artikel ini dengan baik untuk anda baca dan ambil informasi didalamnya. mudah-mudahan isi postingan Artikel List view with background color changing for an item selected, yang kami tulis ini dapat anda pahami. baiklah, selamat membaca.

Judul : List Views
link : List Views

Baca juga


List Views


LIST VIEW Tutorial

             ListView is a view group that displays a list of scrollable items. The list items are automatically inserted to the list using an Adapter that pulls content from a source such as an array or database query and converts each item result into a view that's placed into the list.
ListActivity
ListActivity hosts a ListView object that can be bound to different data sources, typically either an array or a Cursor holding query results.
The Demonstrative project lists a set of Bond films and various list selection features
1.Create  Android project with details as listed in table below
Property name
Property value
Project name
SRM_UIExamples
Package name
in.ac.srmuniv
Activity name
ListViewsExample
Layout xml name
listview
2.Copy the code  to the file listview.xml in res/layout folder 
<?xml version="1.0"encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:divider="#b5b5b5"
        android:dividerHeight="2dp"
        android:listSelector="@drawable/listrow_selection"/>

</LinearLayout>
3 a.Copy the code rowlist_bg.xml in res/drawable-hdpi folder.
<?xml version="1.0"encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
  <gradient
      android:startColor="#f35edd"
      android:centerColor="#f44173"
      android:endColor="#f52a1e"
      android:angle="270" />

</shape>
3 b.Copy the code rowlist_hover.xml in res/drawable-hdpi folder.
<?xml version="1.0"encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
  <gradient
      android:startColor="#faf72e"
      android:centerColor="#bdf743"
      android:endColor="#44f760"
      android:angle="270" />
</shape>
3 c.Copy the code rowlist_border.xml in res/drawable-hdpi folder.
<?xml version="1.0"encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
      <shape
        android:shape="rectangle">
            <stroke android:width="1dp"android:color="#dbdbdc" />
            <solid android:color="#FFFFFF"/>
        </shape>
   </item>
</layer-list>
3 d.Copy the code rowlist_selection.xml in res/drawable-hdpi folder.
<?xml version="1.0"encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_pressed="false"
        android:drawable="@drawable/listrow_bg" />

    <item android:state_pressed="true"
        android:drawable="@drawable/listrow_hover" />

    <item android:state_selected="true"
     android:state_pressed="false"
        android:drawable="@drawable/listrow_hover" />
</selector>
4.Copy the code in ListViewsExample.java Activity
package in.ac.srmuniv;
import android.os.Bundle;
import android.view.View;
importandroid.widget.ArrayAdapter;
importandroid.widget.ListView;
importandroid.widget.Toast;
importandroid.app.ListActivity;

public class ListViewsExample extends ListActivity {
    String[] bond_films = { "Dr.No", "Man with Golden gun",
            "You only Live twice", "Live and Let die", "Thunderball",
            "License to Kill", "From Russia with Love", "Moonraker",
            "Octopussy", "A View to Kill", "On Her Majesty Service",
            "Golden Eye"

    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview);
        setListAdapter(newArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, bond_films));
    }

    public voidonListItemClick(ListView parent, View v, int position, long id) {
        Toast.makeText(this, "You have selected " + bond_films[position],
                Toast.LENGTH_SHORT).show();
    }
}
 5.No need to modify Androidmanifest.xml
 6.Run the application in the emulator. 
. Figure 1 shows the ListView view in action. When an item is selected, a message will be displayed. Click on an item. A message containing the item selected will be displayed.

Figure 1: The ListView view in action item selected is pressed  Figure 2 Shows when it is released


CODE EXPLANATION
The first thing to notice in this example is that the ListViewExample class extends the ListActivity class.The ListActivity class extends the Activity class and  it displays a list of items by binding to a data source. Also note that there is no need to modify the main.xml   file to include the ListView; the ListActivity class itself contains a ListView.Hence, in the onCreate() method, you don’t need to call the setContentView() method to load the UI from the main.xml file:
//---no need to call this---
//setContentView(R.layout.main);
In the onCreate() method, you use the setListAdapter() method to programmatically fill the entire screen of the activity with a ListView. The ArrayAdapter object manages the array of strings that will be displayed by the ListView. In the preceding example, you set the ListView to display in the simple_list_item_1 mode:
               setListAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1bond_films));
Customizing the ListView
   ListView is a versatile view that you can further customize.You can change the background of the 
list row item when selected or pressed to make a lively look.XML resource files in drawable folder provides gradient backround with appropriate RGB values entered.The following ListView property entry ensures selection of background in list view row when pressed and released after selection,
          android:listSelector="@drawable/listrow_selection"
 The following  code added to your code allow multiple items in the ListView to be selected and how you can enable filtering support.TEnabling Filtering and Multi-Item Support in the ListView
1. Using the ListViewActivity  created in the previous section, add the following statements in
bold to the ListviewExample.java file:
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//---no need to call this---
//setContentView(R.layout.main);
ListView lstView = getListView();
//lstView.setChoiceMode(ListView.CHOICE_MODE_NONE);
//lstView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
lstView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lstView.setTextFilterEnabled(true);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_checked, bond_films));
}
2. Run the application on the Android emulator. You can now click on each item to
display the check icon next to it (see Figure 2).

 Figure 2: The ListView view in action with multiple choice

    To programmatically get a reference to the ListView object, you use the getListView() method,which fetches the ListActivity’s list view. You need to do this so that you can programmatically modify the behavior of the ListView. In this case, you used the setChoiceMode() method to specify how the ListView should handle a user’s click. For this example, you set it to ListView.CHOICE_MODE_MULTIPLE, which means that the user can select multiple items:
ListView lstView = getListView();
//lstView.setChoiceMode(ListView.CHOICE_MODE_NONE);
//lstView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
lstView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
A very cool feature of the ListView is its support for filtering. When you enable fi ltering through the setTextFilterEnabled() method, users will be able to type on the keypad and the ListView will automatically fi lter the items to match what was typed:
lstView.setTextFilterEnabled(true);
Figure 3 shows the list filtering in action. Here, all items in the list that contain the word “golden” will appear in the result list.

Figure 3: The ListView view  with filtering action


Demikianlah Artikel List Views

Sekianlah artikel List Views kali ini, mudah-mudahan bisa memberi manfaat untuk anda semua. baiklah, sampai jumpa di postingan artikel lainnya.

Anda sekarang membaca artikel List Views dengan alamat link https://googleandroiddevelopertutorial.blogspot.com/2012/09/list-views.html

Artikel Terkait

List Views
4/ 5
Oleh

Berlangganan

Suka dengan artikel di atas? Silakan berlangganan gratis via email