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.evilkhaoskat.sunshine.app.ForecastFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    // The ArrayAdapter will take data from a source and
    // use it to populate the ListView it's attached to.
    mForecastAdapter = new ArrayAdapter<String>(getActivity(), // The current context (this activity)
            R.layout.list_item_forecast, // The name of the layout ID.
            R.id.list_item_forecast_textview, // The ID of the textview to populate.
            new ArrayList<String>());

    View rootView = inflater.inflate(R.layout.fragment_main, container, false);

    // Get a reference to the ListView, and attach this adapter to it.
    ListView listView = (ListView) rootView.findViewById(R.id.listview_forecast);
    listView.setAdapter(mForecastAdapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override/*from  www  .  j  av  a 2 s  .com*/
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            String forecast = mForecastAdapter.getItem(position);
            Intent intent = new Intent(getActivity(), DetailActivity.class).putExtra(Intent.EXTRA_TEXT,
                    forecast);
            startActivity(intent);
        }
    });

    return rootView;
}

From source file:com.dena.app.usage.watcher.fragment.WatchFragment.java

public View onCreateView(LayoutInflater layoutinflater, ViewGroup viewgroup, Bundle bundle) {
    mIsTotal = getArguments().getBoolean("isTotal");
    View view = layoutinflater.inflate(R.layout.fragment_watch, null);
    mDB = ((App) getActivity().getApplication()).getDatabase();
    mSwipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_refresh_layout);
    ListView listView = (ListView) view.findViewById(R.id.listView);
    mAdapter = new WatchAdapter(getActivity(), R.layout.item_watch, new ArrayList(), mDB);
    listView.setAdapter(mAdapter);
    mSwipeRefreshLayout.setOnRefreshListener(this);
    listView.setOnItemClickListener(this);
    onRefresh();/*from w  w w. ja v  a  2  s.c o  m*/
    return view;
}

From source file:com.example.wmck.mysunshine.ForecastFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    Context context = getActivity();

    // Create some dummy data for the ListView.  Here's a sample weekly forecast
    String[] data = { "Mon 6/23- Sunny - 31/17", "Tue 6/24 - Foggy - 21/8", "Wed 6/25 - Cloudy - 22/17",
            "Thurs 6/26 - Rainy - 18/11", "Fri 6/27 - Foggy - 21/10", "Sat 6/28 - WEATHER STN - 23/18",
            "Sun 6/29 - Sunny - 20/7" };
    List<String> weekForecast = new ArrayList<String>(Arrays.asList(data));

    // Now that we have some dummy forecast data, create an ArrayAdapter.
    // The ArrayAdapter will take data from a source (like our dummy forecast) and
    // use it to populate the ListView it's attached to.
    mForecastAdapter = new ArrayAdapter<String>(context, // The current context (this activity)
            R.layout.list_item_forecast, // The name of the layout ID.
            R.id.list_item_forecast_textView, // The ID of the textview to populate.
            weekForecast);//  ww w . j  av a 2s  .co  m

    View rootView = inflater.inflate(R.layout.fragment_main, container, false);

    // Get a reference to the ListView, and attach this adapter to it.
    ListView listView = (ListView) rootView.findViewById(R.id.listView_forecast);
    listView.setAdapter(mForecastAdapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String weatherStr = parent.getItemAtPosition(position).toString();
            //  Toast.makeText(getActivity(),weatherStr,Toast.LENGTH_SHORT).show();
            Intent detailActivityIntent = new Intent(getActivity(), DetailActivity.class);
            detailActivityIntent.putExtra(Intent.EXTRA_TEXT, weatherStr);
            // method startActivity(Intent i) has to be called on the Context object
            startActivity(detailActivityIntent);
        }
    });

    return rootView;
}

From source file:com.anxpp.blog.MainActivity.java

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

    mSettingsChangedListener = new SettingsChangedListener();

    setContentView(R.layout.example);/*from  w  ww. ja va  2  s. c o m*/

    viewActionsContentView = (ActionsContentView) findViewById(R.id.actionsContentView);
    viewActionsContentView.setSwipingType(ActionsContentView.SWIPING_EDGE);

    //??
    final ListView viewActionsList = (ListView) findViewById(R.id.actions);
    //???
    final ActionsAdapter actionsAdapter = new ActionsAdapter(this);
    viewActionsList.setAdapter(actionsAdapter);
    //??
    viewActionsList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapter, View v, int position, long flags) {
            //???
            final Uri uri = actionsAdapter.getItem(position);
            if (EffectsExampleActivity.URI.equals(uri)) {
                startActivity(new Intent(getBaseContext(), EffectsExampleActivity.class));
                return;
            }
            //?fragment
            updateContent(uri);
            viewActionsContentView.showContent();
        }
    });

    if (savedInstanceState != null) {
        currentUri = Uri.parse(savedInstanceState.getString(STATE_URI));
        currentContentFragmentTag = savedInstanceState.getString(STATE_FRAGMENT_TAG);
    }

    updateContent(currentUri);
}

From source file:com.example.demo_dv_fuse.DetailsTab.java

