Example usage for android.widget ListView setAdapter

List of usage examples for android.widget ListView setAdapter

Introduction

In this page you can find the example usage for android.widget ListView setAdapter.

Prototype

@Override
public void setAdapter(ListAdapter adapter) 

Source Link

Document

Sets the data behind this ListView.

Usage

From source file:com.example.android.gft.PhrasesFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.word_list, container, false);

    // Create and setup the {@link AudioManager} to request audio focus
    mAudioManager = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);

    // Create a list of words
    final ArrayList<Word> words = new ArrayList<Word>();
    words.add(new Word(R.string.phrase_where_are_you_going, R.string.miwok_phrase_where_are_you_going,
            R.raw.phrase_where_are_you_going));
    words.add(new Word(R.string.phrase_what_is_your_name, R.string.miwok_phrase_what_is_your_name,
            R.raw.phrase_what_is_your_name));
    words.add(new Word(R.string.phrase_my_name_is, R.string.miwok_phrase_my_name_is, R.raw.phrase_my_name_is));
    words.add(new Word(R.string.phrase_how_are_you_feeling, R.string.miwok_phrase_how_are_you_feeling,
            R.raw.phrase_how_are_you_feeling));
    words.add(new Word(R.string.phrase_im_feeling_good, R.string.miwok_phrase_im_feeling_good,
            R.raw.phrase_im_feeling_good));
    words.add(new Word(R.string.phrase_are_you_coming, R.string.miwok_phrase_are_you_coming,
            R.raw.phrase_are_you_coming));
    words.add(new Word(R.string.phrase_yes_im_coming, R.string.miwok_phrase_yes_im_coming,
            R.raw.phrase_yes_im_coming));
    words.add(new Word(R.string.phrase_im_coming, R.string.miwok_phrase_im_coming, R.raw.phrase_im_coming));
    words.add(new Word(R.string.phrase_lets_go, R.string.miwok_phrase_lets_go, R.raw.phrase_lets_go));
    words.add(new Word(R.string.phrase_come_here, R.string.miwok_phrase_come_here, R.raw.phrase_come_here));

    // Create an {@link WordAdapter}, whose data source is a list of {@link Word}s. The
    // adapter knows how to create list items for each item in the list.
    WordAdapter adapter = new WordAdapter(getActivity(), words, R.color.category_phrases);

    // Find the {@link ListView} object in the view hierarchy of the {@link Activity}.
    // There should be a {@link ListView} with the view ID called list, which is declared in the
    // word_list.xml layout file.
    ListView listView = (ListView) rootView.findViewById(R.id.list);

    // Make the {@link ListView} use the {@link WordAdapter} we created above, so that the
    // {@link ListView} will display list items for each {@link Word} in the list.
    listView.setAdapter(adapter);

    // Set a click listener to play the audio when the list item is clicked on
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override/*from w ww  .j a  v  a 2  s  .  c  om*/
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            // Release the media player if it currently exists because we are about to
            // play a different sound file
            releaseMediaPlayer();

            // Get the {@link Word} object at the given position the user clicked on
            Word word = words.get(position);

            // Request audio focus so in order to play the audio file. The app needs to play a
            // short audio file, so we will request audio focus with a short amount of time
            // with AUDIOFOCUS_GAIN_TRANSIENT.
            int result = mAudioManager.requestAudioFocus(mOnAudioFocusChangeListener, AudioManager.STREAM_MUSIC,
                    AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);

            if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
                // We have audio focus now.

                // Create and setup the {@link MediaPlayer} for the audio resource associated
                // with the current word
                mMediaPlayer = MediaPlayer.create(getActivity(), word.getAudioResourceId());

                // Start the audio file
                mMediaPlayer.start();

                // Setup a listener on the media player, so that we can stop and release the
                // media player once the sound has finished playing.
                mMediaPlayer.setOnCompletionListener(mCompletionListener);
            }
        }
    });

    return rootView;
}

From source file:com.linkbubble.util.YouTubeEmbedHelper.java

