Eclipse List View (ELV)


ELV:   SOLD       




What is a list view widget? With this widget, you can create a list containing many items. Two or more items can be selected simultaneously depending on the type of list view widget we use.


Figure 7.1. Selection widget 1

1. Run Enclipse, create a new Project


Figure 7.2. New Project

2. Fill in the parameters as follows

| Project name     | MengenalSelectionWidget  |
|------------------|--------------------------|
| Build Target     | Android 2.2              |
| Application name | Mengenal Slection Widget |
| Package name     | contoh.seleksi           |
| Create Activity  | seleksi                  |
| Min SDK version  | 8                        |

3. Then type the following script in main.xml

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent" >
 <TextView
 android:id="@+id/yangDipilih"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content" />
 <ListView
 android:id="@android:id/list"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:drawSelectorOnTop="false"
 />
 </LinearLayout>

4. Type the selection.java script as follows

package contoh.seleksi;

 import android.app.ListActivity;
 import android.os.Bundle;
 import android.view.View;
 import android.widget.ArrayAdapter;
 import android.widget.ListView;
 import android.widget.TextView;

 public class seleksi extends ListActivity {
 /** Called when the activity is first created. */
 TextView seleksi;
 String[] pilihan = {
 "Merbabu", "Merapi", "Lawu", "Rinjani",
 "Sumbing","Sindoro", "Krakatau", "Selat Sunda",
 "Selat Bali","Selat Malaka","Kalimantan",
 "Sulawesi", "Jawa" };
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
 setListAdapter(new ArrayAdapter<String>
 (this,android.R.layout.simple_list_item_1, pilihan));
 seleksi = (TextView) findViewById(R.id.yangDipilih);
 }

 public void onListItemClick(ListView parent, View v,
 int position, long id) {
 seleksi.setText(pilihan[position]);
 }
 }

5. If the script is messy, do Format (source > format).

6. Do a RUN and see the results.

Program Explanation

Okay, let's start from the main.xml layout. You just need to put a TextView (lines 7-10) to display whatever you click and a ListView (lines 11-15) to display whatever items are clickable. The two widgets above, are arranged in one LinearLayout.

We switch to the activity, line 12 is the object declaration section and in lines 13-17 we create an array type object of type string containing the names of mountains and straits in Indonesia. The setLIstAdapter() method in line 22 can be likened to a bridge between the ListView in the xml layout and the item array. In line 23, if simple_list_item_1 is replaced with simple_list_single_choice, the result looks like Figure 6.10(a), while if it is replaced with simple_list_ multiple_choice, the result looks like Figure 6.10(b).


Figure 7.3.(a) ListView with simple_list_single_choice, b) ListView with simple_list_multiple_choice


Post a Comment

Previous Next

نموذج الاتصال