Android中的ListView适配器中的CountDownTimer中的setText?

时间:2014-01-06 06:51:48

标签: android countdowntimer settext listview-adapter

我正在使用婚礼应用程序,我正在维护FeedView的ListView。

我的Feed屏幕ListView的第一个元素是当前日期到WEDDING DATE的TIMER。

由于我在TIMER中使用ListView,因此很难更新TextView方法中onTick()的文字。

婚礼时间仅在列表视图的第一个元素中可见。

_time = new CountDownTimer(WeddingTimeinMiliSec, 5000) {
                String showDate;
                long subtractvalue = 1000;

                Calendar currentTime = Calendar.getInstance();

                public void onTick(long millisUntilFinished) {
                    long diff = Day.getTimeInMillis() - currentTime.getTimeInMillis();
                    diff-=subtractvalue;
                    StringBuffer text = new StringBuffer("");
                    if (diff > DAY) {
                        //showDate = showDate+""+(diff / DAY)+("days ");
                        final long dif = diff;
                        if(holder!=null && holder._dayRespTextView!=null){
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    holder._dayRespTextView.setText(String.valueOf((dif / DAY)));
                                }
                            });
                        }
                        text.append(diff / DAY).append(" days ");
                        diff %= DAY;
                    }
                    if (diff > HOUR) {
                        //  showDate = showDate+""+(diff / HOUR)+("hours ");
                        final long dif = diff;
                        if(holder!=null && holder._hoursRespTextView!=null){
                            runOnUiThread(new Runnable() {
                                public void run() {
                                    holder._hoursRespTextView.setText(String.valueOf((dif / HOUR)));
                                }
                            });
                        }
                        text.append(diff / HOUR).append(" hours ");
                        diff %= HOUR;
                    }
                    if (diff > MINUTE) {
                        //  showDate = showDate+""+(diff / MINUTE)+("minutes ");
                        final long dif = diff;
                        if(holder!=null && holder._minRespTextView!=null){
                            runOnUiThread(new Runnable() {
                                public void run() {
                                    holder._minRespTextView.setText(String.valueOf((dif / MINUTE)));
                                }
                            });
                        }

                        text.append(diff / MINUTE).append(" minutes ");
                        diff %= MINUTE;
                    }
                    if (diff > SECOND) {
                        //  showDate = showDate+""+(diff / SECOND)+("seconds ");
                        final long dif = diff;
                        if(holder!=null && holder._secRespTextView!=null){
                            runOnUiThread(new Runnable() {
                                public void run() {
                                    holder._secRespTextView.setText(String.valueOf((dif / SECOND)));
                                }
                            });                         
                        }

                        text.append(diff / SECOND).append(" seconds ");
                        diff %= SECOND;
                    }
                    /*_tvTime.setText(""+WeddingTime);*/    
                    showDate="";
                    subtractvalue+=1000;
                    notifyDataSetChanged();
                }
                public void onFinish() {

                }
            }.start();

这是我在ListView ADAPTER的CONSTRUCTOR中使用的CountDownTimer代码。

如何在onTick事件的ListView的第一个元素中更新TextViews的文本。请建议我。

2 个答案:

答案 0 :(得分:0)

这是一个有趣的问题。我以前从来没有这么做过,这个答案只是在黑暗中拍摄,但我认为你会通过查看this answer找到一些前进的方法。

基本上,您需要跟踪ListView中可见的项目。仅更新这些可见视图中的TextView。要跟踪可见项目,您需要设置ListView的{​​{1}}。使用ListView#getFirstVisiblePosition()和ListView#getLastVisiblePosition()来保存第一个/最后一个位置,然后对该信息执行某些操作。我会尝试将它们发送到适配器,然后在处理其中一个位置时,在适配器的getView()中执行所需的操作。

就个人而言,我可能会做一些事情,比如让我的适配器实现一些OnScrollListener可以用来获取适配器位置的接口。您可能需要多个OnScrollListener,或者您可能需要找到另一种计算每个事件剩余时间的方法。


<强>更新

接口的东西可能更适合你的目的,但这里有一个概念验证示例代码,可以帮助我理解。

此代码显示一个非常简单的列表,其中所有偶数项目的时间显示每秒更新一次。我没有声明这个解决方案的效率,只是它执行了预期的任务:


MainActivity.java

CountDownTimer

MyCountDownTimer.java

package com.example.selectivelistitemupdating;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;

public class MainActivity extends Activity {

    ListView mListView;
    MyCountDownTimer mMyCountDownTimer;

    @Override
    protected void onCreate( Bundle savedInstanceState ) {

        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main );

