BP-KORP

CLASSE_LISTPERSONALISADA

May 3rd, 2013
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.14 KB | None | 0 0
  1. package com.listteste;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. import android.app.Activity;
  6. import android.database.Cursor;
  7. import android.database.sqlite.SQLiteDatabase;
  8. import android.graphics.Color;
  9. import android.os.Bundle;
  10. import android.util.Log;
  11. import android.view.View;
  12. import android.widget.AdapterView;
  13. import android.widget.AdapterView.OnItemClickListener;
  14. import android.widget.Button;
  15. import android.widget.EditText;
  16. import android.widget.ListView;
  17. import android.widget.TextView;
  18. import android.widget.Toast;
  19.  
  20. public class ListaPers extends Activity implements OnItemClickListener {
  21.  
  22.     private ListView listView;
  23.     private AdapterListView AdapterListView;
  24.     private ArrayList<ItemListView> itens;
  25.     SQLiteDatabase bancoDeDados = null; // banco de dados
  26.     Cursor cursor;
  27.     int i;
  28.     int b = 1;
  29.     EditText editText1;
  30.     Button button1;
  31.     TextView text2;
  32.  
  33.    
  34.     private void setMessage(String message) {
  35.         Log.e(" ListaPersonalisada ", message);
  36.     }
  37.  
  38.     @Override
  39.     public void onCreate(Bundle savedInstanceState) {
  40.         super.onCreate(savedInstanceState);
  41.         setContentView(R.layout.activity_main); // identifica a ACTIVITY
  42.                                                     // desse codigo
  43.  
  44.         editText1 = (EditText) findViewById(R.id.editText1); // identifica o
  45.                                                                 // edittext no
  46.                                                                 // codigo
  47.         button1 = (Button) findViewById(R.id.button1); // identifica o botao no
  48.                                                         // codigo
  49.         listView = (ListView) findViewById(R.id.listView1); // identifica o
  50.                                                             // ListView
  51.         listView.setOnItemClickListener(this); // recebe o clique do listview
  52.         text2 = (TextView) findViewById(R.id.textView1);
  53.         // //////////// LISTVIEW COMPONENTES //////////////////////////////
  54.         itens = new ArrayList<ItemListView>(); // cria o ArrayList
  55.  
  56.         // criar o adapter
  57.         AdapterListView = new AdapterListView(this, itens);
  58.         // Define o Adapter
  59.         listView.setAdapter(AdapterListView);
  60.         // cor quando Γ© selecionado
  61.         listView.setCacheColorHint(Color.TRANSPARENT);
  62.  
  63.         button1.setOnClickListener(new View.OnClickListener() { // recebe o
  64.                                                                 // clique do
  65.                                                                 // botao.
  66.             @Override
  67.             public void onClick(View v) {
  68.  
  69.             gravarRegistro();
  70.             editText1.setText("");
  71.             atualizaListView();
  72.        
  73.            
  74.             }
  75.         });
  76.  
  77.         setMessage(" ON CREATE OK!");
  78.  
  79.     }
  80.  
  81.     @Override
  82.     protected void onResume() {
  83.         super.onResume();
  84.  
  85.         try {
  86.             abreOuCriaBanco();
  87.             atualizaListView();
  88.  
  89.         } catch (Exception e) {
  90.             Toast.makeText(this, "ERRO  " + e.getMessage(), Toast.LENGTH_LONG)
  91.                     .show();
  92.         }
  93.  
  94.     }
  95.  
  96.     public boolean atualizaListView() { // metodo que vai consultar e jogar
  97.                                             // tudo na listview
  98.  
  99.         itens.removeAll(itens); // a cada vez q executa esse metodo ele limpa o
  100.                                 // array e acrescenta o resultado do banco
  101.  
  102.         try {
  103.             cursor = bancoDeDados.query("LISTAPERS", new String[] { "_ID",
  104.                     "NOME", "TIME", "PAGO" }, null,// selection,
  105.                     null,// selectionArgs,
  106.                     null,// groupBy,
  107.                     null,// having,
  108.                     null,// orderBy,
  109.                     null// limit
  110.                     );
  111.  
  112.             text2.setText("N de Reg: " + cursor.getCount()); // CONTADOR De REGISTROS
  113.  
  114.             for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor
  115.                     .moveToNext()) { // conta quantos registros tem no DB e joga
  116.                                         // no array e mostra na lista
  117.  
  118.                 ItemListView item = new ItemListView(cursor.getString(cursor
  119.                         .getColumnIndex("NOME")), R.drawable.atualiza);
  120.                 itens.add(item);
  121.                 AdapterListView.notifyDataSetChanged();
  122.  
  123.             }
  124.  
  125.             return true;
  126.  
  127.         } catch (Exception e) {
  128.             Toast.makeText(this,
  129.                     "ERRO AO ADICIONAR NO ITEM LISTVIEW. " + e.getMessage(),
  130.                     Toast.LENGTH_LONG).show();
  131.             return false;
  132.         }
  133.     }
  134.  
  135.     public void abreOuCriaBanco() { // abre ou cria banco
  136.         try {
  137.             String NOME_BANCO = "LISTAPERSONALISADA";
  138.             String TABELA = "LISTAPERS";
  139.             String COLUNA_NOME = "NOME";
  140.             String COLUNA_TIME = "TIME";
  141.             String COLUNA_PAGO = "PAGO";
  142.             String COLUNA_ID = "_id";
  143.  
  144.             String sql = "CREATE TABLE IF NOT EXISTS " + TABELA + " ("
  145.                     + COLUNA_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
  146.                     + COLUNA_NOME + " text(20) not null," + COLUNA_TIME
  147.                     + " integer(2) default 0," + COLUNA_PAGO
  148.                     + " text(3) default NAO);";
  149.  
  150.             // cria ou abre o banco de dados
  151.             bancoDeDados = openOrCreateDatabase(NOME_BANCO,
  152.                     MODE_ENABLE_WRITE_AHEAD_LOGGING, null);
  153.             bancoDeDados.execSQL(sql);
  154.  
  155.             Toast.makeText(this, "BD CRIADO COM SUCESSO! ", Toast.LENGTH_LONG)
  156.                     .show();
  157.         } catch (Exception e) {
  158.  
  159.             Toast.makeText(this, "BANCO DEU ERRO! ", Toast.LENGTH_LONG).show();
  160.         }
  161.     }
  162.  
  163.    
  164.    
  165.    
  166.     public boolean DeletaItemBanco(int rowId) { // deleta item do banco
  167.         try {
  168.    
  169.     ///////////// NAO FUNCIONA NEM A PAU.      
  170.            
  171.  
  172.     bancoDeDados.delete("LISTAPERS", "_id"+"="+rowId, null);
  173.      
  174.      
  175.            
  176.             Toast.makeText(this, "DELETE OK! ", Toast.LENGTH_LONG).show();
  177.             return true;
  178.         } catch (Exception e) {
  179.  
  180.             Toast.makeText(this, "DELETE DEU ERRO! ", Toast.LENGTH_LONG).show();
  181.             return false;
  182.         }
  183.        
  184.     }
  185.  
  186.  
  187.    
  188.     public void fechaBanco() {
  189.         try {
  190.             bancoDeDados.close();
  191.             cursor.close();
  192.  
  193.         } catch (Exception e) {
  194.             Toast.makeText(this,
  195.                     "ERRO AO FECHAR BANCO. Exception E " + e.getMessage(),
  196.                     Toast.LENGTH_LONG).show();
  197.         }
  198.     }
  199.  
  200.     public void gravarRegistro() { // grava registro no banco. captura do
  201.                                     // edittext e joga pro banco
  202.  
  203.         try {
  204.             String sqlInsert = "INSERT INTO LISTAPERS (nome) values ('"
  205.                     + editText1.getText().toString() + "')";
  206.  
  207.             bancoDeDados.execSQL(sqlInsert);
  208.  
  209.             Toast.makeText(this, "Dados Gravados com Sucesso! ",
  210.                     Toast.LENGTH_LONG).show();
  211.  
  212.         } catch (Exception e) {
  213.             Toast.makeText(this,
  214.                     "ERRO Ao Gravar no BANCO. gravarRegistro()" + e.getMessage(),
  215.                     Toast.LENGTH_LONG).show();
  216.         }
  217.     }
  218.  
  219.     @Override
  220.     protected void onStop() {
  221.  
  222.         bancoDeDados.close();
  223.         cursor.close();
  224.  
  225.         super.onStop();
  226.     }
  227.  
  228.     @Override
  229.     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
  230.         // Pega o item que foi selecionado
  231.         // ItemListView item = (ItemListView) AdapterListView.getItem(arg2);
  232.         // DemonstraΓ§ao
  233.         // Toast.makeText(this, "clicou " +
  234.         // item.getTexto(),Toast.LENGTH_LONG).show();
  235.  
  236.         setMessage(" onItemClick(AdapterView<?> ");
  237.  
  238.     }
  239. }
Advertisement
Add Comment
Please, Sign In to add comment