Andriod Studio ListView不会自行更新

时间:2019-11-29 22:24:08

标签: android android-listview custom-adapter

我有ListView和自己的适配器。 ListView项由TextView,图标图像和图像“垃圾箱”(简称“垃圾箱”)组成。问题是,当我单击任何项​​目时,应将其删除,但实际上ListView的最后一项消失了。我检查了ArrayList,并从中删除了我单击的项目。
另外,当我尝试添加新项目时,程序仅恢复先前的项目,并且如果显示所有初始项目,并且我尝试添加新程序,则程序崩溃。尽管可以在ArrayList中正确添加项目。

活动代码

public class MyItemsActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_items);

        myItemsList = findViewById(R.id.ListOfMyItems);
        myItemsAdapter.arrayList.add(new MyItems("Keys", R.drawable.plus));
        myItemsAdapter.arrayList.add(new MyItems("Wallet", R.drawable.plus));
        myItemsAdapter.arrayList.add(new MyItems("Physics", R.drawable.plus));
        myItemsAdapter.arrayList.add(new MyItems("umovnyy vasyl", R.drawable.plus));
        myItemsAdapter.arrayList.add(new MyItems("Smartphone)", R.drawable.plus));
        for (int i = 0; i<100; i++){
            arrayList.add(new MyItems("Max)", R.drawable.plus));
        }
        myItemsList.addFooterView(new ImageView(this));
        myItemsList.addHeaderView(new ImageView(this));
        myItemsList.setAdapter(myItemsAdapter);
        for (int i = 0; i<100; i++){
            myItemsAdapter.arrayList.remove((5));
        }
        arrayList.add(new MyItems("Maxx)", R.drawable.plus));
        myItemsAdapter.notifyDataSetChanged();
        myItemsAdapter.notifyDataSetInvalidated();

        myItemsList.setOnItemClickListener((parent, view, position, id) -> {
            Toast.makeText(getApplicationContext(),
                    "Click ListItem Number " + position, Toast.LENGTH_SHORT)
                    .show();
            myItemsAdapter.arrayList.remove((position-1));
            myItemsAdapter.notifyDataSetChanged();
            myItemsList.invalidateViews();
            myItemsAdapter.notifyDataSetInvalidated();
        });
    }

    ListView myItemsList;
    ArrayList<MyItems> arrayList = new ArrayList<>();
    MyItemsAdapter myItemsAdapter = new MyItemsAdapter(MyItemsActivity.this, arrayList);

    public void addNewItem(View view) {
        AlertDialog.Builder dialog = new AlertDialog.Builder(MyItemsActivity.this);
        dialog.setView(R.layout.add_new_item);
        AlertDialog alertDialog = dialog.create();
        ListView a = alertDialog.getListView();
        alertDialog.show();
        myItemsList.invalidateViews();
    }

    public void addNewItemButtonClicked(View view){
        LayoutInflater inflater = getLayoutInflater();

        View v = inflater.inflate(R.layout.add_new_item, null);

        EditText nameOfItem = v.findViewById(R.id.nameOfItem);

        arrayList.add(new MyItems(nameOfItem.getText().toString(), R.drawable.forwardbutton));
        myItemsAdapter.notifyDataSetChanged();
        myItemsAdapter.notifyDataSetInvalidated();
    }

    public void notificationClicked(View view){
        Toast.makeText(getApplicationContext(),
                "Notification clicked", Toast.LENGTH_SHORT)
                .show();
    }

    public void openMenu(View view) {
        Intent intent = new Intent(getBaseContext(), MenuActivity.class);
        startActivity(intent);
    }
}

适配器



class MyItemsAdapter extends BaseAdapter {
    ArrayList<MyItems> arrayList;
    Context context;
    public MyItemsAdapter(Context context, ArrayList<MyItems> arrayList) {
        this.arrayList=arrayList;
        this.context=context;
    }
    @Override
    public boolean areAllItemsEnabled() {
        return false;
    }
    @Override
    public boolean isEnabled(int position) {
        return true;
    }
    @Override
    public void registerDataSetObserver(DataSetObserver observer) {
        observer.onChanged();
        super.registerDataSetObserver(observer);
        }
    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {
        super.unregisterDataSetObserver(observer);
        observer.onInvalidated();
    }
    @Override
    public int getCount() {
        return arrayList.size();
    }
    @Override
    public MyItems getItem(int position) {
        return arrayList.get(position);
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public boolean hasStableIds() {
        return false;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final MyItems myItems = arrayList.get(position);
        if(convertView == null) {
            LayoutInflater layoutInflater = LayoutInflater.from(context);
            convertView = layoutInflater.inflate(R.layout.example_of_list_of_items, parent, false);

            TextView nameOfItem = convertView.findViewById(R.id.exampleTextView);
            ImageView itemCircle = convertView.findViewById(R.id.exampleImageViewIcon);
            ImageView trash = convertView.findViewById(R.id.exampleImageViewTrash);

            nameOfItem.setText(myItems.itemName);
            itemCircle.setImageResource(myItems.itemImage);
            trash.setImageResource(R.drawable.trash);

            trash.setOnClickListener(v -> {
                Toast.makeText(context, "lol "+position, Toast.LENGTH_SHORT).show();
                    arrayList.remove(position);
                    notifyDataSetChanged();
                    notifyDataSetInvalidated();
            });
        }
        return convertView;
    }
    @Override
    public int getItemViewType(int position) {
        return position;
    }
    @Override
    public int getViewTypeCount() {
        return arrayList.size();
    }
    @Override
    public boolean isEmpty() {
        return false;
    }
}

MyItems代码

class MyItems {
    String itemName;
    int itemImage;
    public MyItems(String itemName, int image) {
        this.itemName = itemName;
        this.itemImage = image;
    }
}

项目示例

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/exampleTextView"
            android:layout_width="170dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="30dp"
            android:layout_marginTop="20dp"
            android:layout_marginEnd="85dp"
            android:fadingEdge="horizontal|vertical"
            android:layoutDirection="inherit"
            android:lineSpacingExtra="14sp"
            android:text="TextView"
            android:textAlignment="textStart"
            android:textColor="#000000"
            android:textSize="20sp"
            app:autoSizeMaxTextSize="20sp"
            app:autoSizeMinTextSize="8sp"
            app:layout_constraintEnd_toStartOf="@+id/exampleImageViewTrash"
            app:layout_constraintStart_toEndOf="@+id/exampleImageViewLeft"
            app:layout_constraintTop_toTopOf="parent" />


