Listing BD data using SELECT and ORDER BY

Asked

Viewed 543 times

1

Doubt

There is a screen with an Edittext and a Button. When entering any information in Edittext and selecting the Button the system should redirect the information from Edittext to an Activity and that Activity should select in the BD all the data that has that particular word in a column of the table. All data must return and be listed in ID order. On this screen where the data are presented it will be possible to select this data and when selecting it you should open a new Activity for editing it.

Class for BD data information

public class Atividade_Condicao {

public long id;
public String nome;
public String nome_processo;
public String responsavel;
public String departamento;
public String tipo;
public String detalhamento;
public String documento;

Screen with Edittext and Button

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:orientation="vertical">

<TextView android:id="@+id/process"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:layout_marginTop="10dip"
    android:text="Nome do Processo:"
    android:textColor="#000000" />

<EditText
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/etNome_processo"/>

<Button
    android:id="@+id/btBuscar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dip"
    android:padding="15dip"
    android:layout_weight="1"
    android:gravity="center"
    android:background="#1E90FF"
    android:textColor="#F8F8FF"
    android:text="Buscar" />

<Button
    android:id="@+id/btCancelar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dip"
    android:padding="15dip"
    android:layout_weight="1"
    android:gravity="center"
    android:background="#1E90FF"
    android:textColor="#F8F8FF"
    android:text="Cancelar" />

</LinearLayout>

Class with the Edittext

public class BuscarAtividade_Condicao extends Activity implements View.OnClickListener {

private RepositorioAtividade_Condicao repositorio;

@Override
protected void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    init();
    setContentView(R.layout.form_buscar_atividade_condicao);
    Button btBuscar = (Button) findViewById(R.id.btBuscar);
    btBuscar.setOnClickListener(this);

    Button btCancelar = (Button) findViewById(R.id.btCancelar);
    btCancelar.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            setResult(RESULT_CANCELED);
            finish();// Fecha a tela
        }
    });

public void onClick(View view) {
    EditText nome_processo = (EditText) findViewById(R.id.etNome_processo);

    // Recupera o nome do Processo
    String nomeProcesso = nome_processo.getText().toString();
    // Busca o Processo pelo nome

Access Class to the Bank

package com.example.alex.levprocess.banco;

import android.content.ContentValues;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import java.util.ArrayList;
import java.util.List;


public class DataBaseAtividade extends SQLiteOpenHelper {
// Database Version
private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME =    "/data/data/com.example.alex.levprocess/databases/lev";

// Labels table name
private static final String TABLE_LABELS = "atividade_condicao";

// Labels Table Columns names
private static final String KEY_ID = "_id";
private static final String KEY_NOME = "nome";
private static final String KEY_NOME_PROCESSO = "nome_processo";
private static final String KEY_RESPONSAVEL = "responsavel";
private static final String KEY_DEPARTAMENTO = "departamento";
private static final String KEY_TIPO = "tipo";
private static final String KEY_DETALHAMENTO = "detalhamento";
private static final String KEY_DOCUMENTO = "documento";

public DataBaseAtividade (Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
    // Category table create query
    String CREATE_CATEGORIES_TABLE = "CREATE TABLE " + TABLE_LABELS + "("
            + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NOME + " TEXT)" +       KEY_NOME_PROCESSO + " TEXT)" + KEY_RESPONSAVEL + " TEXT)" + KEY_DEPARTAMENTO + "     TEXT)" + KEY_TIPO + " TEXT)"
            + KEY_DETALHAMENTO + " TEXT)" + KEY_DOCUMENTO + " TEXT)";
    db.execSQL(CREATE_CATEGORIES_TABLE);
}

// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_LABELS);

    // Create tables again
    onCreate(db);
}

/**
 * Inserting new lable into lables table
 * */
public void insertLabel(String label){
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_NOME, label);
    values.put(KEY_NOME_PROCESSO, label);
    values.put(KEY_RESPONSAVEL, label);
    values.put(KEY_DEPARTAMENTO, label);
    values.put(KEY_TIPO, label);
    values.put(KEY_DETALHAMENTO, label);
    values.put(KEY_DOCUMENTO, label);

    // Inserting Row
    db.insert(TABLE_LABELS, null, values);
    db.close(); // Closing database connection
}

/**
 * Getting all labels
 * returns list of labels
 * */
public List<String> getAllLabels(){
    List<String> labels = new ArrayList<String>();

    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_LABELS;

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            labels.add(cursor.getString(1));
        } while (cursor.moveToNext());
    }

    // closing connection
    cursor.close();
    db.close();

    // returning lables
    return labels;
}
}

Complement

Only the name and type of the "activity_condition must be returned on screen"

  • 1

    Which part of the code you’re struggling with?

  • I am unable to create Activity to contemplate the above situation.

  • You at least already have a database access class?

  • Yes, I will post in the question the access class to the Bank.

  • 1

    I think what you need is a Listview. See if this link and this one, are very simple examples to do and to understand. Any doubt, just ask here :)

No answers

Browser other questions tagged

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