/**
 * @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup,
 *      android.os.Bundle)// ww w . ja  v  a2s  .  c o m
 */
@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
        final Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.details_tab, container, false);

    final String[] choices = { getString(R.string.viewAirportMaps), getString(R.string.viewGoogleMap) };
    final ListView listView = (ListView) view.findViewById(R.id.detailsList);

    final View headerView = inflater.inflate(R.layout.details_list_header, null);
    listView.addHeaderView(headerView, null, false);

    listView.setAdapter(new ArrayAdapter<String>(container.getContext(), R.layout.details_row,
            R.id.details_list_map_row_title, choices));
    listView.setClickable(true);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        /**
         * @see android.widget.AdapterView.OnItemClickListener#onItemClick(android.widget.AdapterView,
         *      android.view.View, int, long)
         */
        @Override
        public void onItemClick(final AdapterView<?> adapterView, final View parent, final int position,
                final long id) {
            if (position == AIRPORT_MAPS_INDEX) {
                handleViewAirportMapsSelected();
            } else if (position == GOOGLE_MAPS_INDEX) {
                handleViewGoogleMap();
            }
        }
    });

    // departure widgets
    this.departureAirlines = (TextView) view.findViewById(R.id.departure_airlines);
    this.departureAirportCode = (TextView) view.findViewById(R.id.departure_airport_code);
    this.departureFlightNumber = (TextView) view.findViewById(R.id.departure_flight_number);
    this.departureIata = (TextView) view.findViewById(R.id.departure_iata);
    this.departureStatus = (TextView) view.findViewById(R.id.departure_status);
    this.departureTime = (TextView) view.findViewById(R.id.departure_time);

    // arrival widgets
    this.arrivalAirportCode = (TextView) view.findViewById(R.id.arrival_airport_code);
    this.arrivalGate = (TextView) view.findViewById(R.id.arrival_gate);
    this.arrivalTerminal = (TextView) view.findViewById(R.id.arrival_terminal);
    this.arrivalTime = (TextView) view.findViewById(R.id.arrival_time);

    return view;
}

From source file:com.example.mohamedsalama.sunshine.MainActivityFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    // The ArrayAdapter will take data from a source and
    // use it to populate the ListView it's attached to.
    mForecastAdapter = new ArrayAdapter<String>(getActivity(), // The current context (this activity)
            R.layout.list_item_forecast, // The name of the layout ID.
            R.id.list_item_forecast_textview, // The ID of the textview to populate.
            new ArrayList<String>());

    View rootView = inflater.inflate(R.layout.fragment_main, container, false);

    // Get a reference to the ListView, and attach this adapter to it.
    ListView listView = (ListView) rootView.findViewById(R.id.Listview_forecast);
    listView.setAdapter(mForecastAdapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override//from   ww w . ja v  a 2s .com
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            String forecast = mForecastAdapter.getItem(position);
            Intent intent = new Intent(getActivity(), DetailActivity.class).putExtra(Intent.EXTRA_TEXT,
                    forecast);
            startActivity(intent);
        }
    });

    return rootView;
}

From source file:co.adrianblan.noraoke.SongNowPlayingFragment.java

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

    ListView listView = (ListView) rootView.findViewById(R.id.now_playing_song_list);
    listView.setAdapter(new SongAdapter(getActivity(), 3));

    //LinearLayout listHeaderView = (LinearLayout)inflater.inflate(R.layout.list_header, null);
    //listView.addHeaderView(listHeaderView);

    fab = (FloatingActionButton) rootView.findViewById(R.id.now_playing_fab);
    fab.attachToListView(listView);/*from w  w w  .j a v  a2 s.  c  om*/

    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getActivity(), SongPlayingActivity.class);
            getActivity().startActivity(intent);
        }
    });

    //Fulhaxx in order to preserve media player when fragment is destroyed
    if (mPlayer == null) {
        mPlayer = MediaPlayer.create(getActivity().getApplicationContext(), R.raw.iwantitthatway);
    }

    //Connect to the websocket if we don't have it
    if (mWebSocketClient == null) {
        connectWebSocket();
    }

    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mPlayer.seekTo(0);
        }
    };

    //Pressing the previous song button just starts it at the beginning
    final Button prev = (Button) rootView.findViewById(R.id.music_control_prev);
    prev.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.ic_skip_previous_black_24dp);
    prev.setOnClickListener(listener);

    final Button next = (Button) rootView.findViewById(R.id.music_control_next);
    next.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.ic_skip_next_black_24dp);
    next.setOnClickListener(listener);

    //Get music control play button
    play = (Button) rootView.findViewById(R.id.music_control_play);

    //Set the play button to its initial state
    if (mPlayer.isPlaying()) {
        play.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.ic_pause_black_24dp);
    } else {
        play.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.ic_play_arrow_black_24dp);
    }

    //Play and pause the song, also change the icons
    play.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (!mPlayer.isPlaying()) {

                //Send the time in milliseconds
                if (mWebSocketClient.getReadyState() == WebSocket.READYSTATE.OPEN) {
                    mWebSocketClient.send(System.currentTimeMillis() + " - " + mPlayer.getCurrentPosition());
                }

                mPlayer.start();
                play.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.ic_pause_black_24dp);
            } else {

                if (mWebSocketClient.getReadyState() == WebSocket.READYSTATE.OPEN) {
                    mWebSocketClient.send("STOP");
                }

                mPlayer.pause();
                play.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.ic_play_arrow_black_24dp);
            }
        }
    });

    //Get music control playlist button
    Button playlist = (Button) rootView.findViewById(R.id.music_control_playlist);

    //We switch the playlist visibility when we press the playlist button
    playlist.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            LinearLayout playlist = (LinearLayout) rootView.findViewById(R.id.now_playing_playlist);

            if (playlist.getVisibility() != View.VISIBLE) {
                playlist.setVisibility(View.VISIBLE);
                Animation slideIn = AnimationUtils.loadAnimation(getActivity().getApplicationContext(),
                        R.anim.slide_in);
                playlist.startAnimation(slideIn);
                fab.hide();
            } else {
                Animation slideOut = AnimationUtils.loadAnimation(getActivity().getApplicationContext(),
                        R.anim.slide_out);
                playlist.startAnimation(slideOut);
                playlist.setVisibility(View.GONE);
                fab.show();
            }
        }
    });

    rootView.findViewById(R.id.now_playing_playlist).setVisibility(View.GONE);

    ViewCompat.setElevation(rootView.findViewById(R.id.music_control), 50);
    return rootView;
}

