【问题标题】:How can I fix my listView duplicating the same element when I add another element?当我添加另一个元素时,如何修复我的 listView 重复相同的元素?
【发布时间】:2019-01-04 00:56:52
【问题描述】:

我正在尝试在 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");
    }
}

}

主活动:

@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;
}

}

项目列表视图:

<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"/>

【问题讨论】:

  • 能否也分享一下itemListView相关的代码?
  • 我刚刚编辑了我的帖子,它就在底部
  • 抱歉,我说的是像为视图设置LayoutManager 等代码。据我所知,当您更改ListView 中的项目时,您可以简单地更改数据集并调用notifydatasetchanged。在此之后,您应该会看到列表视图/回收器正在用新项目刷新

标签: java android android-listview


【解决方案1】:

`

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

`

你需要做Purchase purchase = new Purchase(name, currentDate, value);在您的 applyChanges 方法中。

【讨论】:

  • 我想我可能一直不清楚,每当我添加一个元素时,所有旧元素都会获得与新元素相同的名称/值。如果我有一个名为“计算机”的旧元素,如果我添加一个名为“鼠标”的新元素,我会将第一个元素替换为“鼠标”,基本上第二个元素称为“鼠标”等......
  • 我刚刚发现我的 ArrayList 的元素顺序正确,但我的视图有问题
【解决方案2】:

问题已解决,在我使用的 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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多