0
I have a Autocompletetextview that from a php api returns suggestions to the user based on values contained in my database.
php autocomplete.:
include_once('../includes/config.php');
$connection = new PDO("mysql:host=$host;dbname=$database;charset=utf8", $user, $pass);
$sql = 'SELECT * from tbl_estoque';
$statement = $connection->prepare($sql);
$statement->execute();
if($statement->rowCount())
{
$row_all = $statement->fetchall(PDO::FETCH_ASSOC);
header('Content-type: application/json');
echo json_encode($row_all);
}
elseif(!$statement->rowCount())
{
echo "no rows";
}
I want that when selecting the suggestion the Edittext for the price is filled also based on the price column of my database.
Fragmentopedidos.java
private void AddProdutoDialog(){
LayoutInflater inflater = LayoutInflater.from(getActivity());
View subView = inflater.inflate(R.layout.layout_adc_produto, null);
new AutoCompletar(getActivity(), subView).execute();
...
private class AutoCompletar extends AsyncTask<String, String, String> {
private String result;
private Context mContext;
private View rootView;
ProgressDialog pdLoading = new ProgressDialog(getActivity());
public AutoCompletar(Context context, View rootView){
this.mContext=context;
this.rootView=rootView;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
//this method will be running on UI thread
pdLoading.setMessage("\tAguarde...");
pdLoading.setCancelable(false);
pdLoading.show();
}
@Override
protected String doInBackground(String... params) {
result = null;
PutUtility put = new PutUtility();
try {
result = put.postData(ADMIN_PANEL_URL + "public/autocomplete-app.php");
/* put.setParam("UserId", params[0].toString());
put.setParam("Latitude", params[1].toString());
put.setParam("Longitude", params[2].toString());
put.setParam("DateTime", params[3].toString()); */
Log.v("result", result);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
@Override
protected void onPostExecute(String result) {
//this method will be running on UI thread
//ArrayList<String> dataList = new ArrayList<String>();
List<String> dataList=new ArrayList<>();
pdLoading.dismiss();
if(result.equals("no rows")) {
// Do some action if no data from database
}else{
try {
JSONArray jArray = new JSONArray(result);
// Extract data from json and store into ArrayList
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
dataList.add(json_data.getString("produto_nome"));
}
final AutoCompleteTextView text = (AutoCompleteTextView)
rootView.findViewById(R.id.tvProduto_Nome);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>
(mContext.getApplicationContext(), android.R.layout.simple_spinner_item, dataList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
text.setThreshold(1);
text.setAdapter(dataAdapter);
} catch (JSONException e) {
// You to understand what actually error is and handle it appropriately
DynamicToast.makeError(getActivity(),"Não foi possível contatar o servidor.", Toast.LENGTH_LONG).show();
}
}
}
}
As seen, the results are obtained by the "autocomplete" class. I just want the product name to receive suggestions but I want the price for the selected product to be filled in as well.
Here’s an example of how I want it to work: