I have a Listview with multiple items, in every item there is 2 buttons. What i want to do is when i click on a button, it sends some data to server through the HttpPost. I put a Thread inside my Adapter.getView() but it's not working because the Adapter is aleady in an Async Thread. Have you an idea about how can i make it work?
My ListView :
// ... OnCreate ListView ...
// ... new ListAppTask().execute();
// ...
public class ListAppTask extends AsyncTask<Void, Void, List<ProductExtended>> {
protected List<ProductExtended> doInBackground(Void... args) {
Product product = new Product();
List<ProductExtended> products = product.getLastProducts(getApplicationContext());
return products;
}
protected void onPostExecute(List<ProductExtended> result) {
data.addAll(result);
adapter.notifyDataSetChanged();
if (progressDialog != null) {
progressDialog.dismiss();
progressDialog = null;
}
}
}
My Adapter :
public class PackageAdapter extends BaseAdapter {
private List<ProductExtended> data;
private Context context;
public PackageAdapter(Context context, List<ProductExtended> data) {
this.context = context;
this.data = data;
}
@Override
public int getCount() {
return data.size();
}
@Override
public ProductExtended getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ProductExtended item = getItem(position);
ViewHolder holder;
if (convertView == null) {
LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = li.inflate(R.layout.package_row, parent, false);
holder = new ViewHolder();
holder.btnBa = (LinearLayout) convertView.findViewById(R.id.idBonneaffaire);
holder.btnJl = (LinearLayout) convertView.findViewById(R.id.idJelai);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.btnBa.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new Thread() {
@Override
public void run() {
// ---------------- PROBLEM HERE -------------------
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("My URL");
// Execute HTTP Post Request
try {
HttpResponse response = httpclient.execute(httppost);
String reponse = EntityUtils.toString(response.getEntity());
} catch (UnknownHostException e) {
} catch (HttpHostConnectException e) {
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
}
}.start();
}
});
// Same for the second LinearLayout considered as Button
return convertView;
}
static class ViewHolder {
LinearLayout btnBa;
LinearLayout btnJl;
}
And the error diplayed :
Only the original thread that created a view hierarchy can touch its views. adapter
Thank's, Akram