0
How to list more than 1 database data using JDBC on a Listview on Android? I have in a user 13 lines, and would like to recover the 13 and not just 1. Below the code I use:
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Tela2 extends MyActivity {
static TextView useron;
static ListView listgeral;
static EditText email, descricao, codigo;
Button atualiza;
TabHost.TabSpec spec1, spec2;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tela2);
//Crio um objeto tabhost
TabHost tabHost = (TabHost) findViewById(R.id.tabhost);
tabHost.setup();
//pego a váriavel do tabhost para setar as configurações
spec1 = tabHost.newTabSpec("Aba 1");
spec1.setContent(R.id.tab1);
spec1.setIndicator("Meus Pacotes");
spec2 = tabHost.newTabSpec("Aba 2");
spec2.setContent(R.id.tab2);
spec2.setIndicator("Novo rastreamento");
tabHost.addTab(spec1);
tabHost.addTab(spec2);
//Crio uma váriavel contendo os componentes "TextView"
listgeral = (ListView) findViewById(R.id.valor1);
atualiza = (Button) (findViewById(R.id.atualiza));
//Crio um TextView de usuario online
useron = (TextView) findViewById(R.id.usuario);
//Seto o valor que é recebido da classe "MyActivity"
useron.setText(dominios);
String url = "jdbc:mysql://localhost";
String driver = "com.mysql.jdbc.Driver";
//String contendo o usu�rio do banco
String userbanco = "kappauni";
//String contendo a senha do usuario do banco
String passbanco = "teste";
java.sql.Connection con = null;
try {
Class.forName(driver).newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
con = DriverManager.getConnection(url, userbanco, passbanco);
Statement st = null;
st = con.createStatement();
ResultSet rs = null;
rs = st.executeQuery("SELECT * FROM encomendas where dominio ='"+useron.getText()+"'");
while (rs.next()){
String valorcodigo = rs.getString("codigorastreio");
String valordescricao = rs.getString("descricao");
String[] stringcodigo = {valorcodigo.toString()+"\t"+"\t"+"\t"+"\t"+"\t"+valordescricao.toString()};
ArrayAdapter<String> arraycodigo = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,stringcodigo);
listgeral.setAdapter(arraycodigo);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
//Método que ao clicar no botão adicionar, ele vai adicionar os valores no banco
public void Adicionar(View add) throws Exception {
email = (EditText) findViewById(R.id.form1);
descricao = (EditText) findViewById(R.id.form2);
codigo = (EditText) findViewById(R.id.form3);
BancoInsert conexaoinsert = new BancoInsert();
conexaoinsert.insert();
}
//BOtão de atualizar
public void atualizar(View upd){
Toast.makeText(Tela2.this,"Atualizou",Toast.LENGTH_SHORT).show();
}
}
Do you give this lot of space in your code even or got badly formatted when posted here? It gets hard to keep track of the code.
– Maniero
Using Mysql localhost database on an Android device? Android itself has Sqlite, why not use it?
– Wakim
It is only for testing, because the same will be used in my hosting as I already used.
– Diego Kappaun