After searching more information about this specific problem, I try to ask here if someone has the solution..
I'm trying to develop an Android application wich present some events in a list view with a countdown timer between today and the event's date.
I've put a "onItemClickListener" on the listview in order to ask the user if he wants to add the event to his list.
This is the countdown code :
while (countdownActive) {
countdownThread = new Thread(new Runnable() {
@Override
public synchronized void run() {
try {
this.wait(300);
handler.sendEmptyMessage(3);
} catch (InterruptedException e) {
Log.d("CKUne", e.getMessage());
}
}
});
countdownThread.start();
The problem is when I click on an item, the Dialog Box is not always show. Sometime it is (probably because the main Thread has the focus) and sometime it isn't (when the countDown thread that has the focus).
Hope you have the solution to stop a thread from another.
Thank you
@Geo : This is the mechanism for the Dialog box :
eventsListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, final int position,
long id) {
ckPosition = position;
new addCKTask().execute(null , null , null);
}
});
the addCKTask :
private class addCKTask extends AsyncTask<URL, Integer, Long> {
@Override
protected Long doInBackground(URL... params) {
Log.d("CKUne" , "itemclick");
handler.sendEmptyMessage(5);
handler.sendEmptyMessage(4);
handler.sendEmptyMessage(2);
return null;
}
}
then the Handler :
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
switch(msg.what) {
case 1 :
findViewById(R.id.loadCategoriesProgress).setVisibility(View.VISIBLE);
title.setText(categoriesName[indexCategorie].getLibelleCategorie());
break;
case 2 :
eventsListView.setAdapter(adapter);
countdownActive = true;
cdTask = new startCountDown();
cdTask.execute(null, null, null);
findViewById(R.id.loadCategoriesProgress).setVisibility(View.INVISIBLE);
break;
case 3 :
Thread.currentThread().interrupt();
adapter.notifyDataSetChanged();
break;
case 4 :
countdownThread.interrupt();
stopCountDown();
AlertDialog.Builder loginAlert = new AlertDialog.Builder(CKALaUne.this);
loginAlert.setMessage("Voulez-vous ajouter " + events.get(ckPosition).getNomCK() + "à votre liste ?");
loginAlert.setPositiveButton("Ajouter", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
chronokifWebServices.AddToMyChronokifs(CKALaUne.this.getIntent().getExtras().getString("pseudonyme"), CKALaUne.this.getIntent().getExtras().getString("password"), (events.get(ckPosition).getIdCK() + ""));
}
});
loginAlert.setNegativeButton("Annuler", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
loginAlert.show();
break;
case 5 :
stopCountDown();
break;
}
}
};
At the beginning, i used only thread and handler but due to wrong result i tried the AsyncTask but it give 开发者_JS百科the same result : When i click, there is a chance that the click doesn't work and a chance that it works.
I'm not sure without the rest of the code, but the Dialog not showing up might be due to the this.wait(300);
since that freezes the screen. Also, if you want to stop the countDown thread use countdownThread.interrupt();
and make sure to include any important actions in the catch (InterruptedException e) {
block.
Lastly, I dont know what the rest of the loop looks like, but it appears as though you are creating a new thread every iteration of your while loop, which- if that was the case- could be why the thread isn't predictable.
精彩评论