2014-09-24 28 views
0

我已经创建了一个动态添加/可移动UI,用户将在编辑文本中输入项目成本,并且当用户单击添加时。该值将被保存在一个ArrayList中,同时一个文本视图将被添加相同的值。当用户单击删除时,该值将从ArrayList中删除,并且文本视图也将被删除。从动态创建的布局中去除arraylist中的重复值Android

因此,只要用户输入类似于[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周里一直在尝试几种方法,但只是无法做到正确。如果你们能帮助我?

下面是我的实现:

row.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> 

Fragment.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; 
     } 
    } 
+0

你应该考虑增加第二个数组列表或实现这一切都在一个地图,那里你可以跟踪其指标的呢!你想删除重复项还是处理在列表中有多个索引的重复值时会发生什么? – Pavlos 2014-09-24 23:25:19

+0

感谢您的建议。这给了我一个想法,把我的值存储在一个hashmap而不是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”对我来说是完美的,可以用作我的hashmap的关键字,但我无法想出如何将这个价值转化为一个变量。你知道我如何做到这一点吗? – enzoshadow 2014-09-25 06:06:47

回答

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

,它会永远delets先前添加的项目

因为addvi EW如果你使用

的lastitem所以将其更改为

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

感谢您的建议。这并不是我想要的,因为布局现在不会被删除。然而,它给了我一个想法,使用“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”对我来说是完美的,可以用作我的hashmap的关键字,但我无法想出如何将这个价值转化为一个变量。你知道我如何做到这一点吗? – enzoshadow 2014-09-25 06:12:54

+0

如果你想要一个unqiue的东西,你可以使用v.getId() – 2014-09-25 06:33:48