AlertDialog getEmbedResultsDialog() {
    if (mEmbedInfo.size() > 0) {
        ListView listView = new ListView(mContext);
        listView.setAdapter(new EmbedItemAdapter());

        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setView(listView);/*from  w  w w.  j av a  2s.co m*/
        builder.setIcon(mYouTubeResolveInfo.loadIcon(mContext.getPackageManager()));
        builder.setTitle(R.string.title_youtube_embed_to_load);

        final AlertDialog alertDialog = builder.create();
        alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                EmbedInfo embedInfo = (EmbedInfo) view.getTag();
                if (embedInfo != null) {
                    loadYouTubeVideo(embedInfo.mId);
                }
                alertDialog.dismiss();
            }
        });

        return alertDialog;
    } else {
        final AlertDialog alertDialog = new AlertDialog.Builder(mContext).create();
        alertDialog.setTitle(R.string.youtube_embed_error_title);
        alertDialog.setMessage(mContext.getString(R.string.youtube_embed_error_summary));
        alertDialog.setButton(AlertDialog.BUTTON_POSITIVE,
                mContext.getResources().getString(R.string.action_ok), new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        alertDialog.dismiss();
                    }

                });
        alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        return alertDialog;
    }
}

From source file:com.ferid.app.frequentcontacts.selectnumber.SelectNumberActivity.java

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

    //init toolbar
    final Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);// w  w w.ja  v a2 s. c  o  m

    if (getSupportActionBar() != null) {
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
    }
    //---

    ListView list = findViewById(R.id.list);
    adapter = new NumberAdapter(this, R.layout.select_number_row, numberList);

    list.setAdapter(adapter);

    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            Contact contact = numberList.get(position);

            //retrieve and set photo of the selected contact
            contact.setPhoto(retrievePhoto(contact.getId()));

            Intent intent = new Intent();
            intent.putExtra("contact", contact);
            setResult(RESULT_OK, intent);
            closeWindow();
        }
    });

    progressBar = findViewById(R.id.progressBar);

    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED) { //if permission is already granted

        new NumberListRetriever().execute();
    }
}

From source file:cn.com.hoonsoft.example.pulltorefresh.PullToRefreshListFragmentActivity.java

/** Called when the activity is first created. */
@Override/*from  w ww.j av  a 2 s .  c om*/
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ptr_list_fragment);

    mPullRefreshListFragment = (PullToRefreshListFragment) getSupportFragmentManager()
            .findFragmentById(R.id.frag_ptr_list);

    // Get PullToRefreshListView from Fragment
    mPullRefreshListView = mPullRefreshListFragment.getPullToRefreshListView();

    // Set a listener to be invoked when the list should be refreshed.
    mPullRefreshListView.setOnRefreshListener(this);

    // You can also just use mPullRefreshListFragment.getListView()
    ListView actualListView = mPullRefreshListView.getRefreshableView();

    mListItems = new LinkedList<String>();
    mListItems.addAll(Arrays.asList(mStrings));
    mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mListItems);

    // You can also just use setListAdapter(mAdapter) or
    // mPullRefreshListView.setAdapter(mAdapter)
    actualListView.setAdapter(mAdapter);

    mPullRefreshListFragment.setListShown(true);
}

From source file:co.edu.uniajc.vtf.content.ListSitesFragment.java

@Override
public void setAdapter(ListPointsAdapter pAdapter) {
    //unknown exception here, I don't know what happen but
    //I insert this ex block for protect the app against this bug.
    try {//from  w  w  w  . j  a  v  a2  s. c o m
        ListView loList = (ListView) this.getView().findViewById(R.id.lstPoints);
        loList.setAdapter(pAdapter);
    } catch (Exception ex) {

    }
}

From source file:com.springsource.greenhouse.twitter.TweetDetailsActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.tweet_details);

    final ListView listView = (ListView) findViewById(R.id.tweet_details_menu);

    String[] menu_items = getResources().getStringArray(R.array.tweet_details_options_array);
    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.menu_list_item, menu_items);
    listView.setAdapter(arrayAdapter);

    listView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            switch (position) {
            case 0:
                Intent replyIntent = new Intent(view.getContext(), PostTweetActivity.class);
                replyIntent.putExtra("reply", tweet.getFromUser());
                startActivity(replyIntent);
                break;
            case 1:
                showRetweetDialog();//from w ww.  ja  v  a  2s  .  c o  m
                break;
            case 2:
                Intent quoteIntent = new Intent(view.getContext(), PostTweetActivity.class);
                String quote = new StringBuilder().append("\"@").append(tweet.getFromUser()).append(" ")
                        .append(tweet.getText()).append("\"").toString();
                quoteIntent.putExtra("quote", quote);
                startActivity(quoteIntent);
                break;
            default:
                break;
            }
        }
    });
}

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

