이 튜토리얼에서는 안드로이드 앱에서 ListView에 사용자 입력을 받아 동적으로 요소를 추가하는 방법을 단계별로 알아봅니다. EditText에 텍스트를 입력하고 버튼을 누르면 해당 값이 실시간으로 리스트뷰에 추가되는 간단한 예제입니다.
1단계: 새 프로젝트 생성
안드로이드 스튜디오(Android Studio)를 실행한 후, 메뉴에서 File → New Project를 선택하여 새 프로젝트를 생성합니다. 프로젝트 생성에 필요한 모든 세부 정보(프로젝트 이름, 패키지명, 최소 SDK 버전 등)를 입력하고 빈 액티비티(Empty Activity) 템플릿을 선택합니다.
2단계: 레이아웃 파일 작성 (res/layout/activity_main.xml)
메인 레이아웃 파일에 아래 코드를 추가합니다. 화면 상단에는 사용자가 값을 입력할 수 있는 EditText와 리스트에 항목을 추가하는 Button이 배치되고, 그 아래에 ListView가 위치합니다.
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <Button android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/editText1" android:layout_centerHorizontal="true" android:text="Add Values to listView" /> <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="26dp" android:ems="10" android:hint="Add elements listView" /> <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/button1" android:layout_centerHorizontal="true" > </ListView> </RelativeLayout>
3단계: MainActivity.java 작성
핵심 로직은 다음과 같습니다. 기본 배열 데이터를 ArrayList로 변환한 뒤 ArrayAdapter에 연결하고, 버튼 클릭 시 EditText의 값을 리스트에 추가한 후 notifyDataSetChanged()를 호출하여 화면을 갱신합니다.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
public class MainActivity extends Activity {
ListView listview;
Button addButton;
EditText GetValue;
String[] ListElements = new String[] {
"Android",
"PHP",
"Python",
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview = findViewById(R.id.listView1);
addButton = findViewById(R.id.button1);
GetValue = findViewById(R.id.editText1);
final List<String> ListElementsArrayList = new ArrayList<>(Arrays.asList(ListElements));
final ArrayAdapter<String> adapter = new ArrayAdapter<>
(MainActivity.this, android.R.layout.simple_list_item_1, ListElementsArrayList);
listview.setAdapter(adapter);
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ListElementsArrayList.add(GetValue.getText().toString());
adapter.notifyDataSetChanged();
}
});
}
}4단계: AndroidManifest.xml 확인
매니페스트 파일에는 MainActivity가 런처(LAUNCHER) 액티비티로 등록되어 있어야 합니다. 아래 코드를 참고하세요.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="https://schemas.android.com/apk/res/android" package="app.com.sample"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
앱 실행 및 결과 확인
이제 애플리케이션을 실행해 보겠습니다. 실제 안드로이드 기기를 컴퓨터에 USB로 연결했다고 가정합니다. 안드로이드 스튜디오에서 프로젝트의 액티비티 파일 중 하나를 연 뒤, 상단 툴바의 Run(실행) 아이콘을 클릭합니다. 목록에서 자신의 모바일 기기를 선택하면 앱이 설치되고 실행됩니다.
앱이 실행되면 초기 화면에 'Android', 'PHP', 'Python' 세 개의 기본 항목이 ListView에 표시됩니다.

상단의 EditText에 원하는 텍스트를 입력하고 'Add Values to listView' 버튼을 누르면, 입력한 값이 즉시 리스트 하단에 새 항목으로 추가되는 것을 확인할 수 있습니다.

핵심 포인트 정리
- ArrayList 사용: 고정 크기의 배열 대신 ArrayList를 사용해야 런타임에 항목을 추가할 수 있습니다.
- notifyDataSetChanged(): 데이터가 변경된 후 반드시 호출해야 어댑터가 변경 사항을 감지하고 ListView를 갱신합니다.
- ArrayAdapter: simple_list_item_1 레이아웃을 사용하면 별도의 커스텀 어댑터 없이 간단히 문자열 리스트를 표시할 수 있습니다.