Skip to content
Advertisement

How to remove everything from ListView using a button onClick?

How to remove everything from ListView using a button onClick? When i try “fullCourseList.clear();”, I can’t add any more courses and the page is refreshed only after visiting the page again

import static com.example.diplom.MainActivity.fullCourseList;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.example.diplom.model.Course;
import com.example.diplom.model.Order;

import java.util.ArrayList;
import java.util.List;

public class OrderPage extends AppCompatActivity {

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

        ListView orders_list = findViewById(R.id.orders_list);

        List<String> coursesTitle = new ArrayList<>();
        for (Course c : MainActivity.fullCourseList) {
            if(Order.items_id.contains(c.getId()))
                coursesTitle.add(c.getTitle());

        }

        orders_list.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, coursesTitle));


    }

    public void openMain(View view){
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        startActivity(intent);
    }
    public void onClick(View v) {
        //fullCourseList.clear();
    }
}

Advertisement

Answer

You should save off the adapter so you can call clear() on it. Clearing the list this way will also automatically notify the adapter to update. Since you copied your data into a new list (coursesTitle) clearing the original list will have no immediate effect.

For example:

public class OrderPage extends AppCompatActivity {
    
    private ListView orders_list;
    private ArrayAdapter<String> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_page);
        orders_list = findViewById(R.id.orders_list);
        List<String> coursesTitle = new ArrayList<>();
        for (Course c : MainActivity.fullCourseList) {
            if(Order.items_id.contains(c.getId()))
                coursesTitle.add(c.getTitle());
        }
        adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, coursesTitle)
        orders_list.setAdapter(adapter);
    }

    public void onClick(View v) {
        adapter.clear();
    }
}

Edit

If you also want to clear the currently displayed course items out of the master list, you would need to add code like this as well. If that isn’t want you want, you need to be more clear in your question about the desired behavior.

public void onClick(View v) {
    // clear what is currently shown in the list
    adapter.clear();

    // Clear the currently displayed entries out of the master list.
    // You may also be able to use "removeIf" if you have a new
    // enough java/api version
    Iterator<Course> itr = MainActivity.fullCourseList.iterator();
    while (itr.hasNext()) {
        Course c = itr.next();
        if(Order.items_id.contains(c.getId())) {
            itr.remove();
        }
    }

    // And if you want to ENTIRELY clear the master list, you
    // could just do this instead
    // MainActivity.fullCourseList.clear()
}
        
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement