Android - The listview is not updating when I use searchview

50 Views Asked by At

I am novice in Android Studio, can someone help me with my problem. After I use searchview, when I delete a item in the listview it is not updating.

Let me give you the screenshot to better visualize the problem

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. Here's what I been working on so far

Main activity.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 (which let me save the data in here in my device memory)

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

There are 2 best solutions below

0
Stephan On

You delete the item from the original list, but not the list in your ArrayAdapter. Therefore the item is still in the list, even after you call notifyDataSetChanged. Do:

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

in your positive button callback.

0
ShayFox On

Hi even if i can't explain why that didn't work, we can fix that with differents way.

  • ArraysAdapters can be capricious, you can just reset it but that isn't best way.
  • You can create you on adapter with new class extends BaseAdapter

That exemple how you can make that:

public class CustomAdapter extends BaseAdapter {

    ArrayList<String> itemList = new ArrayList<>();

    @Override
    public int getCount() {
        return itemList.size();
    }

    @Override
    public Object getItem(int i) {
        return itemList.get(i)
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        view = inflater.inflate(android.R.layout.simple_list_item_1, null);
        String item = itemList.get(i);
        TextView textview = view.findViewById(android.R.id.text1);
        textview.setText(item);

        return view;
    }

Hoping to have helped you, Shayfox.