gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 05:08:53 25 4
gpt4 key购买 nike

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

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

任何帮助将不胜感激

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

主要 Activity :

@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 :

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

最佳答案

`

@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 方法中。

关于java - 当我添加另一个元素时,如何修复我的 listView 复制相同的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54031861/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com