Show SQLITE data between dates - Android Studio (I think be between)

0

Well, I want you to choose the data to display between the dates he chooses. I tried everything but I still could not.

In other words, I want to filter between dates, but the user has to choose them. I already managed with an SQL command, but it was manual. I wanted you to choose the dates. Thanks!

Code:

input.java

private EditText DataEdit;     private EditText editDescricao;     private Button botaoadicionar;     private Button botaover;     static EditText DateEdit;     private SQLiteDatabase db;

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

    DataEdit = (EditText) findViewById(R.id.iEditdata);
    editDescricao = (EditText) findViewById(R.id.ieditDescricao);
    botaoadicionar = (Button) findViewById(R.id.botaoadicionar);
    botaover = (Button) findViewById(R.id.botaover);
    botaoadicionar.setOnClickListener(this);
    botaover.setOnClickListener(this);
    DateEdit = (EditText) findViewById(R.id.iEditdata);
    DateEdit.setOnClickListener(new View.OnClickListener() {
        // ver se fiz asneira...
        public void onClick(View v) {

            showTruitonDatePickerDialog(v);
        }
    });
}

protected void createDatabase(){
    db=openOrCreateDatabase("DadosDB", Context.MODE_PRIVATE, null);
    db.execSQL("CREATE TABLE IF NOT EXISTS tabeladados(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, data DATE,descricao VARCHAR);");
}
protected void insertIntoDB(){
    String data = DataEdit.getText().toString().trim();
    String descricao = editDescricao.getText().toString().trim();
    if(data.equals("") || descricao.equals("")){
        Toast.makeText(getApplicationContext(),"Preencha todos os campos", Toast.LENGTH_LONG).show();
        return;
    }

    String query = "INSERT INTO tabeladados (data,descricao) VALUES('"+data+"', '"+descricao+"');";
    db.execSQL(query);
    Toast.makeText(getApplicationContext(),"Guardado com sucesso", Toast.LENGTH_LONG).show();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {


    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    return super.onOptionsItemSelected(item);
}
private void mostrardados(){
    Intent intent = new Intent(this,output.class);
    startActivity(intent);
    finish();
}
@Override
public void onClick(View v) {
    if(v == botaoadicionar){
        insertIntoDB();
    }
    if(v==botaover){
        mostrardados();
    }
}
public void showTruitonDatePickerDialog(View v) {
    DialogFragment newFragment = new DatePickerFragment();
    newFragment.show(getSupportFragmentManager(), "datePicker");
}

public static class DatePickerFragment extends DialogFragment implements
        DatePickerDialog.OnDateSetListener {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);

        return new DatePickerDialog(getActivity(), this, year, month, day);
    }

    public void onDateSet(DatePicker view, int year, int month, int day) {

        DateEdit.setText(year + "-" + (month + 1) + "-" + day);
    }
}

}

output.java

public class output extends ActionBarActivity implements View.OnClickListener {     private EditText editDescricao;     private EditText editTextId;     private EditText editTextName;     private Button botaoanterior;     private Button botaoseguinte;

private static final String SELECT_SQL="SELECT * FROM tables";

private SQLiteDatabase db;
private Cursor c;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.output);
    openDatabase();
    editTextId = (EditText) findViewById(R.id.oeditTextId);
    editDescricao = (EditText) findViewById(R.id.oeditDescricao);
    editTextName = (EditText) findViewById(R.id.oeditTextName);
    botaoanterior = (Button) findViewById(R.id.botaoanterior);
    botaoseguinte = (Button) findViewById(R.id.botaoseguinte);
    botaoseguinte.setOnClickListener(this);
    botaoanterior.setOnClickListener(this);
    c = db.rawQuery(SELECT_SQL, null);
    c.moveToFirst();
    showRecords();

}

protected void openDatabase() {
    db = openOrCreateDatabase("DadosDB", Context.MODE_PRIVATE, null);
}

protected void showRecords() {
    String id = c.getString(0);
    String data = c.getString(1);
    String add = c.getString(2);
    editTextId.setText(id);
    editDescricao.setText(add);
    editTextName.setText(data);

}
protected void moveNext() {
    if (!c.isLast())
        c.moveToNext();
    showRecords();
}
protected void movePrev() {
    if (!c.isFirst())
        c.moveToPrevious();
    showRecords();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v) {
    if (v == botaoseguinte) {
        moveNext();
    }

    if (v == botaoanterior) {
        movePrev();
    }

}

public void voltar(View view) {
    Intent intent = new Intent(output.this, input.class);
    startActivity(intent);
}

}

input.xml

<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="VER"
    android:id="@+id/botaover"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Adicionar"
    android:id="@+id/botaoadicionar"
    android:layout_above="@+id/botaover"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/iEditdata"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="114dp"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_above="@+id/botaoadicionar"
    android:weightSum="1">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/ieditDescricao"
        android:hint="Descrição:"
        android:layout_below="@+id/iEditdata"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_weight="1" />
</LinearLayout>

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:ems="10"
    android:layout_below="@+id/iEditdata"
    android:id="@+id/Edithoras"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:hint="Total horas:" />

output xml

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/oeditTextName"
    android:weightSum="1">

    <EditText
        android:layout_width="match_parent"
        android:focusableInTouchMode="false"
        android:layout_height="161dp"
        android:id="@+id/oeditDescricao"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_above="@+id/botaoguardar"
        android:layout_weight="1" />
</LinearLayout>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Anterior"
    android:id="@+id/botaoanterior"
    android:layout_weight="0.20"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<Button
    android:text="Voltar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="voltar"
    android:id="@+id/botaovoltar"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Seguinte"
    android:id="@+id/botaoseguinte"
    android:layout_weight="0.31"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_alignLeft="@+id/botaovoltar"
    android:layout_alignStart="@+id/botaovoltar" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:ems="10"
    android:id="@+id/totalhoras"
    android:layout_above="@+id/botaoanterior"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />
    
asked by anonymous 03.01.2017 / 18:15

2 answers

0

As Reginaldo said above, creating a button can solve this problem.

You create the text boxes for the user to enter and then you will get this value to search the database.

I'm not sure about the syntax but it should be something like this:

@Override
public void onClick(View v) {
   String dataInicial = (EditText)findViewById(R.id.dataInicial);
   String dataFinal = (EditText)findViewById(R.id.dataFinal); // EditText é só um exemplo

   String query = "SELECT * FROM tabela 
   WHERE data BETWEEN " + dataInicial + " AND " + dataFinal;
   // aqui será o código para executar a query à BD.
   Cursor c = db.rawQuery(query, null);
   // depois terás de efetuar o tratamento dos dados
}
    
03.01.2017 / 19:03
0

How it works:

SELECT *
FROM tabela
WHERE datapesquisada BETWEEN '2017-01-01' AND '2017-12-31';
    
03.01.2017 / 18:25