Code ส่วนของ main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:id="@+id/LinearLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <ListView android:id="@+id/ListView01" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
Code ส่วนของ Java
package slayer.ListviewOnclickExample; import android.app.Activity; import android.app.AlertDialog; import android.os.Bundle; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.ListView; import android.view.View; public class ListviewOnclickExampleActivity extends Activity { private ListView lv1; private String lv_arr[] = { "Burapha", "Computer", "ZONE", "Galaxy", "MSCIM", "Petdo", "Slayer" }; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); lv1 = (ListView) findViewById(R.id.ListView01); lv1.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, lv_arr)); lv1.setTextFilterEnabled(true); lv1.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> a, View v, int position, long id) { AlertDialog.Builder adb = new AlertDialog.Builder( ListviewOnclickExampleActivity.this); adb.setTitle("LVSelectedItemExample"); adb.setMessage("Selected Item is = " + lv1.getItemAtPosition(position)); adb.setPositiveButton("Ok", null); adb.show(); } }); } }
ไม่มีความคิดเห็น:
แสดงความคิดเห็น