Я пытаюсь создать бюджетное приложение на 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"/>