Assalamualaiqum ☺
Membuat ListView Menggunakan Android ADT dan Eclipse-Pada artikel ini penulis akan membahas cara membuat listview sederhana atau list view (daftar isi) dasar, list view ini banyak sekali digunakan pada proyek aplikasi mobile android apakah sebagai view data atau menu atau aplikasi crud android, berikut dibawah ini adalah langkah-langkah pembuatan listview menggunakan Android adt dan eclipse.
- Buat project baru.
- Isikan source code pada file activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.testapi.MainActivity" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
- Source code main.xml
<menu 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"
tools:context="com.example.testapi.MainActivity" >
<item
android:id="@+id/action_share"
android:orderInCategory="100"
android:title="@string/action_share"
app:showAsAction="never"/>
<item
android:id="@+id/action_help"
android:orderInCategory="100"
android:title="@string/action_help"
app:showAsAction="never"/>
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never"/>
<item
android:id="@+id/action_exit"
android:orderInCategory="100"
android:title="@string/action_exit"
app:showAsAction="never"/>
</menu>
- string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">testapi</string>
<string name="hello_world">Hello world!</string>
<string name="action_share">Share</string>
<string name="action_help">Help</string>
<string name="action_settings">Settings</string>
<string name="action_exit">Exit</string>
</resources>
- MainActivity.java
package com.example.testapi;
//import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity {
protected ListView listView1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView1 = (ListView) findViewById(R.id.listView1);
String[] values = new String[] { "Android Lihat Daftar",
"Implementasi Adapter",
"Daftar Isi Pada Android",
"Membuat Daftar Isi Android",
"Contoh Aplikasi Android",
"Daftar Isi Kode Sumber",
"Daftar Isi Pada Array Adapter",
"Contoh Daftar Isi Android"
};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, values);
listView1.setAdapter(adapter);
listView1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
int itemsPosition = arg2;
String itemsValue = (String) listView1.getItemAtPosition(arg2);
Toast.makeText(getApplicationContext(), "Position :"+itemsPosition+" ListItem : " +itemsValue , Toast.LENGTH_LONG).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
} else if(id == R.id.action_share){
Toast.makeText(getApplicationContext(), "kunjungi http://facebook.com/ghazali.samudera", Toast.LENGTH_LONG).show();
} else if(id == R.id.action_help){
Toast.makeText(getApplicationContext(), "Maaf tool ini belum tersedia", Toast.LENGTH_LONG).show();
} else{
finish();
}
return super.onOptionsItemSelected(item);
}
}
- Jalankan program. Dan inilah hasil akhirnya:
Semoga Bermanfaat ☺
0 komentar:
Posting Komentar