본문 바로가기

동방프로젝트

리스트뷰

package com.example.forseveralactivity;

import androidx.appcompat.app.AppCompatActivity;

import android.app.SearchManager;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

//직풀: 투표결과, 화면에서 가장 많은 표를 받은 그림과 제목을 보여줌
//result.xml(second.xml) ResultActivity.java만을 수정
//이미지파일의 id를 저장할 배열을 만들고, 레이팅바를 작게 변경
public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("암시적 인텐트 예제");

//arrayList형을 통해, 변수를 선언
/* 또는
final String[] mid={"히어로즈", "다이노스", "이글스"} string배열도 가능
*/
final ArrayList<String> midList=new ArrayList<String>();
ListView list=(ListView)findViewById(R.id.listView1);

//simple_list_item_single_choice: 라디오버튼형 리스트뷰
//multiple_choice: 체크박스형 리스트뷰
//다중선택 요구시, list.setChociMode(ListView.CHOICE_MODE_MULTIPLE)가 추가필요
final ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, midList);
list.setAdapter(adapter);

final EditText edtItem=(EditText)findViewById(R.id.edtItem);
Button btnAdd=(Button)findViewById(R.id.btnAdd);

btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
midList.add(edtItem.getText().toString());
adapter.notifyDataSetChanged();
//nodifyDataSetChanged(): 추가된 항목을 보이게 함
}
});

//짧게 클릭시, 해당 리스트 뷰 아이템의 이름을 보여줌줌
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView,
View view, int i, long l) {
Toast.makeText(getApplicationContext(), midList.indexOf(i),
Toast.LENGTH_SHORT).show();
}
});

//항목을 롱클릭시, remove()메소드로 항목이 삭제되도록 한다
list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView,
View view, int i, long l) {
midList.remove(i);
adapter.notifyDataSetChanged();
return false;
}
});
}
}

/////////////////

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edtItem"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnAdd"
android:text="항목 추가"/>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listView1"/>
</LinearLayout>

'동방프로젝트' 카테고리의 다른 글

스피너  (0) 2020.08.19
실습11-1)그리드뷰  (0) 2020.08.19
암시적 인텐트  (2) 2020.08.19
직풀 10-3  (2) 2020.08.19
실습 10-2, 직풀 10-2  (0) 2020.08.18