我的片段包含主要活动中的recyclerview
Fragment.java
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View view = inflater.inflate(R.layout.activity_main, container, false);
initInstances(view);
setRetainInstance(true);
return view;
}
private void initInstances(View view) {
mRecyclerView = (RecyclerView) view.findViewById(R.id.dialogList);
mLayoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(mLayoutManager);
adapter = new MyAdapter(items);
mRecyclerView.setAdapter(mAdapter);
}
Run Code Online (Sandbox Code Playgroud)
MainActivity.java
在onCreate方法中:
myFragment = (MyFragment) getSupportFragmentManager().findFragmentByTag(MyFragment.TAG);
if (MyFragment == null) {
MyFragment = new MyFragment();
getSupportFragmentManager().beginTransaction().add(R.id.content_frame,
myFragment, MyFragment.TAG).commit();
}
Run Code Online (Sandbox Code Playgroud)
但是当方向改变时,我的RecyclerView会重新绘制.
我已尝试使用覆盖保存状态,onSaveInstanceState并且onRestoreInstanceState像这样如何防止自定义视图在屏幕方向更改时丢失状态或保存和恢复视图状态android,但对我无效
如何在方向改变时正确保存RecyclerView和适配器项目的状态?
解决方案: 我的片段使用一些方法从BaseFragment扩展: …
我正在尝试将XMLfile解析为mysqldb.这是xml
<categories>
<category id="28">Woman</category>
<category id="277" parentId="28">T-Shirts</category>
<category id="140" parentId="277">shorts</category>
</category>
Run Code Online (Sandbox Code Playgroud)
的.py
for category in categories:
for item in category.getElementsByTagName("category"):
category_name = item.childNodes[0].nodeValue.encode("utf-8")
category_id = int(item.getAttribute('id'))
category_parentId = item.getAttribute('parentId')
#connect etc
sqlFillCategories = "INSERT INTO categories(category_id, category_parentId, shop_id, category_name) VALUES ('"+category_id + "', '" + category_parentId + "', '" + TREND_BRANDS_SHOPID + "', '" + category_name + "');"
Run Code Online (Sandbox Code Playgroud)
错误:
Traceback (most recent call last):
File "E:/python1/fanswell/parse.py", line 23, in <module>
sqlFillCategories = "INSERT INTO categories(category_id, category_parentId, shop_id, category_name) VALUES ('"+category_id …Run Code Online (Sandbox Code Playgroud)