Adapter가 data를 가져와서 getview() method를 통해 Adapter View형태로 보여준다
Adapter의 종류에는 ArrayAdapter, SimpleAdapter, cursorAdapter가 있다
Adapter View의 종류에는 ListView, GridView, Spinner, Gallery등이 있다
activity_main.xml
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
MainActivity.java
public class MainActivity extends AppCompatActivity {
ArrayList<String> arrayList = null;
ArrayAdapter<String> adapter = null;
ListView listView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
arrayList = new ArrayList<String>();
arrayList.add("jusung");
arrayList.add("aaaaa");
arrayList.add("bbbbb");
arrayList.add("ccccc");
arrayList.add("ddddd");
arrayList.add("eeeee");
arrayList.add("fffff");
arrayList.add("ggggg");
arrayList.add("hhhhh");
arrayList.add("iiiii");
arrayList.add("jjjjjj");
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayList);
listView = findViewById(R.id.listView);
listView.setAdapter(adapter);
//adapter를 통해 화면에 보여준다
}
}
---seperate data---
activity_main.xml
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
MainActivity.java
public class MainActivity extends AppCompatActivity {
ArrayAdapter<CharSequence> arrayAdapter = null;
ListView listView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//createFromResource() method를 이용해 resource로부터 adapter를 생성
arrayAdapter = ArrayAdapter.createFromResource(this, R.array.contact, android.R.layout.simple_list_item_checked);
//ctr+space bar를 눌러서 다른 목록을 선택할수있다.
listView = findViewById(R.id.listView); //listView를 가져온다
listView.setAdapter(arrayAdapter);
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
//listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
//multiple일때 여러개 선택가능하다
//ColorDrawable객체를 이용해 구분선에 색을추가할수있다
ColorDrawable colorDrawable = new ColorDrawable(Color.BLUE);
listView.setDivider(colorDrawable);
listView.setDividerHeight(5);
}
}
data.xml
//res\values\data.xml
<resources>
<string-array name="contact">
<item>fffff 010010010</item>
<item>eeeee 010010010</item>
<item>ddddd 010010010</item>
<item>ccccc 010010010</item>
<item>bbbbb 010010010</item>
<item>aaaaa 010010010</item>
</string-array>
</resources>
---listActivity---
MainActivity.java
public class MainActivity extends ListActivity {
//ListActivity를 상속받는다
ArrayList<String> data = null;
ArrayAdapter<String> adapter = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
//ListActivity에 listView가 포함되있다
data = new ArrayList<String>();
data.add("aaaaa");
data.add("bbbbb");
data.add("ccccc");
data.add("ddddd");
data.add("eeeee");
data.add("fffff");
data.add("ggggg");
data.add("hhhhh");
data.add("iiiii");
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data);
setListAdapter(adapter);
//ListActivity에 setListAdapter method를 이용해 adapter를 추가시킨다
}
}
---listActivity---
MainActivity.java
public class MainActivity extends ListActivity {
//ListActivity를 상속받는다
String[] laughing = {"haha","hoho","hihi","hehe","huhu","hwhw"};
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
//ListActivity에 listView가 포함되있다
adapter = new ArrayAdapter<String>(this, R.layout.activity_list_style,
R.id.laugh, laughing);
//직접만든 activity_list_style로 지정한다
//actvity_list_style에 만든 text view의 id값에 data laughing를 입력한다
setListAdapter(adapter);
//ListActivity에 setListAdapter method를 이용해 adapter를 추가시킨다
}
}
activity_list_style.xml
//res\layout에 activity_list_style.xml를 만든다
<ImageView
android:id="@+id/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher_foreground"/>
<TextView
android:id="@+id/laugh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:paddingTop="50dp"/>
'android studio' 카테고리의 다른 글
GridView (0) | 2019.08.11 |
---|---|
Spinner (0) | 2019.08.10 |
view에 도형, 텍스트 그리기 (0) | 2019.08.10 |
Button, RadioButton, CheckBox, ImageView, ImageButton (0) | 2019.08.10 |
text view (0) | 2019.08.09 |