我正在使用远程基础的android应用程序。我正在使用webservice。所以我在websevice中使用php。
我有一个自动完成的textview。我在此自动完成文本视图中显示客户端的数据。一旦我将其代码自动显示在edittext
中,我想选择一个客户端为此,我创建了这个类
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());
}
}
}
对于json我们有这个
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;
}
}
在日志中,每个客户端都有自己的代码。但是当我在代码edittext中执行时,它会在列表show中显示客户端位置。
例如,如果我在'Autocomplettetview中键入字母A,它会向我显示一些客户端,如果我选择列表中的第一个客户端,它会向我显示其代码为0001,而其代码不是那个。因此,我会在数据库中输入代码,它会向我显示它在列表显示中的位置。
知道在我做停止点的logcat中我注意到每个客户端都有正确的代码都很好。