Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.69 KB | None | 0 0
  1. package m.e;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Random;
  5.  
  6. import android.app.Activity;
  7. import android.app.AlertDialog;
  8. import android.content.DialogInterface;
  9. import android.graphics.Color;
  10. import android.os.Bundle;
  11. import android.os.Handler;
  12. import android.util.Log;
  13. import android.view.LayoutInflater;
  14. import android.view.View;
  15. import android.view.View.OnClickListener;
  16. import android.widget.Button;
  17. import android.widget.CheckBox;
  18. import android.widget.CompoundButton;
  19. import android.widget.ScrollView;
  20. import android.widget.TableLayout;
  21. import android.widget.TableRow;
  22. import android.widget.TextView;
  23. import android.widget.CompoundButton.OnCheckedChangeListener;
  24.  
  25. public class Main extends Activity implements Runnable {
  26. /** Called when the activity is first created. */
  27.  
  28. private TableLayout tL;
  29. private ScrollView sV;
  30. private Handler h;
  31. private Button Apagar;
  32. private ArrayList<Erros> array;
  33. private boolean isReallyChecked;
  34.  
  35. @Override
  36. public void onCreate(Bundle savedInstanceState) {
  37. super.onCreate(savedInstanceState);
  38. setContentView(R.layout.main);
  39. h = new Handler();
  40. h.post(this);
  41. array = new ArrayList<Erros>();
  42. // array.add("Pqp");
  43. // array.add("pqp2");
  44. // array.add("Pqp3");
  45. tL = (TableLayout) findViewById(R.id.widget44);
  46. sV = (ScrollView) findViewById(R.id.widget42);
  47. Apagar = (Button) findViewById(R.id.widget45);
  48. sV.setBackgroundColor(Color.LTGRAY);
  49.  
  50. Apagar.setOnClickListener(new OnClickListener(){
  51.  
  52. @Override
  53. public void onClick(View v) {
  54. // TODO Auto-generated method stub
  55. if (array.size() > 0){
  56. for(int a=array.size()-1;a>=0;--a){
  57. CheckBox check = (CheckBox)tL.getChildAt(a+1).findViewById(R.id.widget30);
  58. if (check.isChecked()){
  59. array.remove(a);
  60. }
  61. }
  62. updateUI();
  63. }
  64. }
  65.  
  66. });
  67.  
  68. updateUI();
  69. }
  70.  
  71. private void updateUI(){
  72. tL.removeAllViews();
  73. if(array.size() == 0){
  74. TableRow Row = new TableRow(this);
  75. TextView text = new TextView(this);
  76. text.setText("Não há nenhuma ocorrência de erro.");
  77. text.setTextColor(Color.BLACK);
  78. Row.addView(text);
  79. tL.addView(Row);
  80. } else {
  81. TableRow Row = new TableRow(this);
  82. CheckBox cBox = new CheckBox(this);
  83. if(isReallyChecked && allCheck()){
  84. cBox.setChecked(true);
  85. } else {
  86. cBox.setChecked(false);
  87. isReallyChecked = false;
  88.  
  89. }
  90. cBox.setText("(Selecionar Todos)");
  91. cBox.setTextColor(Color.BLACK);
  92. cBox.setOnCheckedChangeListener(new OnCheckedChangeListener(){
  93.  
  94. @Override
  95. public void onCheckedChanged(CompoundButton buttonView,
  96. boolean isChecked) {
  97. // TODO Auto-generated method stub
  98.  
  99. isReallyChecked=isChecked;
  100. for(int a=array.size()-1;a>=0;--a){
  101. CheckBox check = (CheckBox)tL.getChildAt(a+1).findViewById(R.id.widget30);
  102. check.setChecked(isChecked);
  103. // check.setId(a);
  104. // check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  105. //
  106. // @Override
  107. // public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  108. // // TODO Auto-generated method stub
  109. // array.get(buttonView.getId()).setChecked(isChecked);
  110. // cBox.setChecked(false);
  111. // isReallyChecked=false;
  112. // }
  113. // });
  114. array.get(a).setChecked(isChecked);
  115. }
  116. }
  117.  
  118. });
  119. Row.addView(cBox);
  120. tL.addView(Row);
  121. for(int a=0;a<array.size();a++){
  122. LayoutInflater inflater = (LayoutInflater) Main.this.getSystemService(LAYOUT_INFLATER_SERVICE);
  123. View layout = inflater.inflate(R.layout.template, null);
  124. CheckBox check = (CheckBox)layout.findViewById(R.id.widget30);
  125. check.setChecked(array.get(a).getChecked());
  126. // check.setId(a);
  127. // check.setOnCheckedChangeListener(new OnCheckedChangeListener(){
  128. //
  129. // @Override
  130. // public void onCheckedChanged(CompoundButton buttonView,
  131. // boolean isChecked) {
  132. // // TODO Auto-generated method stub
  133. // array.get(buttonView.getId()).setChecked(isChecked);
  134. // }
  135. //
  136. // });
  137. final TextView tView1 = (TextView) layout.findViewById(R.id.widget31);
  138. tView1.setText(array.get(a).NomeErro());
  139. TextView tView2 = (TextView) layout.findViewById(R.id.widget39);
  140. tView2.setText(array.get(a).numErros() + " erros.");
  141.  
  142. if(a%2==0){
  143. layout.setBackgroundColor(Color.DKGRAY);
  144. tView1.setTextColor(Color.WHITE);
  145. tView2.setTextColor(Color.WHITE);
  146. } else {
  147. layout.setBackgroundColor(Color.LTGRAY);
  148. tView1.setTextColor(Color.BLACK);
  149. tView2.setTextColor(Color.BLACK);
  150. }
  151.  
  152. Button b = (Button) layout.findViewById(R.id.widget40);
  153. b.setOnClickListener(new OnClickListener(){
  154.  
  155. @Override
  156. public void onClick(View v) {
  157. // TODO Auto-generated method stub
  158. LayoutInflater inflater = (LayoutInflater) Main.this.getSystemService(LAYOUT_INFLATER_SERVICE);
  159. View layout = inflater.inflate(R.layout.template_erros, null);
  160.  
  161. TableLayout privTL = (TableLayout) layout.findViewById(R.id.widget47);
  162.  
  163. privTL.setStretchAllColumns(true);
  164.  
  165. privTL.addView(newRow("Hora", "Tipo", "Duração", "Máximo"));
  166. for(int a=0;a<5;a++){
  167. privTL.addView(newRow(String.valueOf(a*100), "SUBTENSAO", String.valueOf(a*1000), String.valueOf(a*50)));
  168. }
  169.  
  170. AlertDialog.Builder dialog = new AlertDialog.Builder(Main.this);
  171. dialog.setTitle("Ocorrências de " + tView1.getText() + ":");
  172. dialog.setView(layout);
  173. dialog.setNeutralButton("Fechar", new DialogInterface.OnClickListener() {
  174.  
  175. @Override
  176. public void onClick(DialogInterface dialog, int which) {
  177. // TODO Auto-generated method stub
  178. dialog.cancel();
  179. }
  180. })
  181. .show();
  182. }
  183.  
  184. });
  185. tL.addView(layout);
  186. }
  187. }
  188. }
  189.  
  190. private TableRow newRow(String... values){
  191. TableRow tRow = new TableRow(Main.this);
  192.  
  193. for(int a=0;a<values.length;a++){
  194. TextView t = new TextView(Main.this);
  195. t.setText(values[a]);
  196. tRow.addView(t);
  197. }
  198.  
  199. return tRow;
  200. }
  201.  
  202. private boolean allCheck(){
  203. boolean check=true;
  204. for(int a=0;a<array.size();a++){
  205. if(array.get(a).getChecked() == false){
  206. check=false;
  207. }
  208. }
  209. return check;
  210. }
  211.  
  212. private int isFound(String tipo){
  213. int screwd = -1;
  214. for(int a=0;a<array.size();a++){
  215. if(array.get(a).NomeErro().equals(tipo)){
  216. screwd = a;
  217. }
  218. }
  219. return screwd;
  220. }
  221. @Override
  222. public void run() {
  223. // TODO Auto-generated method stub
  224. Random r = new Random();
  225. String[] tipos = new String[]{"Tensão A", "Tensão B", "Tensão C", "Corrente A", "Corrente B", "Corrente C"};
  226. int tipo = r.nextInt(6);
  227. int value = r.nextInt(255);
  228. Log.i("Valores Random", String.valueOf(value) + " " + tipos[tipo]);
  229. if(value> 140){
  230. if(isFound(tipos[tipo])==-1){
  231. array.add(new Erros(tipos[tipo]));
  232. } else {
  233. array.get(isFound(tipos[tipo])).incErro(1);
  234. }
  235.  
  236. }
  237. updateUI();
  238. h.postDelayed(this, 1000);
  239. }
  240.  
  241. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement