如何删除项目时如何保持列表视图项目不在移动位置? - Android Studio

时间:2018-02-27 04:34:02

标签: android listview

所以我的问题是当我的一个项目从列表视图中删除时,其余的项目不再处于正确的位置。例如,当移除位置0时,位置1中的项目移动到位置0.导致它打开活动而不是吐司。如果有人有任何简单的解决方案,那就太棒了。谢谢!

if (getArguments().getInt(ARG_SECTION_NUMBER) == 1) {

 View view = inflater.inflate(R.layout.fragment_kendall_house, container, false);
 final ListView listView = view.findViewById(R.id.kendallBarList);


 final String[] barChallenges = new String[] {
  "Stand in the yard",
  "Look Up",
  "Eat Snow"
 };



 final List < String > barChallengesList = new ArrayList < String > (Arrays.asList(barChallenges));


 final ArrayAdapter < String > listViewAdapter = new ArrayAdapter < String >
  (getActivity(), android.R.layout.simple_list_item_1, barChallengesList);

 listView.setAdapter(listViewAdapter);

 mDatabaseRef.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {

      if (dataSnapshot.child("kendallHouseCh1").exists()) {


       barChallengesList.remove("Stand in the yard");

       listViewAdapter.notifyDataSetChanged();


       listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView < ? > parent, View view, int position, long id) {



         if (position == 0) {
          Intent intent = new Intent(getActivity(), kendallHouseCH1.class);
          startActivity(intent);


         } else if (position == 1) {
          Toast.makeText(getActivity(), "Gross you ate it", Toast.LENGTH_SHORT).show();


         } else if (position == 2) {
          Toast.makeText(getActivity(), "What do you see?", Toast.LENGTH_SHORT).show();


         }
        }
       });

0 个答案:

没有答案