0

I am working on android application with remote base. I am using webservice. so I am using php in websevice.

I have an autocomplete textview. I display data that are clients in this autocomplete textview. I want to choose a client once I will have its code automatically displayed in edittext

For this I create this class

class afficheclient extends AsyncTask<String, String, String> {
    InputStream is = null;
    String result = null;
    String line = null;

    ArrayList<Produit> products;

    /**
     * Override this method to perform a computation on a background thread. The
     * specified parameters are the parameters passed to {@link #execute}
     * by the caller of this task.
     * <p/>
     * This method can call {@link #publishProgress} to publish updates
     * on the UI thread.
     *
     * @param params The parameters of the task.
     * @return A result, defined by the subclass of this task.
     * @see #onPreExecute()       * @see #onPostExecute
     * @see #publishProgress
     */

    @Override
    protected String doInBackground(String... params) {
        return null;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();


        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://192.168.1.16/toutclient.php");
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
            Log.e("Pass 1", "connection success ");
        } catch (Exception e) {
            Log.e("Fail 1", e.toString());
            Toast.makeText(getApplicationContext(), "Invalid IP Address",
                    Toast.LENGTH_LONG).show();
        }

        try {
            BufferedReader reader = new BufferedReader
                    (new InputStreamReader(is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();

            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }

            is.close();
            result = sb.toString();
            Log.e("Pass 2", "connection success ");
        } catch (Exception e) {
            Log.e("Fail 2", e.toString());
        }

        try {
            JSONArray JA = new JSONArray(result);
            JSONObject json = null;

            products = new ArrayList<Produit>();


            for (int i = 0; i < JA.length(); i++) {
                products.add(new Produit(JA.getJSONObject(i)));
            }
            String[] str1 = new String[products.size()];
            for(int i = 0; i < products.size(); i++){
                str1[i] = products.get(i).getmNom();
            }

            final AutoCompleteTextView text = (AutoCompleteTextView)
                    findViewById(R.id.autoComplete2);
            final List<String> list = new ArrayList<String>();

            for (int i = 0; i < str1.length; i++) {
                list.add(str1[i]);
            }
            //.sort(list);
            Collections.sort(list);

            ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>
                    (getApplicationContext(), android.R.layout.simple_spinner_item, list);

            dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            text.setThreshold(1);
            text.setAdapter(dataAdapter);

            text.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

                    String prod=(String) arg0.getItemAtPosition(arg2);
                    String prix = products.get(arg2).getmPrix();

                    EditText prixEditText = (EditText) findViewById(R.id.prix);
                    prixEditText.setText(String.valueOf(prix));




                    // TODO Auto-generated method stub
                    //     Toast.makeText(getBaseContext(), list.get(arg2).toString(),
                    // Toast.LENGTH_SHORT).show();
                }
            });
        } catch (Exception e) {
            Log.e("Fail 3", e.toString());
        }
    }
}

and for the json we have this

public class Produit {

private static final String JSON_NOM = "D_client";
private static final String JSON_PRIX = "C_CodeClient";
private String mNom;
private String mPrix;

public Produit(JSONObject jsonObject) throws JSONException {
    mNom = jsonObject.getString(JSON_NOM);
    mPrix = jsonObject.getString(JSON_PRIX);
}

public Produit() {
}
public JSONObject convertToJSON() throws JSONException {
    JSONObject obj = new JSONObject();
    obj.put(JSON_NOM, mNom);
    obj.put(JSON_PRIX, mPrix);
    return obj;
}

public String getmNom() {
    return mNom;
}

public void setmNom(String mNom) {
    this.mNom = mNom;
}

public String getmPrix() {
    return mPrix;
}

public void setmPrix(String mPrix) {
    this.mPrix = mPrix;
}
}

In the log I have each client with its own code. But when I execute in the code edittext it displays the client position in the list show.

For example if I type the letter A in the 'Autocomplettetview it shows me some client if I choose the first client in the list it shows me that its code is 0001 whereas its code is not the one. So instead I will have the code that is entered in the database it shows me its position in the list show.

knowing that in logcat when I made the stop point I notice that all is good every Client has its correct code.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
jasmine
  • 3
  • 1
  • see [this](http://stackoverflow.com/a/19860624/2252830) – pskink Jan 12 '17 at 15:03
  • Please do not get into the habit of begging for help - no reader will respond faster because of your urgencies. However, you might get downvotes! – halfer Jan 12 '17 at 18:49

0 Answers0