Android:ListView 行中的计时器?

发布于 2024-10-09 01:45:38 字数 2346 浏览 5 评论 0原文

我的 ListView 带有计时器的行 xml


<LinearLayout android:layout_width="fill_parent"
android:id="@+id/layout_orders_list_row_ll_Timer" 
android:focusable="false"   
android:layout_height="wrap_content" 
android:gravity="center_horizontal|right"
android:layout_gravity="center">
<Chronometer android:text="@+id/Chronometer01"
    android:layout_height="wrap_content" android:layout_width="wrap_content"
    android:id="@+id/layout_orders_list_row_chr_Timer"
    android:focusable="false" android:focusableInTouchMode="false"
    android:layout_gravity="center" android:enabled="true">
</Chronometer>
<Button android:background="@drawable/check" android:layout_height="wrap_content" 
    android:id="@+id/layout_orders_list_row_bt_Done" 
    android:layout_width="wrap_content" android:focusable="false"                   android:onClick="OnRowButtonClick">
</Button>


,我想为每一行启动计时器。我从数据库获取数据并填充适配器。之后,我连接到每个计时器并尝试计算它。但是它不起作用。

    SimpleAdapter simpleAdapter = new SimpleAdapter(this, fillMaps,
            R.layout.orders_list_row, from, to);

    listView.setAdapter(simpleAdapter);

       for (int i = 0; i < simpleAdapter.getCount(); i++) {

        LinearLayout vwParentRow = (LinearLayout) simpleAdapter.getView(i,
                null, null);

        LinearLayout ChildTop = (LinearLayout) vwParentRow.getChildAt(0);
        LinearLayout ChildTop2 = (LinearLayout) ChildTop.getChildAt(1);
        final Chronometer chrono = (Chronometer) ChildTop2.getChildAt(0);
        chrono.setText("00:00:00");
        chrono.setBase(SystemClock.elapsedRealtime());
        chrono.start();

        chrono.setOnChronometerTickListener(new OnChronometerTickListener() {

            public void onChronometerTick(Chronometer arg0) {
                // TODO Auto-generated method stub

                    long minutes = ((elapsedTime - chrono.getBase()) / 1000) / 60;
                    long seconds = ((elapsedTime - chrono.getBase()) / 1000) % 60;
                    currentTime = minutes + ":" + seconds;
                    arg0.setText(currentTime);
                    elapsedTime = elapsedTime + 1000;

            }

        });

    }

    listView.invalidate();

我做错了什么?也许还有其他解决方案如何在每一行启动计时器?

I got a row xml for my ListView with chronometer


<LinearLayout android:layout_width="fill_parent"
android:id="@+id/layout_orders_list_row_ll_Timer" 
android:focusable="false"   
android:layout_height="wrap_content" 
android:gravity="center_horizontal|right"
android:layout_gravity="center">
<Chronometer android:text="@+id/Chronometer01"
    android:layout_height="wrap_content" android:layout_width="wrap_content"
    android:id="@+id/layout_orders_list_row_chr_Timer"
    android:focusable="false" android:focusableInTouchMode="false"
    android:layout_gravity="center" android:enabled="true">
</Chronometer>
<Button android:background="@drawable/check" android:layout_height="wrap_content" 
    android:id="@+id/layout_orders_list_row_bt_Done" 
    android:layout_width="wrap_content" android:focusable="false"                   android:onClick="OnRowButtonClick">
</Button>


and I want to start my chronometer for each row.I'm getting data from database and fill the adapter.After that I'm connecting to each chronometer and trying to calculate it.But it doesn't work.

    SimpleAdapter simpleAdapter = new SimpleAdapter(this, fillMaps,
            R.layout.orders_list_row, from, to);

    listView.setAdapter(simpleAdapter);

       for (int i = 0; i < simpleAdapter.getCount(); i++) {

        LinearLayout vwParentRow = (LinearLayout) simpleAdapter.getView(i,
                null, null);

        LinearLayout ChildTop = (LinearLayout) vwParentRow.getChildAt(0);
        LinearLayout ChildTop2 = (LinearLayout) ChildTop.getChildAt(1);
        final Chronometer chrono = (Chronometer) ChildTop2.getChildAt(0);
        chrono.setText("00:00:00");
        chrono.setBase(SystemClock.elapsedRealtime());
        chrono.start();

        chrono.setOnChronometerTickListener(new OnChronometerTickListener() {

            public void onChronometerTick(Chronometer arg0) {
                // TODO Auto-generated method stub

                    long minutes = ((elapsedTime - chrono.getBase()) / 1000) / 60;
                    long seconds = ((elapsedTime - chrono.getBase()) / 1000) % 60;
                    currentTime = minutes + ":" + seconds;
                    arg0.setText(currentTime);
                    elapsedTime = elapsedTime + 1000;

            }

        });

    }

    listView.invalidate();

What I'm doing wrong? Maybe there is other solution how to start chronometer in each row?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

手长情犹 2024-10-16 01:45:38

为了解决这个问题,我创建了自己的适配器,因为没有适合解决方案的标准有价值的选项。


public class SimpleAdapterWithCheckbox extends SimpleAdapter {


public SimpleAdapterWithCheckbox(Context context,
        List<? extends Map<String, ?>> data, int resource, String[] from,
        int[] to) {
    super(context, data, resource, from, to);

}

 // method getView start for each row in adapter to show it in ListView
 public View getView(int position, View convertView, ViewGroup parent) {                
        View v = super.getView(position, convertView, parent);
        startChrono(v);// starting for each row
        return v;       
    }


 private void  startChrono(View v){

    //Here we need to know structure of layout to get our element 
    LinearLayout vwParentRow = (LinearLayout)v;
    LinearLayout ChildTop = (LinearLayout) vwParentRow.getChildAt(0);
    LinearLayout ChildTop2 = (LinearLayout) ChildTop.getChildAt(1);

    //Now, we get chronometer and could work with it as we wont
    Chronometer chrono = (Chronometer) ChildTop2.getChildAt(0);
    chrono.setBase(SystemClock.elapsedRealtime());
    chrono.start();

 }


不是最好的情况,但对我来说效果很好。

To deal with this problem, I created my own adapter, since there is no standard valuable option which suits to solution.


public class SimpleAdapterWithCheckbox extends SimpleAdapter {


public SimpleAdapterWithCheckbox(Context context,
        List<? extends Map<String, ?>> data, int resource, String[] from,
        int[] to) {
    super(context, data, resource, from, to);

}

 // method getView start for each row in adapter to show it in ListView
 public View getView(int position, View convertView, ViewGroup parent) {                
        View v = super.getView(position, convertView, parent);
        startChrono(v);// starting for each row
        return v;       
    }


 private void  startChrono(View v){

    //Here we need to know structure of layout to get our element 
    LinearLayout vwParentRow = (LinearLayout)v;
    LinearLayout ChildTop = (LinearLayout) vwParentRow.getChildAt(0);
    LinearLayout ChildTop2 = (LinearLayout) ChildTop.getChildAt(1);

    //Now, we get chronometer and could work with it as we wont
    Chronometer chrono = (Chronometer) ChildTop2.getChildAt(0);
    chrono.setBase(SystemClock.elapsedRealtime());
    chrono.start();

 }

}


This is not best case scenario, but works just fine for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文