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
- 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;
}
}
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 callnotifyDataSetChanged. Do:in your positive button callback.