@Override
protected void onResume() {
    super.onResume();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    setNavigator(navigationView);/*from   w  ww .jav a2s. c  om*/

    ListView listView = (ListView) findViewById(R.id.allHabitsListView);
    final ArrayList<Habit> habitList = controller.getAllHabits().getHabitList();
    final ArrayAdapter<Habit> habitAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,
            habitList);
    listView.setAdapter(habitAdapter);
    listView.setOnItemClickListener(AllHabitsActivity.this);

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

From source file:com.entertailion.android.launcher.Dialogs.java

/**
 * Display the list of browser history.//from   w  w w  . j  av a 2s  .  co m
 * 
 * @param context
 */
public static void displayBrowserHistory(final Launcher context) {
    final ArrayList<BookmarkInfo> bookmarks = loadBrowserHistory(context);
    if (bookmarks.size() > 0) {
        final Dialog dialog = new Dialog(context);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.bookmarks_list);

        ListView listView = (ListView) dialog.findViewById(R.id.list);
        listView.setAdapter(new BookmarkAdapter(context, bookmarks));
        listView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                BookmarkInfo bookmark = (BookmarkInfo) parent.getAdapter().getItem(position);
                Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(bookmark.getUrl()));
                context.startActivity(browserIntent);
                context.showCover(false);
                dialog.dismiss();
                Analytics.logEvent(Analytics.INVOKE_BOOKMARK);
            }

        });
        listView.setDrawingCacheEnabled(true);
        listView.setOnKeyListener(onKeyListener);
        dialog.setOnDismissListener(new OnDismissListener() {

            @Override
            public void onDismiss(DialogInterface dialog) {
                context.showCover(false);
            }

        });
        context.showCover(true);
        dialog.show();
        Analytics.logEvent(Analytics.DIALOG_BOOKMARKS);
    } else {
        displayAlert(context, context.getString(R.string.dialog_no_browser_history));
    }
}

From source file:ca.ippetad.geerweekapp.view.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    /** View **//*from  www.j  av a  2s .c o m*/
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main);

    /** FRAGMENTS **/
    _frame = (FrameLayout) findViewById(R.id.container);

    _feed = new FeedFragment();
    _events = new EventsFragment();
    _kidnap = new KidnapFragment();
    _godiva = new GodivaFragment();
    _misc = new MiscFragment();

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction().add(R.id.container, _feed).commit();
    }

    /** SLIDING MENU **/
    _slide = new SlidingMenu(this);
    _slide.setMode(SlidingMenu.LEFT);
    _slide.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
    _slide.setBehindOffset(250);
    _slide.setFadeDegree(.9f);
    _slide.setBackgroundColor(Color.parseColor("#444444"));
    _slide.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
    _slide.setMenu(R.layout.layout_menu);

    _slide.setOnOpenedListener(new SlidingMenu.OnOpenedListener() {
        @Override
        public void onOpened() {
            MENU_STATE = HIDDEN;
            invalidateOptionsMenu();
        }
    });
    _slide.setOnClosedListener(new SlidingMenu.OnClosedListener() {
        @Override
        public void onClosed() {
            MENU_STATE = VISIBLE;
            invalidateOptionsMenu();
        }
    });

    CustomTopBar bar = (CustomTopBar) findViewById(R.id.topbar);
    bar.setOnDrawerClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            _slide.toggle();
        }
    });

    ListView sliderList = (ListView) _slide.getRootView().findViewById(R.id.listview);
    CustomArrayAdapter slider_adapter = new CustomArrayAdapter(this, android.R.layout.simple_list_item_1,
            new String[] { "Home", "Events", "Kidnapping", "Godiva", "Misc" });
    sliderList.setAdapter(slider_adapter);
    sliderList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            switch (i) {
            case 0:
                getSupportFragmentManager().beginTransaction().replace(R.id.container, _feed)
                        .addToBackStack(null).commit();
                break;
            case 1:
                getSupportFragmentManager().beginTransaction().replace(R.id.container, _events)
                        .addToBackStack(null).commit();
                break;
            case 2:
                getSupportFragmentManager().beginTransaction().replace(R.id.container, _kidnap)
                        .addToBackStack(null).commit();
                break;
            case 3:
                getSupportFragmentManager().beginTransaction().replace(R.id.container, _godiva)
                        .addToBackStack(null).commit();
                break;
            case 4:
                getSupportFragmentManager().beginTransaction().replace(R.id.container, _misc)
                        .addToBackStack(null).commit();
                break;
            }
            _slide.toggle();
        }
    });

}

