Menampilkan Data ke listView dari EditText
Langkah awal adalah buat Activity Main nya........
MainActivity .xml
<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="930dp"
tools:context=".MainActivity" >
<TextView
android:id="@+id/row_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="12dp"
android:layout_y="8dp"
android:text="Id Peserta"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/Id_txt"
android:layout_width="274dp"
android:layout_height="wrap_content"
android:layout_x="12dp"
android:layout_y="32dp"
android:ems="10"
android:hint="Id Peserta"
android:inputType="textPersonName"
android:textColorHint="#33B5E5" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/row_nama"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="14dp"
android:layout_y="83dp"
android:text="Nama"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/nama_txt"
android:layout_width="280dp"
android:layout_height="wrap_content"
android:layout_x="11dp"
android:layout_y="108dp"
android:ems="10"
android:hint="Nama Peserta"
android:inputType="textPersonName"
android:textColorHint="#33B5E5" />
<ListView
android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="370dp"
android:layout_x="-1dp"
android:layout_y="342dp" >
</ListView>
<TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="14dp"
android:layout_y="158dp"
android:text="Alamat"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/alamat_txt"
android:layout_width="287dp"
android:layout_height="wrap_content"
android:layout_x="10dp"
android:layout_y="182dp"
android:ems="10"
android:hint="Alamat"
android:inputType="textPersonName"
android:textColorHint="#33B5E5" />
<Button
android:id="@+id/ok"
android:layout_width="241dp"
android:layout_height="45dp"
android:layout_x="42dp"
android:layout_y="239dp"
android:background="#33B5E5"
android:text="Submit"
android:textColor="#ffffff"
android:textSize="20sp" />
<Button
android:id="@+id/Header"
android:layout_width="match_parent"
android:layout_height="32dp"
android:layout_x="2dp"
android:layout_y="298dp"
android:background="#FF4444"
android:text="ID Nama Unit"
android:textColor="#ffffff"
android:textSize="16sp" />
</AbsoluteLayout>
Setelah itu Row.xml
<?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" >
<TextView
android:id="@+id/row_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="9dp"
android:layout_y="17dp"
android:text="id"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/row_nama"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="126dp"
android:layout_y="17dp"
android:text="nama"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/row_alamat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="241dp"
android:layout_y="16dp"
android:text="alamat"
android:textAppearance="?android:attr/textAppearanceMedium" />
</AbsoluteLayout>
Setelah itu MainActivity.java
package com.example.box_list;
//@author Th3_JCr4ck3r aka matt
import java.util.ArrayList;
import java.util.HashMap;
import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.view.Menu;
import android.view.View;
import android.widget.*;
public class MainActivity extends ListActivity
{
Button ok;
EditText id,nama,alamat;
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ok=(Button)findViewById(R.id.ok);
id=(EditText)findViewById(R.id.Id_txt);
nama=(EditText)findViewById(R.id.nama_txt);
alamat=(EditText)findViewById(R.id.alamat_txt);
ok.setOnClickListener(submit);
}
View.OnClickListener submit = new View.OnClickListener()
{
@Override
public void onClick(View arg0)
{
isiTab();
}
};
void isiTab()
{
map.put("id",id.getText().toString());
map.put("nama",nama.getText().toString());
map.put("alamat",alamat.getText().toString());
mylist.add(map);
ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.row,
new String[] { "id", "nama","alamat" },
new int[] { R.id.row_id,R.id.row_nama, R.id.row_alamat});
setListAdapter(adapter);
}
}
Komentar
Posting Komentar