Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.listteste;
- import java.util.ArrayList;
- import android.app.Activity;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.graphics.Color;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.AdapterView.OnItemClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.ListView;
- import android.widget.TextView;
- import android.widget.Toast;
- public class ListaPers extends Activity implements OnItemClickListener {
- private ListView listView;
- private AdapterListView AdapterListView;
- private ArrayList<ItemListView> itens;
- SQLiteDatabase bancoDeDados = null; // banco de dados
- Cursor cursor;
- int i;
- int b = 1;
- EditText editText1;
- Button button1;
- TextView text2;
- private void setMessage(String message) {
- Log.e(" ListaPersonalisada ", message);
- }
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main); // identifica a ACTIVITY
- // desse codigo
- editText1 = (EditText) findViewById(R.id.editText1); // identifica o
- // edittext no
- // codigo
- button1 = (Button) findViewById(R.id.button1); // identifica o botao no
- // codigo
- listView = (ListView) findViewById(R.id.listView1); // identifica o
- // ListView
- listView.setOnItemClickListener(this); // recebe o clique do listview
- text2 = (TextView) findViewById(R.id.textView1);
- // //////////// LISTVIEW COMPONENTES //////////////////////////////
- itens = new ArrayList<ItemListView>(); // cria o ArrayList
- // criar o adapter
- AdapterListView = new AdapterListView(this, itens);
- // Define o Adapter
- listView.setAdapter(AdapterListView);
- // cor quando Γ© selecionado
- listView.setCacheColorHint(Color.TRANSPARENT);
- button1.setOnClickListener(new View.OnClickListener() { // recebe o
- // clique do
- // botao.
- @Override
- public void onClick(View v) {
- gravarRegistro();
- editText1.setText("");
- atualizaListView();
- }
- });
- setMessage(" ON CREATE OK!");
- }
- @Override
- protected void onResume() {
- super.onResume();
- try {
- abreOuCriaBanco();
- atualizaListView();
- } catch (Exception e) {
- Toast.makeText(this, "ERRO " + e.getMessage(), Toast.LENGTH_LONG)
- .show();
- }
- }
- public boolean atualizaListView() { // metodo que vai consultar e jogar
- // tudo na listview
- itens.removeAll(itens); // a cada vez q executa esse metodo ele limpa o
- // array e acrescenta o resultado do banco
- try {
- cursor = bancoDeDados.query("LISTAPERS", new String[] { "_ID",
- "NOME", "TIME", "PAGO" }, null,// selection,
- null,// selectionArgs,
- null,// groupBy,
- null,// having,
- null,// orderBy,
- null// limit
- );
- text2.setText("N de Reg: " + cursor.getCount()); // CONTADOR De REGISTROS
- for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor
- .moveToNext()) { // conta quantos registros tem no DB e joga
- // no array e mostra na lista
- ItemListView item = new ItemListView(cursor.getString(cursor
- .getColumnIndex("NOME")), R.drawable.atualiza);
- itens.add(item);
- AdapterListView.notifyDataSetChanged();
- }
- return true;
- } catch (Exception e) {
- Toast.makeText(this,
- "ERRO AO ADICIONAR NO ITEM LISTVIEW. " + e.getMessage(),
- Toast.LENGTH_LONG).show();
- return false;
- }
- }
- public void abreOuCriaBanco() { // abre ou cria banco
- try {
- String NOME_BANCO = "LISTAPERSONALISADA";
- String TABELA = "LISTAPERS";
- String COLUNA_NOME = "NOME";
- String COLUNA_TIME = "TIME";
- String COLUNA_PAGO = "PAGO";
- String COLUNA_ID = "_id";
- String sql = "CREATE TABLE IF NOT EXISTS " + TABELA + " ("
- + COLUNA_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
- + COLUNA_NOME + " text(20) not null," + COLUNA_TIME
- + " integer(2) default 0," + COLUNA_PAGO
- + " text(3) default NAO);";
- // cria ou abre o banco de dados
- bancoDeDados = openOrCreateDatabase(NOME_BANCO,
- MODE_ENABLE_WRITE_AHEAD_LOGGING, null);
- bancoDeDados.execSQL(sql);
- Toast.makeText(this, "BD CRIADO COM SUCESSO! ", Toast.LENGTH_LONG)
- .show();
- } catch (Exception e) {
- Toast.makeText(this, "BANCO DEU ERRO! ", Toast.LENGTH_LONG).show();
- }
- }
- public boolean DeletaItemBanco(int rowId) { // deleta item do banco
- try {
- ///////////// NAO FUNCIONA NEM A PAU.
- bancoDeDados.delete("LISTAPERS", "_id"+"="+rowId, null);
- Toast.makeText(this, "DELETE OK! ", Toast.LENGTH_LONG).show();
- return true;
- } catch (Exception e) {
- Toast.makeText(this, "DELETE DEU ERRO! ", Toast.LENGTH_LONG).show();
- return false;
- }
- }
- public void fechaBanco() {
- try {
- bancoDeDados.close();
- cursor.close();
- } catch (Exception e) {
- Toast.makeText(this,
- "ERRO AO FECHAR BANCO. Exception E " + e.getMessage(),
- Toast.LENGTH_LONG).show();
- }
- }
- public void gravarRegistro() { // grava registro no banco. captura do
- // edittext e joga pro banco
- try {
- String sqlInsert = "INSERT INTO LISTAPERS (nome) values ('"
- + editText1.getText().toString() + "')";
- bancoDeDados.execSQL(sqlInsert);
- Toast.makeText(this, "Dados Gravados com Sucesso! ",
- Toast.LENGTH_LONG).show();
- } catch (Exception e) {
- Toast.makeText(this,
- "ERRO Ao Gravar no BANCO. gravarRegistro()" + e.getMessage(),
- Toast.LENGTH_LONG).show();
- }
- }
- @Override
- protected void onStop() {
- bancoDeDados.close();
- cursor.close();
- super.onStop();
- }
- @Override
- public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
- // Pega o item que foi selecionado
- // ItemListView item = (ItemListView) AdapterListView.getItem(arg2);
- // DemonstraΓ§ao
- // Toast.makeText(this, "clicou " +
- // item.getTexto(),Toast.LENGTH_LONG).show();
- setMessage(" onItemClick(AdapterView<?> ");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment