Help with sql search and android studio

Asked

Viewed 102 times

1

I made this layout

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="false"
    android:layoutMode="clipBounds">

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:background="@android:color/holo_blue_dark"
        tools:context=".Actions_Main.Act_Biblioteca_Ufv">


        <Spinner
            android:id="@+id/Spn_Estacoes"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true" />


        <TextView
            android:id="@+id/tvResultPesquisa"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/Spn_Estacoes"
            android:layout_gravity="bottom"
            android:layout_marginTop="11dp"
            android:textAlignment="textEnd"
            android:textColor="#00aa55"
            android:textSize="10dp"
            android:textStyle="italic" />


        <Button
            android:id="@+id/btnFechar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/btnPesquisar"
            android:layout_marginTop="60dp"
            android:background="@drawable/mybutton"
            android:text="@string/lbl_Fechar"
            android:textColor="#ffffff" />


        <Button
            android:id="@+id/btnPesquisar"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/tvResultPesquisa"
            android:layout_marginTop="19dp"
            android:background="@drawable/mybutton"
            android:text="Pesquisar"
            android:textColor="@color/abc_primary_text_disable_only_material_dark" />


    </RelativeLayout>

</ScrollView>

that has a Spinner, a textview and a bot when searching

I have this connection that searches the bank to get the last 100 record

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class Dao_Hist_Alarmes {

    public String nomeTabela() {

        String nomeData = "";
        String nomeAlarme = "";
        String nomeRecebe = "";
        ConexaoDao conexao = new ConexaoDao();
        ObjetoConexao objConexao = new ObjetoConexao();
        objConexao.db_connect_string = "ufv.ddns.me:5433";
        objConexao.db_name = "Celular";
        objConexao.db_userid = "sa";
        objConexao.db_password = "flextelecom";
        Connection conn = conexao.dbConnect(objConexao);

        if (conn == null) {

            nomeData = "Não foi possivel se conectar ao banco de dados";
            nomeAlarme = "Não foi possivel se conectar ao banco de dados";
            nomeRecebe = "Não foi possivel se conectar ao banco de dados";

        } else if (conn != null) try {
            Statement statement = conn.createStatement();
            String queryString = "select TOP 100 Area,E3TimeStamp,Message from Alarmescelular Where AREA = 'BIBLIOTECA UFV' order By E3TimeStamp DESC ";
            ResultSet rs;

            rs = statement.executeQuery(queryString);

            if (rs.next()) {

                nomeData = rs.getString("E3TimeStamp");
                nomeAlarme = rs.getString("Message");
                nomeRecebe = nomeData + "  " + nomeAlarme;

            }
        } catch (SQLException e) {
            nomeRecebe = e.getMessage();


        }
        return nomeRecebe;


    }

}

and here I receive the data and send to the textview

    public class Act_Hist_Alarmes extends AppCompatActivity implements View.OnClickListener {

        private Spinner Estacoes;
        private TextView tvResultPesquisa;
        private Button btnFechar;
        private Button btnPesquisar;
        String ESTACAO1 = "BIBLIOTECA UFV";
        String ESTACAORECEBIDA;
        Dao_Hist_Alarmes DHA;
        String FalhaCon = "Não foi possivel se conectar ao banco de dados";

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.act_hist_alarmes);

            Estacoes = (Spinner) findViewById(R.id.Spn_Estacoes);
            tvResultPesquisa = (TextView) findViewById(R.id.tvResultPesquisa);
            btnFechar = (Button) findViewById(R.id.btnFechar);
            btnFechar.setOnClickListener(this);
            btnPesquisar = (Button) findViewById(R.id.btnPesquisar);
            btnPesquisar.setOnClickListener(this);


            ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.Sistema_Mobile, android.R.layout.simple_spinner_item);
            Estacoes.setAdapter(adapter);

            final AdapterView.OnItemSelectedListener escolha = new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                    String item = Estacoes.getSelectedItem().toString();
                    ESTACAORECEBIDA = item;

                    Toast.makeText(getApplicationContext(),"teste: "+ESTACAORECEBIDA, Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {


                }


            };

            Estacoes.setOnItemSelectedListener(escolha);

        }


    String nomeRecebeAL1;




    public void onClick(View v) {

    if (v.getId() == R.id.btnFechar) {

            finish();

    } else if (v.getId() == R.id.btnPesquisar) {


        DHA = new Dao_Hist_Alarmes();
        nomeRecebeAL1 = DHA.nomeTabela();

        tvResultPesquisa.setTextColor(Color.parseColor("#FF2D5E46"));
        tvResultPesquisa.setText(nomeRecebeAL1);    

    }
}

But the same only shows me in textiview the first record.

someone can help me understand what’s wrong?

  • What should happen? If I understood you are setting the result in a TextView, you want to set 100 results in one TextView?

  • wanted to show this result on him, I’m actually a beginner. would have another way to do this?

  • The best thing would be to use a listview and an Adapter

  • but it’s still not showing

  • Got your class together DAO to return a ArrayList<String> instead of a String, your method nomeTabela() is returning a string so only one result appears

  • Dao is following the example above. would have an example of how to do?

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.