【问题标题】:How to add button and total dynamically?如何动态添加按钮和总数?
【发布时间】:2015-11-19 02:53:52
【问题描述】:

如何在ListView 内添加Total TextView,以及在ListView 下动态添加保存 Button


Total 将显示类似于下图。 (注意:不是我的数据表示)


活动 A(代码片段)

long as=0; // for total
long bs=0;    

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    // receive from B
    switch (requestCode) 
    {
        case 0:
            result = data.getStringExtra("text"); // holds value (34,24)
            name = data.getStringExtra("a"); // Project
            as = as+Long.parseLong(result); // For total amount
            Text = "  " + name + " " + "RM" + result + ""; // display in listView

            if (mClickedPosition == -1) {
                //add new list
                m_listItems.add(Text);
                m_listBitmapItems.add(Global.img);
            } else {
                // edit listView value and image
                m_listItems.set(mClickedPosition, Text);  
                m_listBitmapItems.set(mClickedPosition, Global.img);
            }

            adapter.notifyDataSetChanged();
            listV.setAdapter(adapter);

            break;
        case 1:  // Another name
            result = data.getStringExtra("text");
            name = data.getStringExtra("a");
            description = data.getStringExtra("c");
            bs = bs + Long.parseLong(result);
            Log.d("FIRST", "result:" + result);
            Text = "  " + name + "  " + "RM" + result + "";

            if (mClickedPosition == -1)
            {
                m_listItems.add(Text);
            } else {
                m_listItems.set(mClickedPosition, Text);
            }

            adapter.notifyDataSetChanged();
            listV.setAdapter(adapter);

            break;
        }
    }

    long samount = as + bs;
    Toast.makeText(getActivity(), samount + "", Toast.LENGTH_LONG).show();
}

活动 A(渲染输出)

在这里,我想添加一个 Total TextView,其值为 58(输出为 RM58),以及一个保存 Button


我正在使用以下 xml 文件。

a.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical">

    <TextView 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:padding="10dp" 
        android:text="@string/text" 
        android:textSize="20sp" />

    <ListView android:id="@+id/listView1" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" />

</LinearLayout>

claims.xml(在 listView 内)

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <TextView android:id="@+id/textView1"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true" 
        android:layout_alignParentTop="true"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <ImageView android:id="@+id/imageView4"
        android:layout_width="101dp"
        android:layout_height="50dp" />

</AbsoluteLayout>

谢谢。

【问题讨论】:

  • @MrsEd 总数怎么样?
  • 您应该查看ListView 控件文档,但一般来说.. 您应该考虑控件的行为.. 这些是其他控件,因此应该通过在列表视图控件。数据需要以另一种方式聚合..
  • 可以提供一些链接或示例吗?我对此一无所知...谢谢
  • @BrettCaswell 你能帮帮我吗?请
  • @MrsEd 一旦有了list,就会出现总数

标签: android listview button android-listview


【解决方案1】:

将以下footer.xml文件添加到res/layout http://developer.android.com/reference/android/widget/ListView.html

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent
    android:orientation="vertical" >

    // Format as you please
    <TextView
        android:id="@+id/tv"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="Total" />

</LinearLayout>

在您的活动中创建并初始化以下变量。

LayoutInflater inflater = getLayoutInflater();
ViewGroup footer = (ViewGroup) inflater.inflate(R.layout.footer, listView,
            false);

// You can add this when an item is added and remove it if the list is empty.
listView.addFooterView(footer, null, false);


tv = (TextView)footer.findViewById(R.id.tv);

// Update the text.
tv.append("t/"+ results.toString());

如 cmets 中所述,您可以使用 long total 作为静态类成员,并使用结果值 total+=results 更新它;如果从列表中删除项目,请注意将值重置为零或减少它。

另一种方法是遍历列表中的项目,将项目解析为对象,并从每个对象中获取long 类型的特定值,然后将它们相加。

由于您现在可以动态添加按钮,我将简要添加以供其他用户浏览,将按钮的可见性设置为 GONE,以便元素在不可见时不会取代布局,HIDDEN 使元素不可见,但元素占用的空间会影响布局(有时这很有用)。然后按钮的可见性在一个项目被添加到列表时动态更改为 VISIBLE,并在列表被清除时返回到 GONE。

<Button
    android:id="@+id/btn"
    android:text="button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="gone"/>

http://developer.android.com/reference/android/transition/Visibility.html

【讨论】:

  • 这就是我动态添加 save 按钮的方式。 post
  • 所以我需要使用不同的xml 吗?一个是button,另一个是total(footer.xml)
猜你喜欢
  • 2020-02-15
  • 1970-01-01
  • 1970-01-01
  • 2019-03-10
  • 2016-08-01
  • 2013-07-07
  • 2016-05-24
  • 1970-01-01
相关资源
最近更新 更多