Example usage for android.widget ArrayAdapter notifyDataSetChanged

List of usage examples for android.widget ArrayAdapter notifyDataSetChanged

Introduction

In this page you can find the example usage for android.widget ArrayAdapter notifyDataSetChanged.

Prototype

@Override
    public void notifyDataSetChanged() 

Source Link

Usage

From source file:com.example.meind.meinders_habittracker.MainActivity.java

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

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);//from  ww w.  jav  a 2  s  .  c  om

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar,
            R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    //getting list of habits
    ListView listview = (ListView) findViewById(R.id.HabitList);
    final ArrayList<Habit> habitsArrayList = HabitListController.getHabitList().getAllHabits();
    final ArrayAdapter<Habit> HabitAdapter = new ArrayAdapter<Habit>(this, android.R.layout.simple_list_item_1,
            habitsArrayList);
    listview.setAdapter(HabitAdapter);

    //added observer
    HabitListController.getHabitList().addListener(new Listener() {
        @Override
        public void update() {
            HabitAdapter.notifyDataSetChanged();
        }
    });

    //deleting habit with long click
    listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long id) {
            AlertDialog.Builder adb = new AlertDialog.Builder(MainActivity.this);
            adb.setMessage(
                    "Delete " + HabitListController.getHabitList().getHabit(position).HabittoString() + "?");
            adb.setCancelable(true);
            final int finalPosition = position;
            adb.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Habit habit = habitsArrayList.get(finalPosition);
                    HabitListController.getHabitList().removeHabit(habit);
                }
            });
            adb.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                }
            });
            adb.show();
            return false;

        }

    });

    //taken to singular activity when clicked using intent
    listview.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
            HabitListController.setViewHabit(position);
            Intent intent = new Intent(MainActivity.this, SingularHabit.class);
            startActivity(intent);

        }

    });

}

From source file:ca.ualberta.cs.shoven_habittracker.MainActivity.java

public void updateScreen() {
    ListView listView = (ListView) findViewById(R.id.mainHabitsListView);
    final ArrayList<Habit> habitList = controller.getDailySchedule(dayOfWeek).getHabits();
    final ArrayAdapter<Habit> habitAdapter = new ArrayAdapter<Habit>(MainActivity.this,
            android.R.layout.simple_list_item_1, habitList);
    listView.setAdapter(habitAdapter);//w w w . ja  va  2  s.c  o m

    listView.setOnItemClickListener(MainActivity.this);

    WeeklyScheduleController.getWeeklySchedule().addListener(new Listener() {
        @Override
        public void update() {
            habitAdapter.notifyDataSetChanged();
        }
    });
}

From source file:com.nextgis.woody.fragment.ListViewFragment.java

private void setAdapter() {
    if (listView != null) {
        ArrayAdapter<String> adapter;
        int currentApiVersion = android.os.Build.VERSION.SDK_INT;
        if (currentApiVersion >= android.os.Build.VERSION_CODES.HONEYCOMB) {
            adapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_list_item_activated_1,
                    listContent);//from ww w . j ava 2s.c o  m
        } else {
            adapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_list_item_single_choice,
                    listContent);
        }

        listView.setAdapter(adapter);

        listView.setItemChecked(selection, true);
        adapter.notifyDataSetChanged();
    }
}

From source file:com.example.mapsv2.ClustersAreListsActivity.java

private void showInfo(final int x, int y, final Cluster cluster) {
    final int orientation = getResources().getConfiguration().orientation;

    // (re-)Load cluster data into the ListView
    @SuppressWarnings("unchecked")
    ArrayAdapter<String> adapter = (ArrayAdapter<String>) listView.getAdapter();
    adapter.clear();// w w w .ja  va2 s . c o m

    for (Marker m : cluster.markers) {
        adapter.add(m.getTitle());
    }
    adapter.notifyDataSetChanged();

    // Reconfigure the layout params to position the info window on screen
    FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) infoWindow.getLayoutParams();

    if (orientation == Configuration.ORIENTATION_PORTRAIT) {
        lp.topMargin = y;
        lp.leftMargin = defaultMargin;
        lp.rightMargin = defaultMargin;
        lp.width = LayoutParams.MATCH_PARENT;
        lp.height = LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.LEFT | Gravity.TOP;
        infoWindow.setBackgroundResource(R.drawable.info_window_bg_up);
    } else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
        lp.leftMargin = x + defaultMargin;
        lp.topMargin = defaultMargin;
        lp.bottomMargin = defaultMargin;
        lp.gravity = Gravity.LEFT | Gravity.CENTER_VERTICAL;
        lp.width = LayoutParams.WRAP_CONTENT;
        lp.height = LayoutParams.WRAP_CONTENT;
        infoWindow.setBackgroundResource(R.drawable.info_window_bg_left);
    }
    infoWindow.setLayoutParams(lp);
    fullScreenOverlay.setVisibility(View.VISIBLE);
}