From source file:com.example.android.gft.FamilyFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.word_list, container, false);

    // Create and setup the {@link AudioManager} to request audio focus
    mAudioManager = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);

    // Create a list of words
    final ArrayList<Word> words = new ArrayList<Word>();
    words.add(new Word(R.string.family_father, R.string.miwok_family_father, R.drawable.family_father,
            R.raw.family_father));/*  w  w w  .jav  a 2 s.c  o m*/
    words.add(new Word(R.string.family_mother, R.string.miwok_family_mother, R.drawable.family_mother,
            R.raw.family_mother));
    words.add(
            new Word(R.string.family_son, R.string.miwok_family_son, R.drawable.family_son, R.raw.family_son));
    words.add(new Word(R.string.family_daughter, R.string.miwok_family_daughter, R.drawable.family_daughter,
            R.raw.family_daughter));
    words.add(new Word(R.string.family_older_brother, R.string.miwok_family_older_brother,
            R.drawable.family_older_brother, R.raw.family_older_brother));
    words.add(new Word(R.string.family_younger_brother, R.string.miwok_family_younger_brother,
            R.drawable.family_younger_brother, R.raw.family_younger_brother));
    words.add(new Word(R.string.family_older_sister, R.string.miwok_family_older_sister,
            R.drawable.family_older_sister, R.raw.family_older_sister));
    words.add(new Word(R.string.family_younger_sister, R.string.miwok_family_younger_sister,
            R.drawable.family_younger_sister, R.raw.family_younger_sister));
    words.add(new Word(R.string.family_grandmother, R.string.miwok_family_grandmother,
            R.drawable.family_grandmother, R.raw.family_grandmother));
    words.add(new Word(R.string.family_grandfather, R.string.miwok_family_grandfather,
            R.drawable.family_grandfather, R.raw.family_grandfather));

    // Create an {@link WordAdapter}, whose data source is a list of {@link Word}s. The
    // adapter knows how to create list items for each item in the list.
    WordAdapter adapter = new WordAdapter(getActivity(), words, R.color.category_family);

    // Find the {@link ListView} object in the view hierarchy of the {@link Activity}.
    // There should be a {@link ListView} with the view ID called list, which is declared in the
    // word_list.xml layout file.
    ListView listView = (ListView) rootView.findViewById(R.id.list);

    // Make the {@link ListView} use the {@link WordAdapter} we created above, so that the
    // {@link ListView} will display list items for each {@link Word} in the list.
    listView.setAdapter(adapter);

    // Set a click listener to play the audio when the list item is clicked on
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            // Release the media player if it currently exists because we are about to
            // play a different sound file
            releaseMediaPlayer();

            // Get the {@link Word} object at the given position the user clicked on
            Word word = words.get(position);

            // Request audio focus so in order to play the audio file. The app needs to play a
            // short audio file, so we will request audio focus with a short amount of time
            // with AUDIOFOCUS_GAIN_TRANSIENT.
            int result = mAudioManager.requestAudioFocus(mOnAudioFocusChangeListener, AudioManager.STREAM_MUSIC,
                    AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);

            if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
                // We have audio focus now.

                // Create and setup the {@link MediaPlayer} for the audio resource associated
                // with the current word
                mMediaPlayer = MediaPlayer.create(getActivity(), word.getAudioResourceId());

                // Start the audio file
                mMediaPlayer.start();

                // Setup a listener on the media player, so that we can stop and release the
                // media player once the sound has finished playing.
                mMediaPlayer.setOnCompletionListener(mCompletionListener);
            }
        }
    });

    return rootView;
}