Android - 当我使用 searchview 时列表视图没有更新

问题描述 投票:0回答:1

我是 Android Studio 的新手,有人可以帮助我解决我的问题吗?使用 searchview 后,当我删除列表视图中的项目时,它不会更新。

让我给你截图,以便更好地直观地看到问题

This is the look of the application

The filter works and it shows the Lemon

After using the searchview, I try to delete the Lemon with the toast message but it is still in the listview

  1. 这是我迄今为止所做的工作

主要活动.java

public class MainActivity extends AppCompatActivity
{
    EditText item, price;

    Button add;

    ListView listView;
    SearchView searchBox;
    ArrayList<String> itemList = new ArrayList<>();
    ArrayList<String> searchList = new ArrayList<>();
    ArrayAdapter<String> arrayAdapter;

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

        item = findViewById(R.id.edtProdName);
        price = findViewById(R.id.edtPrice);
        add = findViewById(R.id.btnAdd);
        listView = findViewById(R.id.list);
        searchBox = findViewById(R.id.searchView);

        itemList = FileHelper.readData(this);
        arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, android.R.id.text1, itemList);

        listView.setAdapter(arrayAdapter);
        add.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                String prodName = item.getText().toString();
                String prodPrice = price.getText().toString();
                String product = prodName.toUpperCase() +  " " + "P" +  prodPrice.toUpperCase();
                itemList.add(product);
                item.setText("");
                price.setText("");
                FileHelper.writeData(itemList,getApplicationContext());
                arrayAdapter.notifyDataSetChanged();
                Collections.sort(itemList);
            }
        });

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
                alert.setTitle("Delete");
                alert.setMessage("Do you want to delete this product from the list?");
                alert.setCancelable(false);
                alert.setNegativeButton("No", new DialogInterface.OnClickListener()
                {
                    @Override
                    public void onClick(DialogInterface dialog, int which)
                    {
                        dialog.cancel();
                    }
                });

                alert.setPositiveButton("Yes", new DialogInterface.OnClickListener()
                {
                    @Override
                    public void onClick(DialogInterface dialog, int which)
                    {
                        itemList.remove(position);
                        FileHelper.writeData(itemList, getApplicationContext());
                        arrayAdapter.notifyDataSetChanged();
                        Toast.makeText(MainActivity.this, "Deleted successfully", Toast.LENGTH_SHORT).show();
                    }
                });

                AlertDialog alertDialog = alert.create();
                alertDialog.show();
            }
        });

        searchBox.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText)
            {
                arrayAdapter.getFilter().filter(newText);
                return false;
            }

        });
    }
}

FileHelper.Java(它让我可以将数据保存在我的设备内存中)

public class FileHelper
{
    public static final String FILENAME = "listinfo.dat";

    public static void writeData(ArrayList<String> item, Context context)
    {
        try
        {
            FileOutputStream fos = context.openFileOutput(FILENAME, Context.MODE_PRIVATE);
            ObjectOutputStream oas = new ObjectOutputStream(fos);
            oas.writeObject(item);
            oas.close();
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }

    }

    public static ArrayList<String> readData(Context context)
    {
        ArrayList<String> itemList = null;
        try
        {
            FileInputStream fis = context.openFileInput(FILENAME);
            ObjectInputStream ois = new ObjectInputStream(fis);
            itemList = (ArrayList<String>) ois.readObject();
        }
        catch (FileNotFoundException e)
        {
            itemList = new ArrayList<>();
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        }
        return itemList;
    }
}
java android listview searchview
1个回答
0
投票

您从原始列表中删除了该项目,但没有删除数组适配器中的列表。因此,即使在您拨打

notifyDataSetChanged
之后,该项目仍然在列表中。做:

...
String itemToRemove = itemList.remove(position);
arrayAdapter.remove(itemToRemove);    
arrayAdapter.notifyDataSetChanged();
...

在你的积极按钮回调中。

© www.soinside.com 2019 - 2024. All rights reserved.