From source file:org.solovyev.android.calculator.history.AbstractCalculatorHistoryFragment.java

private void updateAdapter() {
    final List<CalculatorHistoryState> historyList = getHistoryList();

    final ArrayAdapter<CalculatorHistoryState> adapter = getAdapter();
    try {//from www.j  a  v  a2 s  .  c  o  m
        adapter.setNotifyOnChange(false);
        adapter.clear();
        for (CalculatorHistoryState historyState : historyList) {
            adapter.add(historyState);
        }
    } finally {
        adapter.setNotifyOnChange(true);
    }

    adapter.notifyDataSetChanged();
}

From source file:com.mobicage.rogerthat.plugins.friends.RecommendServiceActivity.java

private void configureMailView() {
    T.UI();/*from   w w w  .j a v a2s.  c  om*/
    final AutoCompleteTextView emailText = (AutoCompleteTextView) findViewById(R.id.recommend_email_text_field);
    emailText.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, new ArrayList<String>()));
    emailText.setThreshold(1);

    if (mService.isPermitted(Manifest.permission.READ_CONTACTS)) {
        mService.postAtFrontOfBIZZHandler(new SafeRunnable() {

            @SuppressWarnings("unchecked")
            @Override
            protected void safeRun() throws Exception {
                L.d("RecommendServiceActivity getEmailAddresses");
                List<String> emailList = ContactListHelper.getEmailAddresses(RecommendServiceActivity.this);
                ArrayAdapter<String> a = (ArrayAdapter<String>) emailText.getAdapter();
                for (int i = 0; i < emailList.size(); i++) {
                    a.add(emailList.get(i));
                }
                a.notifyDataSetChanged();
                L.d("RecommendServiceActivity gotEmailAddresses");
            }
        });
    }

    final SafeViewOnClickListener onClickListener = new SafeViewOnClickListener() {
        @Override
        public void safeOnClick(View v) {
            String email = emailText.getText().toString().trim();
            if (RegexPatterns.EMAIL.matcher(email).matches()) {
                mFriendsPlugin.shareService(mServiceEmail, email);
                emailText.setText(null);
                UIUtils.hideKeyboard(RecommendServiceActivity.this, emailText);

                AlertDialog.Builder builder = new AlertDialog.Builder(RecommendServiceActivity.this);
                builder.setMessage(R.string.service_recommendation_sent);
                builder.setPositiveButton(R.string.rogerthat, null);
                builder.create().show();
            } else {
                AlertDialog.Builder builder = new AlertDialog.Builder(RecommendServiceActivity.this);
                builder.setMessage(R.string.registration_email_not_valid);
                builder.setPositiveButton(R.string.rogerthat, null);
                builder.create().show();
            }
        }
    };

    ((Button) findViewById(R.id.recommend_email_button)).setOnClickListener(onClickListener);

    emailText.setOnEditorActionListener(new OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_DONE || (event.getKeyCode() == KeyEvent.KEYCODE_ENTER
                    && event.getAction() == KeyEvent.ACTION_DOWN)) {
                onClickListener.onClick(view);
                return true;
            }
            return false;
        }
    });
}

From source file:com.mobshep.mobileshepherd.UDataLeakage.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.udl_layout);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);/*from  ww w  .  j  a  v  a  2  s .com*/

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar,
            R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    String destinationDir = "/data/data/" + getPackageName() + "/files/";

    String destinationPath = destinationDir + "Tue Jul 08 172618 EDT 2014";

    File f = new File(destinationPath);

    if (!f.exists()) {
        File directory = new File(destinationDir);
        directory.mkdirs();
        //assets members.db -> /databases/

        try {
            copyKey(getBaseContext().getAssets().open("Tue Jul 08 172618 EDT 2014"),
                    new FileOutputStream(destinationPath));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    ListView noteList = (ListView) findViewById(R.id.noteList);
    final EditText miniNote = (EditText) findViewById(R.id.miniNote);
    final ArrayList<String> noteItems = new ArrayList<String>();
    final ArrayAdapter<String> arrayAdapter;

    arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, noteItems);

    noteList.setAdapter(arrayAdapter);

    miniNote.setOnKeyListener(new View.OnKeyListener() {
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (event.getAction() == KeyEvent.ACTION_DOWN)
                if ((keyCode == KeyEvent.KEYCODE_DPAD_CENTER) || keyCode == KeyEvent.KEYCODE_ENTER) {

                    String Log = miniNote.getText().toString();

                    logDetails(Log);

                    noteItems.add(0, miniNote.getText().toString());
                    arrayAdapter.notifyDataSetChanged();
                    miniNote.setText("");
                    miniNote.setTextColor(Color.WHITE);

                    return true;
                }
            return false;
        }
    }

    );

}

