How to refresh list view immediately in android

I am creating an android application that consists of a list view data inside list view was populated from sqlite and here when i delete a record from list view.It was not showing the updated list view immediately.I used the notifyOndataSetChanged() but it wont works please help me with this
I used list view downloaded from here

package com.developer.milanandroid;

import android.app.ActionBar;
import android.app.Activity;
import android.database.Cursor;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.gc.materialdesign.widgets.Dialog;

import com.milan.emptylayout.layout.EmptyLayout;
import com.milan.lib.progressgenarator.lib.ProgressGenerator;
import com.milan.lib.progressgenarator.lib.ProgressGenerator.OnCompleteListener;
import com.milan.swipemenulistview.SwipeMenu;
import com.milan.swipemenulistview.SwipeMenuCreator;
import com.milan.swipemenulistview.SwipeMenuItem;
import com.milan.swipemenulistview.SwipeMenuListView;
import com.milan.swipemenulistview.SwipeMenuListView.OnMenuItemClickListener;
import com.milan.toast.lib.LoadToast;
import com.processbutton.lib.milan.ActionProcessButton;

public class DatabaseListView extends Activity implements OnCompleteListener {
    MediaPlayer media_player;
    ActionProcessButton fetch_database;
    SwipeMenuListView database_results;
    LoginDataBaseAdapter logindatabase_adapter;
    SimpleCursorAdapter cursoradapter;
    Cursor cursor;
    TextView username_txt,password_txt;
    String user_name_string,password_string;
    String text;
    Dialog dialog;
    EmptyLayout empty_layout;
    View.OnClickListener emptyClickListener;
    String[] from;
    int[] to;
    LoadToast load_toast;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.databaselistview);

        View.OnClickListener mErrorClickListener = new OnClickListener() {          
            @Override
            public void onClick(View v) {
                    fetch_database.setText("Checking...");
                    empty_layout.showLoading();
                    ProgressGenerator pg = new ProgressGenerator(DatabaseListView.this);
                    pg.start(fetch_database);


            }
        };

        emptyClickListener = new OnClickListener() {

            @Override
            public void onClick(View v) {

                Toast.makeText(DatabaseListView.this, "Try again button clicked", Toast.LENGTH_LONG).show();

            }
        };

        ActionBar actionbar = getActionBar();
        actionbar.hide();
        View v;

        LinearLayout linear = (LinearLayout)findViewById(R.id.linearLayout1);
        username_txt = (TextView)linear.getChildAt(0);
        password_txt = (TextView)linear.getChildAt(0);
        user_name_string = username_txt.getText().toString();
        password_string = password_txt.getText().toString();
        fetch_database = (ActionProcessButton)findViewById(R.id.Button_Fetch_from_Database);
        database_results = (SwipeMenuListView)findViewById(R.id.listview_database);
        final ProgressGenerator progressGenerator = new ProgressGenerator(DatabaseListView.this);
        logindatabase_adapter = new LoginDataBaseAdapter(DatabaseListView.this);

        empty_layout = new EmptyLayout(DatabaseListView.this, database_results);
        empty_layout.setErrorButtonClickListener(mErrorClickListener);

        fetch_database.setMode(ActionProcessButton.Mode.PROGRESS);
        fetch_database.setOnClickListener(new View.OnClickListener() {

            @SuppressWarnings("deprecation")
            @Override
            public void onClick(View v) {

                progressGenerator.start(fetch_database);
                fetch_database.setText("Checking...");
                fetch_database.setEnabled(false);
                //empty_layout.showLoading();
                media_player = MediaPlayer.create(DatabaseListView.this, R.raw.retrievingfromdatabase);
                media_player.start();

                String[] from = {LoginDataBaseAdapter.USER_NAME,LoginDataBaseAdapter.USER_PASSWORD};
                int[] to = {R.id.txt_username,R.id.txt_pasword};
                cursor = logindatabase_adapter.feching_Data();
                cursoradapter = new SimpleCursorAdapter(DatabaseListView.this, R.layout.listcell, cursor, from, to);
                cursoradapter.notifyDataSetChanged();
                //database_results.setAdapter(cursoradapter);
                //registerForContextMenu(database_results);
                String Text_for_toast = "Loading...Please wait...";
                load_toast = new LoadToast(DatabaseListView.this).setText(Text_for_toast).setTranslationY(500).show();
                ViewGroup view = (ViewGroup)findViewById(android.R.id.content);
                View view_child = new View(DatabaseListView.this);
                view.addView(view_child, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,1200));
            }
        });


        SwipeMenuCreator swipe_list_view = new SwipeMenuCreator() {

            @Override
            public void create(SwipeMenu menu) {

                SwipeMenuItem open_swipemenu = new SwipeMenuItem(DatabaseListView.this);
                open_swipemenu.setBackground(new ColorDrawable(Color.rgb(0x9B,0x33,0xF0)));
                open_swipemenu.setWidth(dp2px(90));
                open_swipemenu.setIcon(R.drawable.databasedelete);
                menu.addMenuItem(open_swipemenu);
            }
        };

        database_results.setMenuCreator(swipe_list_view);
        database_results.setOnMenuItemClickListener(new OnMenuItemClickListener() {

            @Override
            public boolean onMenuItemClick(final int position, SwipeMenu menu, int index) {

            switch(index)   {
            case 0:

                dialog = new Dialog(DatabaseListView.this, "Delete Record", "Do you want to delete Record from database");
                dialog.setCancelable(false);
                dialog.setOnAcceptButtonClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        cursor  = (Cursor) database_results.getItemAtPosition(position);
                        final int item_id = cursor.getInt(cursor.getColumnIndex(LoginDataBaseAdapter.ID));
                        cursor.getString(cursor.getColumnIndex(LoginDataBaseAdapter.USER_NAME));
                        cursor.getString(cursor.getColumnIndex(LoginDataBaseAdapter.USER_PASSWORD));
                        logindatabase_adapter.deleteEntry(item_id);
                        //cursoradapter = new SimpleCursorAdapter(DatabaseListView.this,   R.layout.listcell, cursor, from, to);
                        cursoradapter.notifyDataSetChanged();
                        database_results.setAdapter(cursoradapter);


                        //database_results.setAdapter(cursoradapter);
                        //database_results.getAdapter().notify();
                    }
                });

                dialog.show();      

            }

                return false;
            }

        });


    }

    protected int dp2px(int dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
                getResources().getDisplayMetrics());
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.database_list_view, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }



    @Override
    public void onComplete() {
        if(cursor!=null && cursor.getCount()>0){
        database_results.setAdapter(cursoradapter);
        fetch_database.setEnabled(false);
        fetch_database.setText("SUCCESS");
        load_toast.success();
        media_player = MediaPlayer.create(DatabaseListView.this, R.raw.recordssuccess);
        media_player.start();
        }
        else{
            /*fetch_database.setEnabled(false);
            fetch_database.setText("OOPS");
            fetch_database.setBackgroundColor(Color.parseColor("#ffb74d"));
            final Dialog dialog_database = new Dialog(DatabaseListView.this, "Database Records", "No Records was found in the database");
                                    dialog_database.setOnAcceptButtonClickListener(new View.OnClickListener() {

                                        @Override
                                        public void onClick(View v) {
                                            dialog_database.cancel();
                                        }
                                    });
                                    dialog_database.show();*/
            fetch_database.setText("Oops..No items in database");
            fetch_database.setBackgroundColor(Color.parseColor("#ffb74d"));
            media_player = MediaPlayer.create(DatabaseListView.this, R.raw.norecordsindatabase);
            media_player.start();
            //empty_layout.showEmpty();
            load_toast.error();


        }


    }


}


Source: android

Leave a Reply