Home > other >  How to remove everything from ListView using a button onClick?
How to remove everything from ListView using a button onClick?

Time:05-15

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();
    }
}

CodePudding user response:

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();
    }
}

CodePudding user response:

After clearing the ArrayList you have to notify the adapter that the data source has been changed.

public class OrderPage extends AppCompatActivity {
    
    private ListView orders_list;

    @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());
        }
        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();
        orders_list.notifyDataSetChanged();
    }
}
  • Related