From source file:com.coderdojo.libretalk.MainActivity.java

private final void printMsg(final String message) {
    ArrayAdapter<SpannableString> adapter = new ArrayAdapter<SpannableString>(this,
            android.R.layout.simple_list_item_1, mMessageListArray);
    ListView listView = (ListView) findViewById(R.id.message_list);
    listView.setAdapter(adapter);// ww  w. j a  v  a 2  s . c  om
    listView.setStackFromBottom(true);

    mMessageListArray.add(new SpannableString(message));
    adapter.notifyDataSetChanged();
}

From source file:com.mifos.mifosxdroid.online.generatecollectionsheet.GenerateCollectionSheetFragment.java

private void setSpinner(Spinner spinner, List<String> values) {
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item,
            values);/*from   ww w  . j a v  a  2 s.c  om*/
    adapter.notifyDataSetChanged();
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
}

From source file:com.audiokernel.euphonyrmt.fragments.QueueFragment.java

/**
 * Updates the scrollbar./*from   w ww  .j a  v  a  2s.  co  m*/
 *
 * @param newSongList   The updated list of songs for the playlist.
 * @param listPlayingID The current playing playlist id.
 */
protected void updateScrollbar(final ArrayList newSongList, final int listPlayingID) {
    mActivity.runOnUiThread(new Runnable() {
        /**
         * This is a helper method to workaround shortcomings of the fast scroll API.
         *
         * @param scrollbarStyle The {@code View} scrollbar style.
         * @param isAlwaysVisible The visibility of the scrollbar.
         */
        private void refreshFastScrollStyle(final int scrollbarStyle, final boolean isAlwaysVisible) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                mList.setFastScrollAlwaysVisible(isAlwaysVisible);
                mList.setScrollBarStyle(scrollbarStyle);
            } else {
                mList.setScrollBarStyle(scrollbarStyle);
                mList.setFastScrollAlwaysVisible(isAlwaysVisible);
            }
        }

        @Override
        public void run() {
            final int firstVisibleElementIndex = mList.getFirstVisiblePosition();
            final View firstVisibleItem = mList.getChildAt(0);
            final int firstVisiblePosition;
            final ArrayAdapter songs = new QueueAdapter(mActivity, R.layout.playlist_queue_item, newSongList);

            if (firstVisibleItem != null) {
                firstVisiblePosition = firstVisibleItem.getTop();
            } else {
                firstVisiblePosition = 0;
            }

            setListAdapter(songs);
            mSongList = newSongList;
            songs.notifyDataSetChanged();

            /**
             * Note : Setting the scrollbar style before setting the fast scroll state is very
             * important pre-KitKat, because of a bug. It is also very important post-KitKat
             * because it needs the opposite order or it won't show the FastScroll.
             *
             * This is so stupid I don't even .... argh.
             */
            if (newSongList.size() >= MIN_SONGS_BEFORE_FASTSCROLL) {
                refreshFastScrollStyle(View.SCROLLBARS_INSIDE_INSET, true);
            } else {
                refreshFastScrollStyle(View.SCROLLBARS_INSIDE_OVERLAY, false);
            }

            if (mActionMode != null) {
                mActionMode.finish();
            }

            // Restore the scroll bar position
            if (firstVisibleElementIndex == 0 || firstVisiblePosition == 0) {
                /**
                 * Only scroll if there is a valid song to scroll to. 0 is a valid song but
                 * does not require scroll anyway. Also, only scroll if it's the first update.
                 * You don't want your playlist to scroll itself while you are looking at other
                 * stuff.
                 */
                if (listPlayingID > 0 && getView() != null) {
                    setSelection(listPlayingID);
                }
            } else {
                mList.setSelectionFromTop(firstVisibleElementIndex, firstVisiblePosition);
            }
        }
    });
}