i'm trying to solve a problem since two weeks to put a countdown timer in each row of a listview, starting the countdown 开发者_开发百科at the onclick on the row. The problem is that the timer begins on many rows concurrently and if i press the options button and i push on any new activity and i come back to the main, all countdown disappear. Do you have any idea? here is my code.
CountDownTest.java
public class CountDownTest extends ListActivity {
Cursor model=null;
TestAdapter adapter=null;
TestHelper helper=null;
long currentId=0;
View currentView = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
helper=new TestHelper(this);
model=helper.getAll();
startManagingCursor(model);
adapter=new TestAdapter(model);
setListAdapter(adapter);
}
@Override
public void onDestroy() {
super.onDestroy();
helper.close();
}
@Override
public void onListItemClick(ListView list, View view, int position, long id) {
currentId = id;
currentView = view;
final TextView dt = (TextView)currentView.findViewById(R.id.countDownList);
final CountDownTimer myTimer = new CountDownTimer(60000, 1000) {
public void onTick(long millisUntilFinished) {
dt.setText("Arrives in " + millisUntilFinished/1000 + " seconds");
}
public void onFinish() {
dt.setText("Arrived?");
}
};
myTimer.start();
}
class TestAdapter extends CursorAdapter {
TestAdapter(Cursor c) {
super(EasyQueue.this, c);
}
@Override
public void bindView(View row, Context ctxt, Cursor c) {
TestHolder holder=(TestHolder)row.getTag();
holder.populateFrom(c, helper);
}
@Override
public View newView(Context ctxt, Cursor c, ViewGroup parent) {
LayoutInflater inflater=getLayoutInflater();
View row=inflater.inflate(R.layout.row, parent, false);
TestHolder holder=new TestHolder(row);
row.setTag(holder);
return(row);
}
}
static class TestHolder {
private TextView name=null;
private TextView phone=null;
TestHolder(View row) {
name=(TextView)row.findViewById(R.id.nameList);
phone=(TextView)row.findViewById(R.id.phoneList);
}
void populateFrom(Cursor c, TestHelper helper) {
name.setText(helper.getName(c));
phone.setText(helper.getPhone(c));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
new MenuInflater(this).inflate(R.menu.option, menu);
return(super.onCreateOptionsMenu(menu));
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId()==R.id.reset) {
return(true);
}else if (item.getItemId()==R.id.add){
startActivity(new Intent(EasyQueue.this, DetailForm.class));
return(true);
}else if (item.getItemId()==R.id.prefs){
return(true);
}
return(super.onOptionsItemSelected(item));
}
}
Can you check the code please? i tryed with an Handler but the result is worst. The countdown survives to an option button pression and come back but it start on more rows and doesn't start always. MANY THANKS!!!!
精彩评论