menampilkan data dengan ListView sederhana di Android

Alhamdulillah emak udah keluar RS. senin kemarin dan udah kontrol juga.
fiuhh…

Hahaha seminggu ini bener-bener dah rasanya nano-nano πŸ˜€ , bikin ga bisa galau lagi pokoknya. Pengen cerita tapi males sih (uppss bilang aje mo galau). Kali ini kita coba bikin aplikasi android aja yuk.
Sebenernya males euy ngoding JAVA, bikin laptop ng’lag mulu πŸ˜€ (out of memory) hehehe padahal seru nih, terus kan bisa pamer tuh nti ama temen, weee gua bisa bikin aplikasi android sendiri dong(hahaha pngalaman pribadi).

Okeh langsung aja yuk, kita akan buat aplikasi android sederhana untuk menampilkan array data lewat ListView-nya android. Mau nyoba yang dari database langsung sih tapi masih bingung euy, coba yang ini aja dululah.

1. Bikin project baru di eclipse(tau lahh caranya, yaa kan??), ubah MainActivity.java jadi kek gini :

package com.listview;

import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import android.support.v7.app.ActionBarActivity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        ListView list = (ListView) findViewById(R.id.listView1);
        
        String[] item = {
        		"Milk",
        		"Butter",
        		"Yougurt",
        		"Tea",
        		"Coffee"
        };
        
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, item);
        
        list.setAdapter(adapter);
        
        list.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				Object detail = parent.getItemAtPosition(position);
				String tampil = detail.toString();
				Toast.makeText(getApplicationContext(), "Ini adalah " + tampil, Toast.LENGTH_SHORT).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;
        }
        return super.onOptionsItemSelected(item);
    }
}

2. Edit main.xml jadi kek gini :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    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.listview.MainActivity" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_toRightOf="@+id/button1" >

    </ListView>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignTop="@+id/listView1"
        android:text="Button" />

</RelativeLayout>

3. Klik Run As deh.

Hehehe gimana?? bisa?? πŸ˜€
Lanjut browsing lagi ahhh buat bahan nanti, bye.

Advertisements
This entry was posted in android and tagged , , . Bookmark the permalink.

One Response to menampilkan data dengan ListView sederhana di Android

  1. Nia says:

    Kak Hrydi, boleh ndak setiap code di jelasin biar paham maksudnya dari code yang telah dibuat..
    Makasih Ilmunya sudah disharing Kak..

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s