我已经设法使用notifydatasetchanged不断刷新listview中的数据。但是,每次我添加一组新数据时,前一组数据总是会更改为当前数据。有没有办法在每次“notifydatasetchanged”后保留前一组数据?
这是我的密码:
private ArrayList<Model_inputs> inputsList;
boolean isActive = false;
private Adapter_inputs adapter_inputs;
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button refresh_btn = view.findViewById(R.id.refresh);
refresh_btn.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
if(!isActive) {
isActive = true;
Toast.makeText(getContext(), "Refreshing...", Toast.LENGTH_SHORT).show();
newInputs();
}
else if(isActive){
isActive = false;
Toast.makeText(getContext(), "Refreshing Stopped", Toast.LENGTH_SHORT).show();
}
}
});
inputsList = new ArrayList<>();
Model_inputs inputs = new Model_inputs("Text1", "Text2", "Text3", "Text4");
inputsList.add(inputs);
ListView listView = (ListView) getActivity().findViewById(R.id.InputValue_LV);
adapter_inputs = new Adapter_inputs(requireActivity(), R.layout.row_inputs, inputsList);
listView.setAdapter(adapter_inputs);
}
public void newInputs(){
inputsList.add(new Model_inputs("Text1", "Text2", "Text3", "Text4"));
adapter_inputs.notifyDataSetChanged();
if (isActive) {
refresh(1000);
}
}
public void refresh(int milliseconds){
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
newInputs();
}
}, milliseconds);
}
暂无答案!
目前还没有任何答案,快来回答吧!