Android/Summary

ListView 를 생성하자.

gandus 2010. 7. 7. 15:13

package com.gandus.ListView;

 

import android.app.Activity;

import android.os.Bundle;

import android.widget.ListView;

import android.widget.EditText;

import java.util.ArrayList;

import android.widget.ArrayAdapter;

import android.view.*; // View객체를 사용하기 위해 import합니다.

import android.view.View.OnKeyListener; // 키 리스너를 사용하기 위해 import 합니다.

 

public class main extends Activity {

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

       

        ListView myListView = (ListView)findViewById(R.id.myListView);

        final EditText myEditText = (EditText)findViewById(R.id.myEditText);

       

        final ArrayList<String> todoItems = new ArrayList<String>(); // java

       

        final ArrayAdapter<String> aa; // android

        aa=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, todoItems);

       

        myListView.setAdapter(aa);

       

        myEditText.setOnKeyListener(new OnKeyListener(){ // (1)

         public boolean onKey(View v, int keyCode, KeyEvent event) { // (2)

          if(event.getAction()==KeyEvent.ACTION_DOWN)  // (3)

          {

           if(keyCode==KeyEvent.KEYCODE_DPAD_CENTER) {

                      

           todoItems.add(0, myEditText.getText().toString()); // (4)

           aa.notifyDataSetChanged(); // (5)

           myEditText.setText(""); //(6)

           return true;

           }

          }

          return false;

         }

        });

    }

}