728x90
반응형
💡Adapter 생성 - getView() 오버라이딩
◾ 주어진 인덱스(position)에서의 뷰를 지역변수 view에 저장한다.
View view = converView;
◾ view가 null이면, row,xml파일을 인플레이트 한다.
if(view == null) { view = inflater.inflate(R.layout.row, null); }
◾ view에서 이미지뷰를 찾아 인덱스인 position에 따라 이미지 변경한다.
ImageView iv = (ImageView)view.findViewById(R.id.iv); int nResId = R.drawable.heart; if(position%2 == 0) { nResId = R.drawable.star; }
◾ view에서 텍스트뷰를 찾는다.
TextView tv = (TextView)view.findViewById(R.id.tv);
◾ 문자열 배열의 텍스트를 텍스트뷰에 지정한다.
tv.setText(mItems[position]);
📂자료
📃아이콘과 텍스트뷰 같이 출력하기
📝row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/heart" />
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30dp" />
</LinearLayout>
📝layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/list"/>
</LinearLayout>
📝MainActivity.java
package com.example.linearview_test;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
String str_items[] = { "red", "blue", "green", "yellow"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
ArrayAdapter<String> aa = new ArrayAdapter<String>(this, R.layout.row, R.id.tv, str_items);
ListView lv = findViewById(R.id.list);
lv.setAdapter(aa);
}
}
📃하트 아이콘을 별 아이콘으로 바꾸는 응용 예제
📝row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/star" />
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30dp" />
</LinearLayout>
📝layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/list"/>
</LinearLayout>
📝MainActivity.java
package com.example.listview_icon;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
String str_items[] = {"red", "blue", "green", "yellow"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
ArrayAdapter<String> aa = new ArrayAdapter<String>(this, R.layout.row, R.id.tv, str_items);
ListView lv = findViewById(R.id.list);
lv.setAdapter(aa);
}
}
📃짝수는 별, 홀수는 하트 아이콘으로 표시하기
📝row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:padding="20px"
android:id="@+id/iv"
android:src="@drawable/heart"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="30dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/tv"/>
</LinearLayout>
📝layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/list"/>
</LinearLayout>
📝MainActivity.java
package com.example.adapter_test;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
String str_items[] = { "red", "blue", "green", "yellow"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
MyAdapter aa = new MyAdapter(this, R.layout.row, R.id.tv, str_items);
ListView lv = findViewById(R.id.list);
lv.setAdapter(aa);
}
}
class MyAdapter extends ArrayAdapter<String>{
LayoutInflater inflater;
String[] mItems;
public MyAdapter(Context context, int resId, int tvId, String[] objects) {
super(context, resId, tvId, objects);
mItems = objects;
inflater = LayoutInflater.from(context);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View view = convertView; // 주어진 인덱스(position)에서의 뷰를 지역변수 view에 저장
if(view == null) {
view = inflater.inflate(R.layout.row, null); // view가 null이면 row.xml파일을 인플레이트한다.
}
ImageView iv = view.findViewById(R.id.iv); // view에서 이미지뷰를 찾아
int nResId = R.drawable.heart; // 인덱스인 position에 따라
if(position % 2 == 0) { // 이미지 변경
nResId = R.drawable.star;
}
iv.setImageResource(nResId);
TextView tv = view.findViewById(R.id.tv); // view에서 텍스트뷰를 찾음
tv.setText(mItems[position]); // 문자열 배열의 텍스트를 텍스트뷰에 저장
return view;
}
}
📃새로운 Adapter 생성, item 내용 바꾸기
◾ ArrayAdapter를 상속받아 MyAdapter를 추가한다.
◾ MyAdapter의 멤버변수
LayoutInflator inflator; String[] mItems;
◾ 생성자에서 멤버변수 초기화
◾ getView 오버라이딩 - row.xml 사용
📝row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/heart" />
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30dp" />
</LinearLayout>
📝layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/list"/>
</LinearLayout>
📝MainActivity.java
package com.example.listview_icon;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import org.w3c.dom.Text;
public class MainActivity extends AppCompatActivity {
String str_items[] = {"red", "blue", "green", "yellow"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
MyAdapter aa = new MyAdapter(this, R.layout.row, R.id.tv, str_items);
ListView lv = findViewById(R.id.list);
lv.setAdapter(aa);
}
}
class MyAdapter extends ArrayAdapter<String> {
LayoutInflater inflater;
String[] mItems;
public MyAdapter(Context context, int resource, int textViewResourceId, String[] objects) {
super(context, resource, textViewResourceId, objects);
mItems = objects;
inflater = LayoutInflater.from(context);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View view = convertView; // 주어진 인덱스(position)에서의 뷰를 지역변수 view에 저장
String str[] = {"빨강", "파랑", "초록", "노랑"};
if(view == null) {
view = inflater.inflate(R.layout.row, null);
}
TextView tv = (TextView)view.findViewById(R.id.tv);
tv.setText(str[position]);
return view;
}
}
📃아이템을 누르면 textView에 적혀있는 text대로 배경색 지정하기
📝row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/heart" />
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30dp" />
</LinearLayout>
📝layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list"/>
</LinearLayout>
📝MainActivity.java
package com.example.listview_icon2;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
String str_items[] = {"red", "blue", "green", "yellow"};
int color[] = {Color.RED, Color.BLUE, Color.GREEN, Color.YELLOW};
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
MyAdapter ma = new MyAdapter(this, R.layout.row, R.id.tv, str_items);
lv = findViewById(R.id.list);
lv.setAdapter(ma);
lv.setOnItemClickListener(lis);
}
AdapterView.OnItemClickListener lis = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
view.setBackgroundColor(color[position]);
}
};
}
class MyAdapter extends ArrayAdapter<String> {
LayoutInflater inflater;
String[] mItems;
public MyAdapter(Context context, int res, int tvResId, String[] objects) {
super(context, res, tvResId, objects);
inflater = LayoutInflater.from(context);
mItems = objects;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View view = convertView;
if (view == null) {
view = inflater.inflate(R.layout.row, null);
}
TextView tv = (TextView) view.findViewById(R.id.tv);
tv.setText(mItems[position]);
return view;
}
}
📃버튼 누르면 동적으로 리스트 생성되도록 하기
📝row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/iv"
android:src="@drawable/heart"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30dp"
android:id="@+id/tv"/>
</LinearLayout>
📝layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="40dp"
android:text="버튼"
android:backgroundTint="#9999ff"
android:id="@+id/btn"/>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list"/>
</LinearLayout>
📝MainActivity.java
package com.example.listview_icon3;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ArrayList<String> al;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
al = new ArrayList<String>();
MyAdapter ma = new MyAdapter(this, R.layout.row, R.id.tv, al);
ListView lv = findViewById(R.id.list);
lv.setAdapter(ma);
findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
al.add("hello");
ma.notifyDataSetChanged();
}
});
}
}
class MyAdapter extends ArrayAdapter<String> {
LayoutInflater inflater;
ArrayList<String> mItems;
public MyAdapter(Context context, int resId, int tvId, ArrayList objects) {
super(context, resId, tvId, objects);
mItems = objects;
inflater = LayoutInflater.from(context);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView,
@NonNull ViewGroup parent) {
View view = convertView; //① 주어진 인덱스(position)에서의 뷰를 지역변수 view에 저장
if(view == null){ // ② view가 null 이면
view = inflater.inflate(R.layout.row, null); // ③ row.xml 파일을 인플레이트한다
}
ImageView iv = view.findViewById(R.id.iv); // ④ view에서 이미지뷰를 찾아
int nResId = R.drawable.heart; // ⑤ 인덱스인 position에 따라
if(position%2==0){ // 이미지 변경
nResId = R.drawable.star;
}
iv.setImageResource(nResId);
TextView tv = (TextView)view.findViewById(R.id.tv); // ⑥ view에서 텍스트뷰를 찾음
tv.setText(mItems.get(position)); // ⑦ 문자열 배열의 텍스트를 텍스트뷰에 지정
return view;
}
}
📃argb 인자의 값을 난수를 발생시켜 버튼을 누를 때마다 배경색을 바꾸시오.
https://developer.android.com/reference/android/graphics/Color?hl=en
📝MainActivity.java
package com.example.listview_icon3;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
ListView lv = findViewById(R.id.list);
findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int r = (int)(Math.random()*256);
int g = (int)(Math.random()*256);
int b = (int)(Math.random()*256);
int result = Color.argb(125, r, g, b);
lv.setBackgroundColor(result);
}
});
}
}
📃OK버튼을 눌러 아이템에 이름을 추가할 때마다 아이템 배경 색을 난수로 지정하시오.
📝row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/heart"
android:id="@+id/iv"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30dp"
android:id="@+id/tv"/>
</LinearLayout>
📝layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp">
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="이름"
android:textSize="30dp" />
<EditText
android:id="@+id/et_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBaseline="@id/tv_name"
android:layout_toRightOf="@id/tv_name" />
<Button
android:id="@+id/btn_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/et_input"
android:layout_alignParentRight="true"
android:text="OK"
android:textSize="20dp" />
<Button
android:id="@+id/btn_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/et_input"
android:layout_marginRight="10dp"
android:layout_toLeftOf="@id/btn_ok"
android:text="CANCEL"
android:textSize="20dp" />
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list"
android:layout_below="@id/btn_cancel"/>
</RelativeLayout>
📝MainActiviy.java
package com.example.item_change_color;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ArrayList<String> al = new ArrayList<>();
MyAdapter ma;
EditText et;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
et = findViewById(R.id.et_input);
ma = new MyAdapter(this, R.layout.row, R.id.tv, al);
((ListView) findViewById(R.id.list)).setAdapter(ma);
findViewById(R.id.btn_ok).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
al.add("" + et.getText().toString());
et.setText("");
ma.notifyDataSetChanged();
}
});
}
}
📝MyAdapter.java
package com.example.item_change_color;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.ArrayList;
public class MyAdapter extends ArrayAdapter {
LayoutInflater inflater;
ArrayList<String> mItems;
int nTvId;
public MyAdapter(Context context, int resId, int tvId, ArrayList objects) {
super(context, resId, tvId, objects);
mItems = objects;
inflater = LayoutInflater.from(context);
nTvId = tvId;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView,
@NonNull ViewGroup parent) {
View view = convertView;
if (view == null) {
view = inflater.inflate(R.layout.row, null);
view.setBackgroundColor(getRandomColorId()); // null 상태일 때 배경색을 바꾼다.
}
TextView tv = (TextView) view.findViewById(nTvId);
tv.setText(mItems.get(position));
return view;
}
int getRandomColorId() {
int r = (int) (Math.random() * 256);
int g = (int) (Math.random() * 256);
int b = (int) (Math.random() * 256);
return Color.argb(125, r, g, b);
}
}
📃커스터마이징 어댑터, 리스트뷰 활용
◾ 교과목이 자바면 이미지뷰에 자바 사진과 이름을 출력하고, C언어면 C언어 사진과 이름을 출력한다.
📝row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/java"
android:id="@+id/iv"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv"
android:textSize="30dp"/>
</LinearLayout>
📝layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp">
<TextView
android:id="@+id/tv_subject"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="교과목"
android:textSize="30sp" />
<EditText
android:id="@+id/et_subject"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBaseline="@id/tv_subject"
android:layout_toRightOf="@id/tv_subject"
android:textSize="20sp" />
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_subject"
android:text="이름"
android:textSize="30sp" />
<EditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/et_subject"
android:layout_alignBaseline="@id/tv_name"
android:layout_toRightOf="@id/tv_subject"
android:textSize="20sp" />
<Button
android:id="@+id/btn_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/et_name"
android:layout_alignParentRight="true"
android:backgroundTint="#4599ff"
android:text="CANCEL"
android:textSize="20sp" />
<Button
android:id="@+id/btn_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/et_name"
android:layout_marginRight="10dp"
android:layout_toLeftOf="@id/btn_cancel"
android:backgroundTint="#9999ff"
android:text="OK"
android:textSize="20sp" />
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/btn_cancel" />
</RelativeLayout>
📝MainActivity.java
package com.example.customizing_adapter;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import java.lang.reflect.Array;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
ArrayList<String> al = new ArrayList<String>();
ArrayList<String> subAl = new ArrayList<String>();
ListView lv = findViewById(R.id.lv);
Button btn_ok = findViewById(R.id.btn_ok);
Button btn_cancel = findViewById(R.id.btn_cancel);
MyAdapter ma = new MyAdapter(this, R.layout.row, R.id.tv, al, subAl);
lv.setAdapter(ma);
btn_ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EditText et_name = findViewById(R.id.et_name);
EditText et_subject = findViewById(R.id.et_subject);
al.add(et_name.getText().toString());
subAl.add(et_subject.getText().toString());
et_subject.setText("");
et_name.setText("");
ma.notifyDataSetChanged();
}
});
}
}
📝MyAdapter.java
package com.example.customizing_adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.ArrayList;
public class MyAdapter extends ArrayAdapter {
LayoutInflater inflater;
ArrayList<String> mItems;
ArrayList<String> subItems;
int tvId;
public MyAdapter(@NonNull Context context, int res, int tvResId, ArrayList<String> objects, ArrayList<String> subjects) {
super(context, res, tvResId, objects);
inflater = LayoutInflater.from(context);
mItems = objects;
subItems = subjects;
tvId = tvResId;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View view = convertView;
if(view == null) {
view = inflater.inflate(R.layout.row, null);
}
ImageView iv = view.findViewById(R.id.iv);
int resIv = R.drawable.java;
if(subItems.get(position).equals("c")) {
resIv = R.drawable.c;
}
iv.setImageResource(resIv);
TextView tv = (TextView)view.findViewById(tvId);
tv.setText(mItems.get(position));
return view;
}
}
🔸 클래스로 만들기
📝Student.java
package com.example.registration2;
public class Student {
private String name;
private String subject;
public Student(String name, String subject){
this.name = name;
this.subject = subject;
}
String getName(){
return name;
}
String getSubject(){
return subject;
}
}
📝MainActivity.java
package com.example.registration2;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ArrayList<Student> al = new ArrayList<>();
MyAdapter ma;
EditText et;
EditText et_sub;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
et = findViewById(R.id.et_input);
et_sub = findViewById(R.id.et_subject);
ma = new MyAdapter(this, R.layout.row, R.id.listitem_tv, al);
((ListView)findViewById(R.id.list_names)).setAdapter(ma);
findViewById(R.id.btn_ok).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Student st = new Student(et.getText().toString(), et_sub.getText().toString());
al.add(st);
ma.notifyDataSetChanged();
et.setText("");
}
});
}
}
📝MyAdapter.java
package com.example.registration2;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.ArrayList;
public class MyAdapter extends ArrayAdapter {
LayoutInflater inflater;
ArrayList<Student> mItems;
int nTvId;
public MyAdapter(Context context, int resId, int tvId, ArrayList objects){
super(context, resId, tvId, objects);
mItems = objects;
inflater = LayoutInflater.from(context);
nTvId = tvId;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView,
@NonNull ViewGroup parent) {
View view = convertView;
if(view == null){
view = inflater.inflate(R.layout.row, null);
}
int resId = R.drawable.heart_imo;
if((mItems.get(position).getSubject()).equals("java")){
resId = R.drawable.star_imo;
}
((ImageView)view.findViewById(R.id.listeitem_iv)).setBackgroundResource(resId);
TextView tv = (TextView)view.findViewById(nTvId);
tv.setText(mItems.get(position).getName());
return view;
}
}
728x90
반응형
'App > Android Studio' 카테고리의 다른 글
Android Studio - 명시적 인텐트 (0) | 2022.08.08 |
---|---|
Android Studio - 액티비티 수명주기와 재실행 (0) | 2022.08.08 |
Android Studio - ImageView Shuffle 실습 (0) | 2022.08.04 |
Android Studio - 버튼 클릭 시 ImageView 동적 변경 (0) | 2022.08.04 |
Android Studio - ArrayList를 이용한 ListView 동적 생성 (0) | 2022.08.04 |
댓글