我已經創建了動態視圖來顯示事件。我將這些事件存儲在數據庫中。如何刷新動態創建的視圖?
這些事件我從數據庫中檢索並加載到一個片段中。事件正在成功創建。
現在我想刪除事件。當我在另一個活動中刪除該事件時,該記錄將從數據庫中刪除,但事件仍顯示在碎片中。
我試着在viewgroup和view上使用invalidate(),但dint得到了任何結果。
我該如何刷新?
片段代碼:
public class Mon extends Fragment {
private FrameLayout fab;
private EventTableHelper mDb;
private Intent i;
private ViewGroup dayplanView;
private int minutesFrom,minutesTo;
private List<EventData> events;
private EventData e;
private LayoutInflater inflater;
public boolean editMode;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_mon, container, false);
fab = (FrameLayout) view.findViewById(R.id.main_fab);
ImageButton imageButton = (ImageButton) view.findViewById(R.id.imgbtn_fab);
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
i = new Intent(getActivity(), AddEventActivity.class);
editMode = false;
i.putExtra("EditMode",editMode);
startActivityForResult(i, 1);
}
});
dayplanView = (ViewGroup) view.findViewById(R.id.hoursRelativeLayout);
showEvents();
return view;
}
private void createEvent(LayoutInflater inflater, ViewGroup dayplanView, int fromMinutes, int toMinutes, String title,String location,final int id) {
final View eventView = inflater.inflate(R.layout.event_view, dayplanView, false);
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) eventView.getLayoutParams();
RelativeLayout container = (RelativeLayout) eventView.findViewById(R.id.container);
TextView tvTitle = (TextView) eventView.findViewById(R.id.textViewTitle);
dayplanView.invalidate();
if (tvTitle.getParent() != null)
((ViewGroup) tvTitle.getParent()).removeView(tvTitle);
if(location.equals(""))
{
tvTitle.setText("Event : " + title);
}
else
{
tvTitle.setText("Event : " + title + " (At : " + location +")");
}
int distance = (toMinutes - fromMinutes);
layoutParams.topMargin = dpToPixels(fromMinutes + 9);
layoutParams.height = dpToPixels(distance);
eventView.setLayoutParams(layoutParams);
dayplanView.addView(eventView);
container.addView(tvTitle);
// eventView.setTag(tag);
eventView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
i = new Intent(getActivity(),AddEventActivity.class);
editMode = true;
i.putExtra("EditMode",editMode);
int tag = 0;
// tag =(int)v.getTag();
// i.putExtra("tag",tag);
// EventData e =new EventData();
// int id=0;/ id = e.getId();
i.putExtra("id",id);
startActivityForResult(i,1);
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
showEvents();
}
public void showEvents()
{
mDb = new EventTableHelper(getActivity());
events = mDb.getAllEvents("Mon");
int tag = 0;
for (EventData eventData : events) {
int id = eventData.getId();
String datefrom = eventData.getFromDate();
if (datefrom != null) {
String[] times = datefrom.substring(11, 16).split(":");
minutesFrom = Integer.parseInt(times[0]) * 60 + Integer.parseInt(times[1]);
}
String title = eventData.getTitle();
String location = eventData.getLocation();
String dateTo = eventData.getToDate();
if (dateTo != null) {
//times = dateTo.substring(11,16).split(":");
String[] times1 = dateTo.substring(11, 16).split(":");
minutesTo = Integer.parseInt(times1[0]) * 60 + Integer.parseInt(times1[1]);
}
createEvent(inflater, dayplanView, minutesFrom, minutesTo, title, location, id);
tag++;
id++;
}
}
private int dpToPixels(int dp) {
return (int) (dp * getResources().getDisplayMetrics().density);
}
}
編輯:在另一個活動
刪除按鈕的代碼。
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
i = new Intent();
db.deleteEvent(eventData);
Log.i("d", "delete");
setResult(RESULT_OK, i);
finish();
}
});
謝謝。
你確定條目正在從數據庫中刪除嗎? 你可以檢查你的數據庫條目嗎? – vishalk
是的,我正在檢查日誌。它不會顯示ny值。但它顯示了創建的事件。 – user5881997
當我關閉應用程序並再次打開時,它不會顯示任何事件,因爲數據庫沒有任何價值。但是當我按下刪除來分割它仍然顯示事件。我將更新有問題的刪除代碼。 – user5881997