        mListView = (ListView) findViewById( R.id.listView );
        MyAdapter adapter = new MyAdapter( this );
        mListView.setAdapter( adapter );

        mMyCountDownTimer = new MyCountDownTimer( 1000, 1000, adapter );
    }

    @Override
    protected void onResume() {

        super.onResume();
        mMyCountDownTimer.start();
    }

    @Override
    protected void onPause() {

        mMyCountDownTimer.cancel();
        super.onPause();
    }
}

MyAdapter.java

package com.example.selectivelistitemupdating;

import android.os.CountDownTimer;
import android.widget.BaseAdapter;

public class MyCountDownTimer extends CountDownTimer {

    BaseAdapter mAdapter;

    public MyCountDownTimer(long millisInFuture, long countDownInterval, BaseAdapter adapter) {

        super( millisInFuture, countDownInterval );
        mAdapter = adapter;
    }

    @Override
    public void onFinish() {

        mAdapter.notifyDataSetChanged();
        this.start();
    }

    @Override
    public void onTick( long millisUntilFinished ) {

        // NO OP
    }

}

activity_main.xml中

package com.example.selectivelistitemupdating;

import java.text.DateFormat;
import java.util.Calendar;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class MyAdapter extends BaseAdapter {

    private static final int COUNT = 30;

    private Context context;
    private DateFormat dateFormat;

    public MyAdapter(Context context) {

        this.context = context;
        dateFormat = DateFormat.getTimeInstance();
    }

    @Override
    public int getCount() {

        return COUNT;
    }

    @Override
    public String getItem( int position ) {

        return "Item " + String.valueOf( position );
    }

    @Override
    public long getItemId( int position ) {

        return position;
    }

    @Override
    public View getView( int position, View convertView, ViewGroup parent ) {

        if ( convertView == null ) {
            convertView = LayoutInflater.from( getContext() ).inflate( android.R.layout.two_line_list_item, parent, false );
        }

        TextView text1 = (TextView) convertView.findViewById( android.R.id.text1 );
        TextView text2 = (TextView) convertView.findViewById( android.R.id.text2 );

        text1.setText( getItem( position ) );
        text2.setText( getTimeString( position ) );

        return convertView;
    }

    private CharSequence getTimeString( int position ) {

        if ( position % 2 == 0 ) {

            return dateFormat.format( Calendar.getInstance().getTime() );
        }
        else {
            return null;
        }
    }

    private Context getContext() {

        return context;
    }
}

答案 1 :(得分:-1)

试试这个..

/**
* ADDING HEADER LAYOUT TO THE LISTVIEW
*/

View headerView = ((LayoutInflater)PostScreen.this.getSystemService(PostScreen.this.LAYOUT_INFLATER_SERVICE)).
inflate(R.layout.header_layout, null, false);
textview = (TextView)headerView.findViewById(R.id.countdown_tv);


 _time = new CountDownTimer(WeddingTimeinMiliSec, 5000) {
String showDate;
long subtractvalue = 1000;

Calendar currentTime = Calendar.getInstance();
public void onTick(long millisUntilFinished) {
long diff = Day.getTimeInMillis() - currentTime.getTimeInMillis();
diff-=subtractvalue;
StringBuffer text = new StringBuffer("");
if (diff > DAY) {
//showDate = showDate+""+(diff / DAY)+("days ");
final long dif = diff;
dayThread = String.valueOf((dif / DAY));
text.append(diff / DAY).append(" days ");
diff %= DAY;
}
if (diff > HOUR) {
final long dif = diff;
hoursThread = String.valueOf((dif / HOUR));
text.append(diff / HOUR).append(" hours ");
diff %= HOUR;
}
if (diff > MINUTE) {
final long dif = diff;
minThread = String.valueOf((dif / MINUTE));
text.append(diff / MINUTE).append(" minutes ");
diff %= MINUTE;
}
if (diff > SECOND) {
final long dif = diff;
secThread = String.valueOf((dif / SECOND));
text.append(diff / SECOND).append(" seconds ");
diff %= SECOND;
}
showDate="";
subtractvalue+=1000;

String dateText = "<font color="+countDown_TextColor+">"+dayThread+
" DIAS </font> <font color="+countDown_TextColor+">"+hoursThread+
" HOURAS </font><font color="+countDown_TextColor+">"+minThread+" MIN</font>" +
"<font color="+countDown_TextColor+">"+secThread+" MIN</font>";
textview.setText(Html.fromHtml(dateText));
}

public void onFinish() {

}
}.start();