Menampilkan data ke listview (no database)
Kali ini
postingan saya adalah tentang menampilkan data di listview, langkah awal adalah
buat project android dulu, min os ginger bread, setelah itu di
“MainActivity.java” ketikkan code berikut :
package com.example.listiseng;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Vector;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.util.Log;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends ListActivity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
data();
}
void data()
{
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
HashMap<String, String> map1 = new HashMap<String, String>();
HashMap<String, String> map2 = new HashMap<String, String>();
map.put("barang","rinso");
map.put("harga","23000");
map.put("unit","3");
map1.put("barang","Kit");
map1.put("harga","4000");
map1.put("unit","2");
map2.put("barang","kopi");
map2.put("harga","3000");
map2.put("unit","3");
mylist.add(map);
mylist.add(map1);
mylist.add(map2);
ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.row,
new String[] { "barang", "harga","unit" },
new int[] { R.id.barang,R.id.harga, R.id.unit});
setListAdapter(adapter);
}
}
Setelah
selesai, sekarang ketikan “XML code” pada “Activity_main.xml” di folder RES รจ layout sbb :
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/AbsoluteLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<ListView
android:id="@id/android:list"
android:layout_width="346dp"
android:layout_height="447dp"
android:layout_x="-6dp"
android:layout_y="32dp" >
</ListView>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="32dp"
android:layout_x="-1dp"
android:layout_y="0dp"
android:background="#FF4444"
android:text="Barang Harga Unit"
android:textColor="#ffffff"
android:textSize="16sp" />
</AbsoluteLayout>
Jika Sudah
selesai, buat file XML pada folder RES รจ layout sbb :
<?xml version="1.0"
encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/AbsoluteLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/liner" >
<TextView
android:id="@+id/barang"
android:layout_width="62dp"
android:layout_height="wrap_content"
android:layout_x="52dp"
android:layout_y="3dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#ffffff" />
<TextView
android:id="@+id/harga"
android:layout_width="62dp"
android:layout_height="wrap_content"
android:layout_x="158dp"
android:layout_y="3dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#ffffff" />
<TextView
android:id="@+id/unit"
android:layout_width="62dp"
android:layout_height="wrap_content"
android:layout_x="262dp"
android:layout_y="0dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#ffffff" />
</AbsoluteLayout>
Komentar
Posting Komentar