gpt4 book ai didi

java - 如何删除所有 View 并重新添加?

转载 作者:行者123 更新时间:2023-12-01 10:28:31 26 4
gpt4 key购买 nike

我正在创建 View 来显示事件。我正在从数据库中检索记录并根据记录显示事件。现在正在创建事件。

现在我想在删除记录时删除 View 。现在,如果我删除记录,那么 View 也会在 fragment 中创建。

我添加了添加和删除记录的事件 Activity 。记录在数据库中被删除,完成后我仍然可以看到 View (); Activity 。

创建 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();

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++;
}

}

尝试删除 View :

  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);

我仍然可以看到风景。请问出了什么问题?

编辑:

异常(exception):

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: 在 android.app.ActivityThread.deliverResults(ActivityThread.java:3574)

at:parent.removeAllViews();

最佳答案

您不需要执行 dayplanView.removeAllViews();在 for 循环中。从您的代码看来,由于列表的大小为 0,因此甚至没有输入 for 循环。这就是您所需要的:

public void removeView()
{
dayplanView.removeAllViews();
}

关于java - 如何删除所有 View 并重新添加?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35225742/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com