Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package m.e;
- import java.util.ArrayList;
- import java.util.Random;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.content.DialogInterface;
- import android.graphics.Color;
- import android.os.Bundle;
- import android.os.Handler;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.CheckBox;
- import android.widget.CompoundButton;
- import android.widget.ScrollView;
- import android.widget.TableLayout;
- import android.widget.TableRow;
- import android.widget.TextView;
- import android.widget.CompoundButton.OnCheckedChangeListener;
- public class Main extends Activity implements Runnable {
- /** Called when the activity is first created. */
- private TableLayout tL;
- private ScrollView sV;
- private Handler h;
- private Button Apagar;
- private ArrayList<Erros> array;
- private boolean isReallyChecked;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- h = new Handler();
- h.post(this);
- array = new ArrayList<Erros>();
- // array.add("Pqp");
- // array.add("pqp2");
- // array.add("Pqp3");
- tL = (TableLayout) findViewById(R.id.widget44);
- sV = (ScrollView) findViewById(R.id.widget42);
- Apagar = (Button) findViewById(R.id.widget45);
- sV.setBackgroundColor(Color.LTGRAY);
- Apagar.setOnClickListener(new OnClickListener(){
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- if (array.size() > 0){
- for(int a=array.size()-1;a>=0;--a){
- CheckBox check = (CheckBox)tL.getChildAt(a+1).findViewById(R.id.widget30);
- if (check.isChecked()){
- array.remove(a);
- }
- }
- updateUI();
- }
- }
- });
- updateUI();
- }
- private void updateUI(){
- tL.removeAllViews();
- if(array.size() == 0){
- TableRow Row = new TableRow(this);
- TextView text = new TextView(this);
- text.setText("Não há nenhuma ocorrência de erro.");
- text.setTextColor(Color.BLACK);
- Row.addView(text);
- tL.addView(Row);
- } else {
- TableRow Row = new TableRow(this);
- CheckBox cBox = new CheckBox(this);
- if(isReallyChecked && allCheck()){
- cBox.setChecked(true);
- } else {
- cBox.setChecked(false);
- isReallyChecked = false;
- }
- cBox.setText("(Selecionar Todos)");
- cBox.setTextColor(Color.BLACK);
- cBox.setOnCheckedChangeListener(new OnCheckedChangeListener(){
- @Override
- public void onCheckedChanged(CompoundButton buttonView,
- boolean isChecked) {
- // TODO Auto-generated method stub
- isReallyChecked=isChecked;
- for(int a=array.size()-1;a>=0;--a){
- CheckBox check = (CheckBox)tL.getChildAt(a+1).findViewById(R.id.widget30);
- check.setChecked(isChecked);
- // check.setId(a);
- // check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
- //
- // @Override
- // public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
- // // TODO Auto-generated method stub
- // array.get(buttonView.getId()).setChecked(isChecked);
- // cBox.setChecked(false);
- // isReallyChecked=false;
- // }
- // });
- array.get(a).setChecked(isChecked);
- }
- }
- });
- Row.addView(cBox);
- tL.addView(Row);
- for(int a=0;a<array.size();a++){
- LayoutInflater inflater = (LayoutInflater) Main.this.getSystemService(LAYOUT_INFLATER_SERVICE);
- View layout = inflater.inflate(R.layout.template, null);
- CheckBox check = (CheckBox)layout.findViewById(R.id.widget30);
- check.setChecked(array.get(a).getChecked());
- // check.setId(a);
- // check.setOnCheckedChangeListener(new OnCheckedChangeListener(){
- //
- // @Override
- // public void onCheckedChanged(CompoundButton buttonView,
- // boolean isChecked) {
- // // TODO Auto-generated method stub
- // array.get(buttonView.getId()).setChecked(isChecked);
- // }
- //
- // });
- final TextView tView1 = (TextView) layout.findViewById(R.id.widget31);
- tView1.setText(array.get(a).NomeErro());
- TextView tView2 = (TextView) layout.findViewById(R.id.widget39);
- tView2.setText(array.get(a).numErros() + " erros.");
- if(a%2==0){
- layout.setBackgroundColor(Color.DKGRAY);
- tView1.setTextColor(Color.WHITE);
- tView2.setTextColor(Color.WHITE);
- } else {
- layout.setBackgroundColor(Color.LTGRAY);
- tView1.setTextColor(Color.BLACK);
- tView2.setTextColor(Color.BLACK);
- }
- Button b = (Button) layout.findViewById(R.id.widget40);
- b.setOnClickListener(new OnClickListener(){
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- LayoutInflater inflater = (LayoutInflater) Main.this.getSystemService(LAYOUT_INFLATER_SERVICE);
- View layout = inflater.inflate(R.layout.template_erros, null);
- TableLayout privTL = (TableLayout) layout.findViewById(R.id.widget47);
- privTL.setStretchAllColumns(true);
- privTL.addView(newRow("Hora", "Tipo", "Duração", "Máximo"));
- for(int a=0;a<5;a++){
- privTL.addView(newRow(String.valueOf(a*100), "SUBTENSAO", String.valueOf(a*1000), String.valueOf(a*50)));
- }
- AlertDialog.Builder dialog = new AlertDialog.Builder(Main.this);
- dialog.setTitle("Ocorrências de " + tView1.getText() + ":");
- dialog.setView(layout);
- dialog.setNeutralButton("Fechar", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- dialog.cancel();
- }
- })
- .show();
- }
- });
- tL.addView(layout);
- }
- }
- }
- private TableRow newRow(String... values){
- TableRow tRow = new TableRow(Main.this);
- for(int a=0;a<values.length;a++){
- TextView t = new TextView(Main.this);
- t.setText(values[a]);
- tRow.addView(t);
- }
- return tRow;
- }
- private boolean allCheck(){
- boolean check=true;
- for(int a=0;a<array.size();a++){
- if(array.get(a).getChecked() == false){
- check=false;
- }
- }
- return check;
- }
- private int isFound(String tipo){
- int screwd = -1;
- for(int a=0;a<array.size();a++){
- if(array.get(a).NomeErro().equals(tipo)){
- screwd = a;
- }
- }
- return screwd;
- }
- @Override
- public void run() {
- // TODO Auto-generated method stub
- Random r = new Random();
- String[] tipos = new String[]{"Tensão A", "Tensão B", "Tensão C", "Corrente A", "Corrente B", "Corrente C"};
- int tipo = r.nextInt(6);
- int value = r.nextInt(255);
- Log.i("Valores Random", String.valueOf(value) + " " + tipos[tipo]);
- if(value> 140){
- if(isFound(tipos[tipo])==-1){
- array.add(new Erros(tipos[tipo]));
- } else {
- array.get(isFound(tipos[tipo])).incErro(1);
- }
- }
- updateUI();
- h.postDelayed(this, 1000);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement