[英]file input stream need to read from the Object
我正在尝试将数据 onDestroy 方法保存在 bottomsheetDialogFragment 中。 我正在保存有关destroy 方法的信息并获取有关onCreate 方法的保存信息。
但我在这条特定线上遇到错误
listOfItem= new ArrayList<> (Arrays.asList ( split ) );
我正在使用来自 Item class 的 recylerView 添加列表,但我不知道如何将拆分字符串添加到 ArrayList 中。
希望你能理解我的问题。 下面是我的代码非常感谢
public class CustomFragment_1 extends BottomSheetDialogFragment {
private RecyclerView recyclerView;
private CustomAdapter_1 mAdapter;
ArrayList<Item> listOfItem;
String cName;
Button btn;
FlatDialog flatDialog;
EditText editText;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate ( R.layout.fragment_custom_1, container, false );
recyclerView = (RecyclerView) view.findViewById ( R.id.customRV );
btn = (Button) view.findViewById ( R.id.button1 );
editText = (EditText) view.findViewById ( R.id.edt1 );
LinearLayoutManager linearLayoutManager = new LinearLayoutManager ( getContext () );
recyclerView.setLayoutManager ( linearLayoutManager );
listOfItem = new ArrayList<> ();
mAdapter = new CustomAdapter_1 ( getContext (), listOfItem );
recyclerView.setAdapter ( mAdapter );
listOfItem.add ( new Item ( "FreeNow" ) );
listOfItem.add ( new Item ( "Uber" ) );
listOfItem.add ( new Item ( "Bolt" ) );
btn.setOnClickListener ( new View.OnClickListener () {
@Override
public void onClick(View view) {
flatDialog = new FlatDialog ( getActivity () );
flatDialog.setTitle ( "Company Name" )
.setSubtitle ( "Please add the name of the company" )
.setFirstTextFieldHint ( "Company Name" )
.setFirstButtonText ( "ADD" )
.setSecondButtonText ( "CANCEL" )
.withFirstButtonListner ( new View.OnClickListener () {
@Override
public void onClick(View view) {
cName = flatDialog.getFirstTextField ();
listOfItem.add ( new Item ( cName ) );
mAdapter.notifyItemInserted ( listOfItem.size () );
flatDialog.dismiss ();
}
} )
.withSecondButtonListner ( new View.OnClickListener () {
@Override
public void onClick(View view) {
flatDialog.dismiss ();
}
} )
.show ();
}
} );
loadContent ();
return view;
}
public void loadContent() {
File path = getActivity ().getFilesDir ();
File readFrom = new File ( path, "list.txt" );
byte[] content = new byte[(int) readFrom.length ()];
FileInputStream stream = null;
try {
stream = new FileInputStream ( readFrom );
stream.read ( content );
String s = new String ( content );
s = s.substring ( 1, s.length () - 1 );
String split[] = s.split ( ", " );
listOfItem = new ArrayList<> ( Arrays.asList ( split ) );
mAdapter = new CustomAdapter_1 ( getContext (), listOfItem );
recyclerView.setAdapter ( mAdapter );
} catch (Exception e) {
e.printStackTrace ();
}
}
@Override
public void onDestroy() {
File path = getContext ().getFilesDir ();
try {
FileOutputStream writer = new FileOutputStream ( new File ( path, "list.txt" ) );
writer.write ( listOfItem.toString ().getBytes () );
writer.close ();
} catch (Exception e) {
e.printStackTrace ();
}
super.onDestroy ();
}
}
下面是我得到的错误
error: incompatible types: cannot infer type arguments for ArrayList<>
listOfItem = new ArrayList<> ( Arrays.asList ( split ) );
^
reason: inference variable E has incompatible bounds
equality constraints: Item
lower bounds: T,String
where E,T are type-variables:
E extends Object declared in class ArrayList
T extends Object declared in method <T>asList(T...)
我将回避您的实际错误,因为您的代码存在多个问题,然后我会建议一种更好的方法来做到这一点。
首先,您应该知道 onDestroy() 并不总是被调用(例如,当应用程序被强制关闭时)。
其次,您尝试在 UI 线程上执行 IO,这将导致您的应用程序在读取和写入数据时都冻结。
您最好使用SharedPreferences之类的东西来存储您的listOfItem
数组。 并且您需要在数组更改时将其保存到SharedPreferences
,而不仅仅是 onDestroy()。 这样,即使未调用 onDestroy(),它也将始终是最新的。
最后,如果您坚持自己将对象写入文件存储,最好使用 object 序列化库,例如GSON 。 没有理由重新发明轮子。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.