android - 如何动态添加按钮和总计?

标签 android listview button android-listview

如何在 ListView 内添加 Total TextView 以及 Save Button ListView下,动态的?


总计的显示方式与下图类似。 (注意:不是我的数据表示)

Example of a ListView Total Row


Activity A(代码 fragment )

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();
}

Activity A(渲染输出)

Rendered Output

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


我正在使用以下 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>

谢谢。

最佳答案

将以下 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>

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

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());

正如评论中提到的,您可以使用long total作为静态类成员,并用结果值total+=results更新它;如果从列表中删除某个项目,请小心将该值重置为零或递减。

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

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

<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

关于android - 如何动态添加按钮和总计?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33794442/

相关文章:

java - 从 MainActivity 到 Fragment 问题的非静态字符串变量

android - 在 Android 网络浏览器上选择一个项目

android - 单击项目时,自定义 ListView 不会在第二个 Activity 中显示图像

wpf - 如何将 WPF 滚动条样式应用于特定的 ListView ?

java - Android:如何检测所有ListView对象是否已加载

java - 使用按钮在jsp中调用java函数

python - Python 的 Tkinter 可以在按钮小部件中同时显示文本和图像吗?

android - 我们可以在 android 中将 3 个控件绑定(bind)到一个控件中吗?

java - Android Double 过于精确

javascript - 如果按钮没有显示,请更改按钮的文本?