728x90
안드로이드 스튜디오로 전화번호부 만들기
코드
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="이름" />
<EditText
android:id="@+id/num"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="전화번호"
android:inputType="number" />
<Button
android:id="@+id/btnAdd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="삽입"
android:textSize="15dp" />
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/nameItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#0000ff"
android:textSize="20dp" />
<TextView
android:id="@+id/numItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/nameItem"
android:layout_alignLeft="@+id/nameItem"
android:textSize="15dp" />
</RelativeLayout>
MainActivity.java
package com.cookandroid.test2;
import java.util.ArrayList;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private ArrayList<String> nameList = new ArrayList<>();
private ArrayList<String> numList = new ArrayList<>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("간단 전화번호부");
ListView list = findViewById(R.id.listView);
CustomAdapter adapter = new CustomAdapter(this);
list.setAdapter(adapter);
final EditText name = findViewById(R.id.name);
final EditText num = findViewById(R.id.num);
Button btnAdd = findViewById(R.id.btnAdd);
btnAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
nameList.add(name.getText().toString());
numList.add(num.getText().toString());
adapter.notifyDataSetChanged();
}
});
list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
nameList.remove(position);
numList.remove(position);
adapter.notifyDataSetChanged();
return false;
}
});
setOnItemLongClickListener를 넣어서 꾹 누르면 지워지게 만듦
}
private class CustomAdapter extends BaseAdapter {
Context context;
public CustomAdapter(Context context) {
this.context = context;
}
@Override
public int getCount() {
return nameList.size();
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = View.inflate(context, R.layout.item, null);
TextView nameItem = itemView.findViewById(R.id.nameItem);
TextView numItem = itemView.findViewById(R.id.numItem);
nameItem.setText(nameList.get(position));
numItem.setText(numList.get(position));
return itemView;
}
}
}
커스텀 어댑터 사용
결과
728x90
'개발 > Android' 카테고리의 다른 글
[Android Studio] 간단 그림판 만들기 (0) | 2021.11.17 |
---|---|
[Android Studio] Layout 종류 (0) | 2021.11.16 |
[Android Studio] 계산기 만들기 (2) | 2021.10.07 |