当我添加另一个元素时,如何修复复制相同元素的listView?

时间:2019-01-04 00:56:52

标签: java android android-listview

我正在尝试在Android Studio(完整的初学者)上创建预算应用程序,目前遇到了一个问题: 我基本上有一个listView,可以在其中添加任何购买的商品(名称和价值+购买日期)

每当我向列表视图中添加一个元素,而不是添加另一个元素,新的元素就会被复制。 例如,如果我已经在列表视图中添加了Element1并添加了“ Element2”,那么我将再次获取“ Element2”和“ Element2”,并替换其他元素。 我认为我的适配器或视图有问题,但是我无法弄清楚...

任何帮助将不胜感激

public class CustomPopUp extends AppCompatDialogFragment {
private EditText editTextName;
private EditText editTextValue;
private CustomPopUpListener listener;
private Calendar calendar;
private static String name;
private static String currentDate;
private static Float value;

@Override
public Dialog onCreateDialog (Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    final AlertDialog ad = builder.show();

    LayoutInflater inflater = getActivity().getLayoutInflater();
    View view = inflater.inflate(R.layout.activity_ajouter_pop_up, null);

    calendar = Calendar.getInstance();

    builder.setView(view)
            .setTitle("Add element")
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    ad.dismiss();
                }
            })
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    name = editTextName.getText().toString();
                    String s = editTextValue.getText().toString();
                    value = Float.parseFloat(s);
                    String currentDate = DateFormat.getDateInstance().format(calendar.getTime());
                    listener.applyChanges(name, currentDate, value);
                    ad.dismiss();
                }
            });

    editTextName = view.findViewById(R.id.item_edit_text);
    editTextValue = view.findViewById(R.id.item_edit_value);

    return builder.create();
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);

    try {
        listener = (CustomPopUpListener) context;
    } catch (ClassCastException e) {
        throw new ClassCastException(context.toString() + "must implement CustomPopUpListener");
    }
}

}

MainActivity:

@Override
public void applyChanges(String name, String currentDate, Float value) {
    purchase = new Purchase(name, currentDate, value);
    items.add(purchase);
    adapter = new PurchaseListAdapter(getApplicationContext(), R.layout.adapter_view_layout, items);
    itemsListView.setAdapter(adapter);
}

}

public class PurchaseListAdapter extends ArrayAdapter<Purchase> {
private static final String TAG ="PurchaseListAdapter";

private Context mContext;
private int mResource;

public PurchaseListAdapter(Context context, int resource, ArrayList<Purchase> objects) {
    super(context, resource, objects);
    mContext = context;
    mResource = resource;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    String name = CustomPopUp.getName();
    String currentDate = CustomPopUp.getCurrentDate();
    Float value = CustomPopUp.getValue();
    String sValue = Float.toString(value);

    Purchase purchase = new Purchase(name, currentDate, value);

    LayoutInflater inflater = LayoutInflater.from(mContext);
    convertView = inflater.inflate(mResource, parent, false);

    TextView tvName = (TextView) convertView.findViewById(R.id.textView25);
    TextView tvCurrentDate = (TextView) convertView.findViewById(R.id.textView26);
    TextView tvValue = (TextView) convertView.findViewById(R.id.textView27);

    tvName.setText(name);
    tvCurrentDate.setText(currentDate);
    tvValue.setText(sValue);

    return convertView;
}

}

ItemsListView:

<ListView
    android:id="@+id/items_list"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="28dp"
    android:layout_marginEnd="8dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/progressBar" />




 itemsListView = (ListView) findViewById(R.id.items_list);

我的“ adapter_view_layout”由

组成
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="100">


    <TextView
        android:gravity="center"
        android:textAlignment="center"
        android:text="TextView1"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:id="@+id/textView25"
        android:layout_weight="66.6"/>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="33.3">

        <TextView
            android:gravity="center"
            android:text="TextView2"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:id="@+id/textView26"/>

        <TextView
            android:gravity="center"
            android:text="TextView3"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:id="@+id/textView27"
            android:layout_below="@+id/textView2"/>

2 个答案:

答案 0 :(得分:0)

`

@Override
public void applyChanges(String name, String currentDate, Float value) {
    Purchase purchase = new Purchase(name, currentDate, value);
    items.add(purchase);
    adapter = new PurchaseListAdapter(getApplicationContext(), 
    R.layout.adapter_view_layout, items);
    itemsListView.setAdapter(adapter);
}

`

您需要进行购买购买=新购买(名称,currentDate,值);在您的applyChanges方法中。

答案 1 :(得分:0)

问题已解决,在我使用的PurchaseListAdapter中

String name = CustomPopUp.getName();
String currentDate = CustomPopUp.getCurrentDate();
Float value = CustomPopUp.getValue();

这解决了问题:

String name = getItem(position).getName();
String currentDate = getItem(position).getCurrentDate();
Float value = getItem(position).getValue();