From source file:com.example_pfe.android.sunshine.ForecastFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    // The ArrayAdapter will take data from a source and
    // use it to populate the ListView it's attached to.
    mForecastAdapter = new ArrayAdapter<String>(getActivity(), // The current context (this activity)
            R.layout.list_item_forecast, // The name of the layout ID.
            R.id.list_item_forecast_textview, // The ID of the textview to populate.
            new ArrayList<String>());

    View rootView = inflater.inflate(R.layout.fragment_main, container, false);

    // Get a reference to the ListView, and attach this adapter to it.
    ListView listView = (ListView) rootView.findViewById(R.id.listview_forecast);
    listView.setAdapter(mForecastAdapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override/*  w  ww . j a  v a  2s  . c o m*/
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            String forecast = mForecastAdapter.getItem(position);
            Intent intent = new Intent(getActivity(), com.example_pfe.android.sunshine.DetailActivity.class)
                    .putExtra(Intent.EXTRA_TEXT, forecast);
            startActivity(intent);
        }
    });

    return rootView;
}

From source file:com.example.android.newsapp.NewsActivity.java

public void updateUi() {
    ListView newsListView = (ListView) findViewById(R.id.list);

    mEmptyStateTextView = (TextView) findViewById(R.id.empty_view);
    newsListView.setEmptyView(mEmptyStateTextView);

    mAdapter = new NewsAdapter(this, new ArrayList<NewsEvent>());

    newsListView.setAdapter(mAdapter);

    newsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override/*from   www. ja  v  a  2  s  . c  o  m*/
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            NewsEvent currentNewsEvent = mAdapter.getItem(position);

            Uri currentNewsEventUri = Uri.parse(currentNewsEvent.getUrl());

            Intent websiteIntent = new Intent(Intent.ACTION_VIEW, currentNewsEventUri);

            startActivity(websiteIntent);
        }
    });

    ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();

    if (networkInfo != null && networkInfo.isConnected()) {
        LoaderManager loaderManager = getLoaderManager();
        loaderManager.initLoader(NEWS_LOADER_ID, null, this);
    } else {
        View loadingIndicator = findViewById(R.id.loading_indicator);
        loadingIndicator.setVisibility(View.GONE);

        mEmptyStateTextView.setText(R.string.no_internet_connection);
    }
}

From source file:com.example.android.test.app.DemoActivity.java

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

    setContentView(R.layout.main);// ww  w . j  a  v  a2s.  com
    mDisplay = (TextView) findViewById(R.id.display);

    context = getApplicationContext();

    // Check device for Play Services APK. If check succeeds, proceed with GCM registration.
    if (checkPlayServices()) {
        gcm = GoogleCloudMessaging.getInstance(this);
        regid = getRegistrationId(context);

        if (regid.isEmpty()) {
            registerInBackground();
        }
    } else {
        Log.i(TAG, "No valid Google Play Services APK found.");
    }

    List<Person> persons = Person.listAll(Person.class);
    ArrayList<String> notifications = new ArrayList<>();
    // Copied
    for (int i = persons.size() - 1; i >= 0; --i) {
        notifications.add(persons.get(i).name + " detected on " + persons.get(i).time);
    }
    /*String [] notificationArray = {
        "This ", "is", "a", "Dynamic", "List", "to", "display", "notifications",
        "for", "the", " Application"
     };
     List<String> notifications= new ArrayList<String>(
        Arrays.asList(notificationArray));*/

    mNotificationAdapter = new ArrayAdapter<String>(this, R.layout.list_item_notifications,
            R.id.list_item_notifications_textview, notifications);

    ListView listView = (ListView) findViewById(R.id.listview_notifications);
    listView.setAdapter(mNotificationAdapter);

}