在Firebase中删除数据后如何更新列表视图?

时间:2018-11-10 10:35:40

标签: android listview

这是我的TimeTableActivity.java,它显示项目的列表视图(个人的日程表)

package com.example.jongha.meetingorganizer;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ListView;

import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

import java.util.ArrayList;
import java.util.List;

public class TimeTableActivity extends AppCompatActivity {

private FloatingActionButton addScheduleBtn;

DatabaseReference dref;
ListView listview;


@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_timetable);

    //listview 연결, adapter 생성과 연결, dref연결
    listview=(ListView)findViewById(R.id.schedule_list_view);
    final ScheduleListViewAdapter adapter = new ScheduleListViewAdapter();
    listview.setAdapter(adapter);
    dref = FirebaseDatabase.getInstance().getReference("timetables/test1999");
    //dref change to path of logged-in user하기


    //realtime database 데이터 변화에 대한 반응
    dref.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
            adapter.addItem(dataSnapshot.getValue(ScheduleDTO.class));
            adapter.notifyDataSetChanged();
        }

        @Override
        public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

        }

        @Override
        public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
            adapter.removeItem(dataSnapshot.getValue(ScheduleDTO.class));
            adapter.notifyDataSetChanged();
        }

        @Override
        public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });


    //listview의 스케쥴 각각에 대한 onclicklistner

    listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            ScheduleDTO item = (ScheduleDTO) parent.getItemAtPosition(position);

            Intent intent = new Intent(TimeTableActivity.this, DisplayScheduleActivity.class);
            String[] infoOfSchedule = new String[] {item.getActivityName(), item.getStartHour(), item.getStartMin(), item.getEndHour(), item.getEndMin(), item.getDayOfWeek()};
            intent.putExtra("dataStrings", infoOfSchedule);
            startActivity(intent);
        }
    });


    //스케쥴 추가 버튼 onclicklistner
    addScheduleBtn = findViewById(R.id.add_schedule_btn);
    addScheduleBtn.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            startActivity(new Intent(TimeTableActivity.this, NewScheduleActivity.class));
        }
    });
}
}

这是我的自定义列表视图适配器ScheduleListViewAdapter.java

package com.example.jongha.meetingorganizer;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;

public class ScheduleListViewAdapter extends BaseAdapter {


private ArrayList<ScheduleDTO> listViewItemList = new ArrayList<ScheduleDTO>();

//ScheduleListViewAdapter 생성
public ScheduleListViewAdapter() {
}

@Override
public int getCount() {
    return listViewItemList.size();
}

@Override
public Object getItem(int position) {
    return listViewItemList.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final int pos = position;
    final Context context = parent.getContext();

    // "layout_schedule_listview.xml" Layout을 inflate하여 convertView 참조 획득.
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.layout_schedule_listview, parent, false);
    }

    // 화면에 표시될 View(Layout이 inflate된)으로부터 위젯에 대한 참조 획득
    TextView titleTextView = (TextView) convertView.findViewById(R.id.scheduleListNameView) ;
    TextView timeTextView = (TextView) convertView.findViewById(R.id.scheduleListTimeView) ;

    // Data Set(listViewItemList)에서 position에 위치한 데이터 참조 획득
    ScheduleDTO listViewItem = listViewItemList.get(position);

    // 아이템 내 각 위젯에 데이터 반영
    titleTextView.setText(listViewItem.getActivityName());
    timeTextView.setText(listViewItem.getListViewTime());

    return convertView;
}

public void addItem(ScheduleDTO gotItem) {
    listViewItemList.add(gotItem);
}

public void removeItem(ScheduleDTO gotItem){
    listViewItemList.remove(gotItem);
}
}

我的问题是,尽管在其他活动中调用的删除功能正在正确运行(在Firebase控制台中删除数据),并且在完成TimeTableActivity之后再次调用TimeTableActivity之后,listview得到了很好的更新,但是之后删除项目。

实际上,起初我在TimetableActivity.java中有一个错字。 我打过 在onChlidRemoved方法中,使用adapter.addItem(....)而不是adapter.removeItem(....)。 在这种情况下,listview已更新,其中包含两个相同的项目。 例如,如果我有项目A并被删除,则列表视图会立即更新并显示两个A。 但是,更改为adapter.removeItem(...)后,删除项后不会立即更新listview。

我应该如何解决此问题?

0 个答案:

没有答案