【问题标题】:Remove duplicate value inside the arraylist from a dynamically created layout Android从动态创建的布局Android中删除arraylist中的重复值
【发布时间】:2014-09-24 23:21:32
【问题描述】:

我创建了一个动态添加/可移除 UI,用户将在其中在编辑文本中输入项目的成本,并且当用户单击添加时。该值将保存在 ArrayList 中,同时将在其中添加具有相同值的文本视图。当用户点击remove时,该值将从ArrayList中删除,文本视图也将被删除。

所以它就像我想要的那样工作,让用户输入类似 [1.00 2.00 3.00 4.00 5.00 6.00] 的内容。

但是,如果用户输入具有重复值的内容,例如 [1.00 2.00 3.00 2.00 4.00 2.00]。

如果在值为“2.00”的文本视图之一上单击删除,则有时会删除带有“2.00”的不正确索引,并且我的 ArrayList 中的序列与我的文本视图不同步。

在过去的 2 周里,我尝试了几种方法,但都无法做到正确。如果你们能帮帮我?

下面是我的实现:

行.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/rowLinearLayoutItem"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginRight="50dp"
        android:layout_marginLeft="50dp"
        android:baselineAligned="true" >

        <Button
            android:id="@+id/rowButtonRemove"
            android:text="-"
            android:layout_width="35dp"
            android:layout_height="35dp"
            android:background="@drawable/circle_button"/>

        <TextView
            android:id="@+id/rowItemCost"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:inputType="numberDecimal"
            android:ems="10"
            android:layout_weight="1" />

        <TextView
            android:id="@+id/rowItemTotal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:inputType="numberDecimal"
            android:ems="10"
            android:layout_weight="1" />

    </LinearLayout>

</LinearLayout>

片段.java

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.advButtonAddItem:
                String itemAddString = editBoxItemAdd.getText().toString();
                if (itemAddString.isEmpty()){
                    Toast.makeText(getActivity(), "Please Enter Item Cost", Toast.LENGTH_LONG).show();
                }
                else {
                    LayoutInflater layoutInflater = (LayoutInflater) getActivity().getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    final View addView = layoutInflater.inflate(R.layout.row, null);
                    final TextView rowItemTotal = (TextView) addView.findViewById(R.id.rowItemTotal);

                    Button rowButtonRemove = (Button) addView.findViewById(R.id.rowButtonRemove);

                    rowItemTotal.setText(editBoxItemAdd.getText().toString());

                    itemList.add(rowItemTotal.getText().toString());  

                    updateCost();

                    rowButtonRemove.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            ((LinearLayout)addView.getParent()).removeView(addView);
                            itemList.remove(rowItemTotal.getText().toString()); //Remove inputted Item Cost into ArrayList itemList

                            updateCost();
                        }
                    });
                    row.addView(addView);
                }
                break;
        }
    }

【问题讨论】:

  • 您应该考虑添加第二个数组列表或在地图中实现所有这些,您也可以在那里跟踪它们的索引!您是想删除重复项还是处理列表中存在具有多个索引的重复值时发生的情况?
  • 感谢您的建议。这让我想到了将我的值存储在哈希图中而不是 ArrayList 中。我添加了一个 "Log.d("LOG","v " + v);"在我的 rowButtonRemove.setOnClickListener 中,以及下面生成的 logcat。 v android.widget.Button{42323740 VFED..C. ...P.... 0,2-70,72 #7f0a0080 app:id/rowButtonRemove} 我认为值“42323740”非常适合我用作哈希图的键,但我似乎无法确定找出如何将该值取出到变量中。你知道我该怎么做吗?

标签: android dynamic arraylist android-framelayout viewgroup


【解决方案1】:
((LinearLayout)addView.getParent()).removeView(addView);

它总是会删除之前添加的项目

因为 addview 是您使用的最后一项

所以改成

((LinearLayout)addView.getParent()).removeView(v);

【讨论】:

  • 感谢您的建议。它并没有达到我想要的效果,因为布局现在没有被删除。然而,它给了我一个想法,使用“v”中的唯一值作为我的哈希图的键。我添加了一个 "Log.d("LOG","v " + v);"在我的 rowButtonRemove.setOnClickListener 中,以及下面生成的 logcat。 v android.widget.Button{42323740 VFED..C. ...P.... 0,2-70,72 #7f0a0080 app:id/rowButtonRemove} 我认为值“42323740”非常适合我用作哈希图的键,但我似乎无法确定找出如何将该值取出到变量中。你知道我该怎么做吗?
  • 如果你想要一个 unqiue 的东西,你可以使用 v.getId()
猜你喜欢
  • 2010-12-25
  • 2012-09-09
  • 2019-05-31
  • 1970-01-01
  • 2014-03-25
  • 1970-01-01
  • 2016-07-08
  • 2018-05-29
  • 1970-01-01
相关资源
最近更新 更多