nck nck - 1 year ago 70
Android Question

How can I reload an activity from a listview to update information of a database?

I'm trying to implement something like this:


I have a main class with a layout from where I get some data from the database and plot it as shown in the image (with another layout complex_list):

public class main_class extends Activity{
DatabaseHelper myDB;
ListView list;
protected void onCreate(Bundle savedInstanceState){

myDB=new DatabaseHelper(this);
list = (ListView) findViewById(;
Cursor res = myDB.getAllData();
if (res.getCount()==0){
list.setAdapter(new listAdapter(..., this.getBaseContext()));

I've a class to implement the BaseAdapter:

class listAdapter extends BaseAdapter {
public View getView(final int position, View convertView, final ViewGroup parent) {

inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE );
View row;
row = inflater.inflate(R.layout.complex_list, parent, false);
Delete = (Button) row.findViewById(;
Delete.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent refresh = new Intent(context, main_class.class);
refresh.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //I've added this line because otherwise I get an error an the app crashes.
return (row);
public void deleteEntry ( int id){
DatabaseHelper myDB = new DatabaseHelper(context);

What I'm trying to do is refreshing the whole activity once the delete button is pressed to see an updated version of the database.

What I've written does work, but when I press the return button in the phone it returns to the old image of the database instead of going to the menu from where I accessed to this activity.

I've been researching similar questions and the only solution I've found is to add the line which is commented:


But it makes my app crash with this error:

java.lang.ClassCastException: cannot be cast to

Could anyone give me a hint of what I'm doing wrong or if there is an easier way to this?

Answer Source
ClassCastException: cannot be cast to

Probably starts from

list.setAdapter(new listAdapter(..., this.getBaseContext()));

You should use this in place of this.getBaseContext() there as Activity extends Context

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download