mardi 3 mars 2015

Android Array in SQL Query

Sorry, I'm newbie... :) please take a minute's time to help me and forgive me for my bad english


I have made autocompletetextview with data coming from the database that also I have made with use SQLite, for example as follows


table country

column name | column code



United States | USA

Brazil | BRA

England | ENG

France | FRA

China | CHI

Japan | JPN


well, this time I've done a data call using either country name and country code through autocompletetextview. Success and without error. before accessing previous autocompletetextview must select the option to use the spinner with a choice



  1. Europe

  2. America

  3. Asia


after selecting the option on the spinner, the application will make deliveries http post to the server, and the server responds only country code.


the problem that I face, how to display suggestion autocompletetextview by the selected spinner. For example, the user selects Asia then suggestion on autocompletetextview only display china and japan.


I inform some pieces of code :



public class CustomAutoCompleteTextChangedListener implements TextWatcher {

@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}

@Override
public void onTextChanged(CharSequence userInput, int start, int before,
int count) {

try {
PopulateAutoComplete[] myObject = databaseHandler.readCodeRoutes("SUB", userInput.toString());
routesAdapter = new AutocompleteCustomArrayAdapter(context,
R.layout.list_view_row, myObject);
etBerangkat.setAdapter(routesAdapter);
etKembali.setAdapter(routesAdapter);

routesAdapter.notifyDataSetChanged();
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}

}
}


I use that way, each typing on autocompletetextview will access the database one by one word. before the user selects option on the spinner :



public PopulateAutoComplete[] readCodeRoutes(String country) {
String sql = "SELECT * FROM " + tableName;
sql += " WHERE " + fieldObjectName + " LIKE '%" + country + "%'";
sql += " ORDER BY " + fieldObjectId + " DESC";
}


after the user selects option on the spinner. I tried this, but it does not bring up suggestions on autocompletetextview



public PopulateAutoComplete[] readCodeRoutes(String world, String country) {
String sql = "SELECT * FROM " + tableName;
sql += " WHERE " + fieldObjectName + " LIKE '%" + country+ "%'";
sql += " AND " + fieldObjectKode + " LIKE '%" + world + "%'"; <-- I add this
sql += " ORDER BY " + fieldObjectId + " DESC";
}


if I write it on the line a second query in sql



sql += " AND " + fieldObjectKode + " LIKE '%" + world + "%'";


to be :



sql += " AND " + fieldObjectKode + " LIKE '%CHI%'";


then suggestion on autocompletetextview only display china.


and my question how easy way to display the suggestion based on the spinner selection. I think by using an array in query sql but as far as I'm trying until now always error. Please help me :((


Aucun commentaire:

Enregistrer un commentaire