본문 바로가기

Programing (프로그래밍)/Android Studio (안드로이드 스튜디오)

안드로이드스튜디오 ListView 리스트뷰 ( AndroidStudio / List<String> / ArrayAdapter )

728x90
반응형

ListViewExample.z01
10.00MB

 

ListViewExample.zip
5.03MB

 

app-debug.apk
6.64MB

 

 

activity_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
 
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="ListViewExample"
        android:textAlignment="center" />
 
    <ListView
        android:id="@+id/lvw"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        />
 
</LinearLayout>
cs

 

MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.example.listviewexample;
 
import androidx.appcompat.app.AppCompatActivity;
 
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
 
import java.util.ArrayList;
import java.util.List;
 
public class MainActivity extends AppCompatActivity {
    private ListView lvw;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        lvw = (ListView) findViewById(R.id.lvw);
 
        List<String> lst =new ArrayList<>();
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, lst);
        lvw.setAdapter(adapter);
        lst.add("A");
        lst.add("B");
        lst.add("C");
        lst.add("A");
        lst.add("B");
        lst.add("C");
        lst.add("A");
        lst.add("B");
        lst.add("C");
        lst.add("A");
        lst.add("B");
        lst.add("C");
        adapter.notifyDataSetChanged();
    }
}
cs

 

728x90
반응형