我正在创建显示事件的视图。我正在从数据库中检索记录并根据记录显示事件。这些事件现在正在创建。
现在我想在删除记录时删除视图。现在,如果我删除记录,那么也会在片段中创建视图。
我添加了事件活动,我添加和删除了记录。记录在数据库中被删除了,我在看完之后可以看到视图();活动。
创建视图:
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 {who = null,request = 131073,result = -1,data = Intent {cmp = com.example.siddhi.timetablelayout / .AddEventActivity(has extras)}}传递给activity {com.example.siddhi.timetablelayout / com.example.siddhi.timetablelayout.MainActivity}:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'void android.widget.RelativeLayout.removeAllViews()' 02-05 08:02:37.871 29485-29485 / com.example.siddhi.timetablelayout E / AndroidRuntime:at android.app.ActivityThread.deliverResults(ActivityThread.java:3574)
at:parent.removeAllViews();
答案 0 :(得分:1)
你不需要做dayplanView.removeAllViews();在for循环中。从你的代码看起来好像甚至没有输入for循环,因为列表的大小是0.这就是你所需要的:
public void removeView()
{
dayplanView.removeAllViews();
}