我正在創建視圖來顯示事件。我正在從數據庫檢索記錄並根據記錄顯示事件。事件現在正在創建。如何刪除所有視圖並重新添加?
現在我想刪除記錄時刪除視圖。現在,如果我刪除了該記錄,那麼也會在片段中創建視圖。
我添加事件活動,我添加和刪除記錄。記錄在數據庫中被刪除,但我完成後仍能看到視圖();活動。
創建視圖:
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();
container = (RelativeLayout) eventView.findViewById(R.id.container);
TextView tvTitle = (TextView) eventView.findViewById(R.id.textViewTitle);
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);
removeView();
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++;
}
}
試圖刪除視圖:
public void removeView()
{
ArrayList<View> list = new ArrayList<View>();
for(int i=0; i<list.size(); i++)
{
dayplanView.removeAllViews();
}
}
dayplanview是ViewGroup
dayplanView = (ViewGroup) view.findViewById(R.id.hoursRelativeLayout);
我仍然可以看到該視圖。請問怎麼回事?
編輯:
例外:
了java.lang.RuntimeException:失敗遞送結果ResultInfo {誰= NULL,請求= 131073,結果= -1,數據=意向{CMP = com。示例。 siddhi.timetablelayout/.AddEventActivity(has extras)}} to activity {com.example.siddhi.timetablelayout/com.example.siddhi.timetablelayout.MainActivity}:java.lang.NullPointerException:試圖調用虛方法'void android.widget .RelativeLayout.removeAllViews()'null對象引用 02-05 08:02:37.871 29485-29485/com.example.siddhi.timetablelayout E/AndroidRuntime:at android.app.ActivityThread.deliverResults(ActivityThread.java:35 74)
at:parent.removeAllViews();
刪除arraylist和for循環。 – Keshav
爲什麼不使用'ListView'? –