memasukkan data ke listview


Buat project baru di android, dan buat activity baru, di Main Activity isi kode berikut :

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
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.view.View;
import android.view.View.OnClickListener;
import android.widget.*;

public class MainActivity extends ListActivity 
{
EditText text;
Button ok;
ArrayAdapter Ardapter;
Vector v = new Vector();
String isi;
List<String> data = new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text= (EditText) findViewById(R.id.inputSearch);
ok= (Button) findViewById(R.id.button1);
ListView lv = getListView();
   lv = getListView();
   Ardapter =new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data);
        lv.setAdapter(Ardapter);
   ok.setOnClickListener(new OnClickListener() 
{
       @Override
       public void onClick(View currentView)
       {
        String input = text.getText().toString();
           if(null!=input&&input.length()>0){     
           data.add(input);
           Ardapter.notifyDataSetChanged();
           text.setText("");
           }
   };
   
});
 
}
}

lalu untuk view, ActivityMain.xml 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="242dp"
        android:layout_weight="0.12"
        android:drawSelectorOnTop="false" />

    <TextView
        android:id="@id/android:empty"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="No data" />

    <EditText
        android:id="@+id/inputSearch"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:ems="10"
        android:hint="ketik di sini.."
        android:inputType="textVisiblePassword" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Ok" />

</LinearLayout>






Komentar

Postingan Populer