Сделал проверку на очистку дерева (и модели), сделал спиннер при загрузке.

This commit is contained in:
ChronosX88 2018-11-20 14:23:32 +04:00
parent 8c8927489d
commit 2d823ac11c

View File

@ -59,17 +59,19 @@ public class MainActivity extends AppCompatActivity
mSwipeRefreshLayout.setOnRefreshListener(MainActivity.this); mSwipeRefreshLayout.setOnRefreshListener(MainActivity.this);
mSwipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary); mSwipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary);
treeView = new AndroidTreeView(MainActivity.this, root);
treeView.setDefaultAnimation(true);
ViewGroup containerView = (ViewGroup) findViewById(R.id.treeViewContainer);
containerView.addView(treeView.getView());
try{ try{
refreshTree(); refreshTree(false);
} catch (IllegalStateException e) { } catch (IllegalStateException e) {
// //
} }
root.addChildren(firstLevelNodes);
treeView = new AndroidTreeView(MainActivity.this, root);
treeView.setDefaultAnimation(true);
ViewGroup containerView = (ViewGroup) findViewById(R.id.treeViewContainer);
containerView.addView(treeView.getView());
} }
@Override @Override
@ -111,10 +113,12 @@ public class MainActivity extends AppCompatActivity
return true; return true;
} }
private void fillFirstLevelNodes() { private void fillRootNode() {
for (int i = 0; i < firstLevelObjects.size(); i++) { for (int i = 0; i < firstLevelObjects.size(); i++) {
firstLevelNodes.add(new TreeNode(firstLevelObjects.get(i).getName())); firstLevelNodes.add(new TreeNode(firstLevelObjects.get(i).getName()));
} }
SwipeRefreshLayout mSwipeRefreshLayout = findViewById(R.id.swipeRefreshLayout);
mSwipeRefreshLayout.setRefreshing(false);
} }
@Override @Override
@ -123,7 +127,7 @@ public class MainActivity extends AppCompatActivity
mSwipeRefreshLayout.setRefreshing(true); mSwipeRefreshLayout.setRefreshing(true);
try { try {
refreshTree(); refreshTree(true);
} catch (IllegalStateException e) { } catch (IllegalStateException e) {
// //
} }
@ -137,11 +141,21 @@ public class MainActivity extends AppCompatActivity
}); });
} }
private void refreshTree() throws IllegalStateException { private void refreshTree(boolean clean) throws IllegalStateException {
for (int i = 0; i < firstLevelNodes.size(); i++) { SwipeRefreshLayout mSwipeRefreshLayout = findViewById(R.id.swipeRefreshLayout);
root.deleteChild(firstLevelNodes.get(i)); mSwipeRefreshLayout.setRefreshing(true);
firstLevelNodes.remove(i); if(clean) {
List<TreeNode> list = new ArrayList<>(root.getChildren());
for (int i = 0; i < list.size(); i++) {
root.deleteChild(list.get(i));
}
for (int i = 0; i < firstLevelNodes.size(); i++) {
firstLevelNodes.remove(i);
}
for (int i = 0; i < firstLevelObjects.size(); i++) {
firstLevelObjects.remove(i);
}
} }
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
@ -163,7 +177,7 @@ public class MainActivity extends AppCompatActivity
Toast.makeText(MainActivity.this, "Ответ от сервера неверен! Перепроверьте данные подключения!", Toast.LENGTH_LONG).show(); Toast.makeText(MainActivity.this, "Ответ от сервера неверен! Перепроверьте данные подключения!", Toast.LENGTH_LONG).show();
Log.e("ERROR", "Answer of server is wrong! Re-check your connection credentials! Error: " + response.message()); Log.e("ERROR", "Answer of server is wrong! Re-check your connection credentials! Error: " + response.message());
} }
fillFirstLevelNodes(); fillRootNode();
} else { } else {
Log.e("ERROR", "Что-то пошло не так..."); Log.e("ERROR", "Что-то пошло не так...");
} }