안녕하세요 앱에서 Edittext칸에 예를들어 우유를 입력하고 버튼을 눌렀을 때, 원래는 리스트뷰에 입력한 값이 떠야하지만 버튼을 누르면 입력한 값이 초기화가 되어 리스트뷰에 나타나지 않습니다.. 어떻게 하면 좋을까요 ㅠㅠㅠ 도와주시면 감사하겠습니다..!! 부탁드려요
- FragmentMemo.java -
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import androidx.fragment.app.Fragment;
import java.util.ArrayList;
public class FragmentMemo extends Fragment implements View.OnClickListener {
private ArrayList<String> items;
private ArrayAdapter<String> adapter;
private ListView listView;
public FragmentMemo() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_memo, container, false);
//데이터 준비
items = new ArrayList<String>();
// 어댑터 생성
adapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_single_choice, items);
// 어댑터 설정
listView = view.findViewById(R.id.listView);
listView.setAdapter(adapter);
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); // 하나의 항목만 선택할 수 있도록 설정
adapter.notifyDataSetChanged();
Button btnAdd = view.findViewById(R.id.btnAdd);
Button btnDelete = view.findViewById(R.id.btnDelete);
btnAdd.setOnClickListener(this);
btnDelete.setOnClickListener(this);
return view;
}
@SuppressLint("NonConstantResourceId")
@Override
public void onClick(View v) {
EditText ed = getActivity().findViewById(R.id.newitem);
switch (v.getId()) {
case R.id.btnAdd: // ADD 버튼 클릭시
String text = ed.getText().toString(); // EditText에 입력된 문자열값을 얻기
if (!text.isEmpty()) { // 입력된 text 문자열이 비어있지 않으면
items.add(text); // items 리스트에 입력된 문자열 추가
ed.setText(""); // EditText 입력란 초기화
adapter.notifyDataSetChanged(); // 리스트 목록 갱신
}
break;
case R.id.btnDelete: // DELETE 버튼 클릭시
int pos = listView.getCheckedItemPosition(); // 현재 선택된 항목의 첨자(위치값) 얻기
if (pos != ListView.INVALID_POSITION) { // 선택된 항목이 있으면
items.remove(pos); // items 리스트에서 해당 위치의 요소 제거
listView.clearChoices(); // 선택 해제
adapter.notifyDataSetChanged();
// 어답터와 연결된 원본데이터의 값이 변경된을 알려 리스트뷰 목록 갱신
}
break;
}
}
}
- fragment_memo.xml -
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/top_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".FragmentMemo">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/newitem"
android:focusable="true"
android:clickable="true"
android:focusableInTouchMode="true" />
<Button
android:text="ADD"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="@+id/btnAdd"
android:onClick="mOnclick"
tools:ignore="OnClick" />
<Button
android:text="DELETE"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="@+id/btnDelete"
android:onClick="mOnClick"
tools:ignore="OnClick" />
</LinearLayout>
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" />
</FrameLayout>
혹시 몰라 MainActivity.java도 올리겠습니다
public class MainActivity extends AppCompatActivity {
private final FragmentManager fragmentManager = getSupportFragmentManager();
private final FragmentCart fragmentCart = new FragmentCart();
private final FragmentMemo fragmentMemo = new FragmentMemo();
private final FragmentReceipt fragmentReceipt = new FragmentReceipt();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.frameLayout, fragmentCart).commitAllowingStateLoss();
BottomNavigationView bottomNavigationView = findViewById(R.id.navigationView);
bottomNavigationView.setOnNavigationItemSelectedListener(new ItemSelectedListener());
}
class ItemSelectedListener implements BottomNavigationView.OnNavigationItemSelectedListener{
@SuppressLint("NonConstantResourceId")
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
// 프래그먼트 사용을 위해
FragmentTransaction transaction = fragmentManager.beginTransaction();
switch(menuItem.getItemId())
{
// cart 아이콘 클릭 시 프래그먼트카트 호출
case R.id.cart:
transaction.replace(R.id.frameLayout, fragmentCart).commitAllowingStateLoss();
break;
// memo 아이콘 클릭 시 프래그먼트메모 호출
case R.id.memo:
transaction.replace(R.id.frameLayout, fragmentMemo).commitAllowingStateLoss();
break;
// receipt 아이콘 클릭 시 프래그먼트리십트 호출
case R.id.receipt:
transaction.replace(R.id.frameLayout, fragmentReceipt).commitAllowingStateLoss();
break;
}
return true;
}
}
}