Listing DB data using SELECT and ORDER BY

1

Doubt

There is a screen with an EditText and a Button. When entering any information in EditText and selecting the Button the system must redirect the information of the EditText to an activity and this activity should select in the BD all the data that owns that particular word in a column of the table. All data must be returned and listed in order of ID. In this screen the data that is presented will be able to select this data and when selecting them it should open a new activity for editing it.

Class for information on DB data

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 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

Bank Access Class

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

You only need to return the name and type of the "activity_content" on the screen

    
asked by anonymous 09.01.2016 / 00:06

0 answers