        <ImageView
            android:id="@+id/exampleImageViewLeft"
            android:layout_width="25dp"
            android:layout_height="23dp"
            android:layout_marginStart="32dp"
            android:layout_marginTop="24dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@mipmap/ic_launcher_round" />

        <ImageView
            android:id="@+id/exampleImageViewTrash"
            android:layout_width="49dp"
            android:layout_height="47dp"
            android:layout_marginTop="12dp"
            android:layout_marginEnd="20dp"
            android:cropToPadding="true"
            android:fitsSystemWindows="true"
            android:focusableInTouchMode="false"
            android:focusable="false"
            android:clickable="false"
            android:scaleType="centerInside"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@drawable/trash" />

        <androidx.constraintlayout.widget.Guideline
            android:id="@+id/guideline2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintGuide_begin="70dp"
            app:layout_constraintStart_toStartOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>

主屏幕代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="99dp">

        <androidx.constraintlayout.widget.Guideline
            android:id="@+id/guideline"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintGuide_begin="127dp"
            app:layout_constraintStart_toStartOf="parent" />

        <TextView
            android:id="@+id/myItemsTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/my_items"
            android:textColor="#000000"
            android:textSize="36sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <ImageView
            android:id="@+id/menuImageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="20dp"
            android:clickable="true"
            android:focusable="true"
            android:onClick="openMenu"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@drawable/menu" />

        <ImageView
            android:id="@+id/notificationImageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="16dp"
            android:clickable="true"
            android:focusable="true"
            android:onClick="notificationClicked"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@drawable/notification" />
    </androidx.constraintlayout.widget.ConstraintLayout>


    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ListView
            android:id="@+id/ListOfMyItems"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_marginStart="1dp"
            android:layout_marginTop="30dp"
            android:layout_marginEnd="1dp"
            android:layout_marginBottom="100dp"
            android:alwaysDrawnWithCache="true"
            android:animateLayoutChanges="true"
            android:choiceMode="singleChoice"
            android:clickable="false"
            android:clipToPadding="false"
            android:descendantFocusability="beforeDescendants"
            android:dividerHeight="1dp"
            android:footerDividersEnabled="true"
            android:headerDividersEnabled="true"
            android:isScrollContainer="true"
            android:layoutDirection="ltr"
            android:scrollbars="vertical"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="1.0"
            tools:listfooter="@android:layout/simple_list_item_1">

        </ListView>

        <LinearLayout
            android:layout_width="409dp"
            android:layout_height="70dp"
            android:layout_marginStart="1dp"
            android:layout_marginTop="1dp"
            android:layout_marginEnd="1dp"
            android:focusable="auto"
            android:orientation="horizontal"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/ListOfMyItems">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:clickable="true"
                android:focusable="true">

                <TextView
                    android:id="@+id/addNewItemTextView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="-1dp"
                    android:layout_marginTop="16dp"
                    android:layout_marginEnd="70dp"
                    android:clickable="true"
                    android:focusable="true"
                    android:onClick="addNewItem"
                    android:text="@string/add_new_item"
                    android:textColor="#000000"
                    android:textSize="30sp"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toEndOf="@+id/plusImageView"
                    app:layout_constraintTop_toTopOf="parent" />

                <ImageView
                    android:id="@+id/plusImageView"
                    android:layout_width="67dp"
                    android:layout_height="74dp"
                    android:layout_marginStart="56dp"
                    android:clickable="false"
                    android:focusable="true"
                    android:onClick="addNewItem"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    app:srcCompat="@drawable/plus" />
            </androidx.constraintlayout.widget.ConstraintLayout>
        </LinearLayout>
    </androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>

AlertDialog添加新项目的代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:orientation="vertical">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:background="#FFFFFF">

        <EditText
            android:id="@+id/nameOfItem"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="100dp"
            android:ems="10"
            android:hint="Enter name"
            android:inputType="textPersonName"
            android:singleLine="true"
            android:textColor="#000000"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="48dp"
            android:layout_marginTop="9dp"
            android:background="@android:drawable/btn_default_small"
            android:onClick="addNewItemButtonClicked"
            android:text="Button"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/nameOfItem" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

看起来您的数据在Adapter的 getView 方法中未正确更新。 contentView 不为空时,您应该尝试更新适配器内的项目。 出于性能原因,适配器内部使用了一种称为ViewHolder的模式。有关更多信息,请参见Android listview using ViewHolder。只需在 contentView 为空时创建Holder,然后在不为空时设置数据。希望对您有所帮助。

P.S。 ListView被认为已弃用。请改用RecyclerView。它已经内置了ViewHolder模式,并且更加灵活