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

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    // 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.
    // The current context (this activity)
    // The name of the layout ID.
    // The ID of the textview to populate.
    mForecastAdapter = new ArrayAdapter<>(getActivity(), // The current context (this activity)
            R.layout.list_item_forecast, // The name of the layout ID.
            R.id.tvListItemForecast, // The ID of the textview to populate.
            new ArrayList<String>());

    // Get a referece to the ListView and attach the adapter to it.
    ListView listView = (ListView) view.findViewById(R.id.listView_forecast);
    listView.setAdapter(mForecastAdapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override/* w  w w . ja  va  2  s.c om*/
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String forecast = mForecastAdapter.getItem(position);
            Intent intent = new Intent(getActivity(), DetailActivity.class);
            intent.putExtra(Intent.EXTRA_TEXT, forecast);
            startActivity(intent);
        }
    });
}

From source file:it_minds.dk.eindberetningmobil_android.views.ChooseProvider.java

private void refreshProviderList() {
    final ListView lw = getViewById(R.id.choose_provider_view_list);
    ServerFactory.getInstance(this).getProviders(new ResultCallback<List<Provider>>() {
        @Override//from   www. j a  v a  2 s  .co m
        public void onSuccess(final List<Provider> result) {
            lw.setAdapter(new ProviderAdapter(ChooseProvider.this, result));
            lw.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    //select provider and continue.
                    lw.setOnItemClickListener(null);
                    useProvider(result.get(position));
                }
            });
        }

        @Override
        public void onError(Exception error) {
            if (error instanceof VolleyError) {
                VolleyError vError = (VolleyError) error;
                if (vError.networkResponse != null) {
                    try {
                        JSONObject responseData = new JSONObject(
                                new String(vError.networkResponse.data, "UTF-8"));
                        Log.d("DATA:", responseData.toString());
                    } catch (JSONException e) {
                        Toast.makeText(ChooseProvider.this, "Ukendt fejl ved download af udbydere\n("
                                + vError.networkResponse.statusCode + ") (1)", Toast.LENGTH_SHORT).show();
                    } catch (UnsupportedEncodingException e) {
                        Toast.makeText(ChooseProvider.this, "Ukendt fejl ved download af udbydere\n("
                                + vError.networkResponse.statusCode + ") (2)", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    //We have not internet
                }
            } else {
                Toast.makeText(ChooseProvider.this, "Ukendt fejl ved download af udbydere.\n(3)",
                        Toast.LENGTH_SHORT).show();
                Log.d("ERROR", error.getMessage());
            }
            showProviderFetchError();
        }
    });
}

From source file:com.example.abrahamrequena.sunshine.ForecastFragment.java

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

    // 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>(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 w ww.j  a va 2s  .co m
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            CharSequence forecast = mForecastAdapter.getItem(position);
            Intent intent = new Intent(getActivity(), DetailActivity.class).putExtra(Intent.EXTRA_TEXT,
                    forecast);
            startActivity(intent);
        }
    });

    return rootView;
}

From source file:com.facebook.samples.sessionlogin.LoginUsingActivityActivity.java

public void showDialog() {
    final Dialog dialog = new Dialog(LoginUsingActivityActivity.this);
    dialog.setContentView(R.layout.list_view_contact);
    dialog.setTitle("Pic Number");
    ListView listView = (ListView) dialog.findViewById(R.id.lc_contacts);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.spinner_row_view,
            list);// w  ww  .  ja  v  a  2s  .com
    listView.setAdapter(adapter);

    dialog.show();
    /*DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
            
    dialog.getWindow().setLayout(metrics.heightPixels, metrics.widthPixels);*/
}

From source file:com.achep.header2actionbar.HeaderFragment.java

public void setListViewAdapter(ListView listView, ListAdapter adapter) {
    isListViewEmpty = adapter == null;/*w ww.j  av  a  2s. co m*/
    listView.setAdapter(null);
    listView.removeHeaderView(mFakeHeader);
    listView.addHeaderView(mFakeHeader);
    listView.setAdapter(adapter);
}

From source file:com.orca19.android.weather.ForecastFragment.java

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

    // 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>(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/*ww  w  .j a v  a2s .c o  m*/
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            String forecast = mForecastAdapter.getItem(position);

            Intent detailIntent = new Intent(getActivity(), DetailActivity.class).putExtra(Intent.EXTRA_TEXT,
                    forecast);
            startActivity(detailIntent);

            // Toast.makeText(getActivity(), forecast, Toast.LENGTH_SHORT).show();
        }
    });

    return rootView;
}

From source file:com.digium.respoke.GroupActivity.java

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

    String groupID = null;/*from   ww  w.j av  a  2 s  .  c  o m*/

    // Check whether we're recreating a previously destroyed instance
    if (savedInstanceState != null) {
        groupID = savedInstanceState.getString(GROUP_ID_KEY);
    } else {
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            groupID = extras.getString(GROUP_ID_KEY);
        } else {
            // The activity must have been destroyed while it was hidden to save memory. Use the most recent persistent data.
            SharedPreferences prefs = getSharedPreferences(ConnectActivity.RESPOKE_SETTINGS, 0);
            groupID = prefs.getString(GROUP_ID_KEY, "");
        }
    }

    this.setTitle(groupID);

    for (RespokeGroup eachGroup : ContactManager.sharedInstance().groups) {
        if (eachGroup.getGroupID().equals(groupID)) {
            group = eachGroup;
            break;
        }
    }

    listAdapter = new ListDataAdapter();

    ListView lv = (ListView) findViewById(R.id.list); //retrieve the instance of the ListView from your main layout
    lv.setAdapter(listAdapter); //assign the Adapter to be used by the ListView

    lv.setOnItemClickListener(this);
}

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

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

    // 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>(getActivity(), // The current context (this activity)
            R.layout.list_item_forcast, // 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 w  ww . j a  v  a2s.  c om
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String forecast = (String) parent.getItemAtPosition(position);
            Intent otherIntent = new Intent(getActivity(), DetailActivity.class).putExtra(Intent.EXTRA_TEXT,
                    forecast);

            startActivity(otherIntent);
        }
    });
    return rootView;
}

From source file:com.touchtechpayments.developer.sunshine.ForecastFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    String[] data = { "Loading weather data for:", "Dublin, Ireland" };
    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>(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.
            weekForecast);//from  w  ww. j a va  2  s  .c  om

    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() {
        // The parameter "position" refers to the position of the list item in mForecastAdapter
        // that was clicked
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {

            //TODO: Replace the Toast with an explicit intent to launch DetailActivity
            //getItem() returns the object @ position - in our case it's a string
            String forecast = (String) mForecastAdapter.getItem(position);
            //Toast.makeText(getActivity(), forecast, Toast.LENGTH_LONG).show();
            Log.v("Toast string = ", forecast);
            Intent detailIntent = new Intent(getActivity(), DetailActivity.class).putExtra("test", forecast);
            startActivity(detailIntent);
        }
    });
    return rootView;
}

From source file:com.example.administrator.basdf.FileBrowser.java

License:asdf

private void showDir(String targetDirectory) {

    setCurrentPathText("Current Directory: " + currentPath);

    targets = new ArrayList<String>();
    paths = new ArrayList<String>();

    File f = new File(targetDirectory);
    File[] directoryContents = f.listFiles();

    if (!targetDirectory.equals(root))

    {//from   ww w  .  j a  v a2  s  . com
        targets.add(root);
        paths.add(root);
        targets.add("../");
        paths.add(f.getParent());
    }

    for (File target : directoryContents) {
        paths.add(target.getPath());

        if (target.isDirectory()) {
            targets.add(target.getName() + "/");
        } else {
            targets.add(target.getName());

        }

    }

    ListView fileBrowserListView = (ListView) findViewById(R.id.file_browser_listview);

    ArrayAdapter<String> directoryData = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
            targets);
    fileBrowserListView.setAdapter(directoryData);

    fileBrowserListView.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> arg0, View view, int pos, long id) {

            File f = new File(paths.get(pos));

            if (f.isFile()) {
                targetFile = f;
                returnTarget();
                //Return target File to activity
            } else {
                //f must be a dir
                if (f.canRead()) {
                    currentPath = paths.get(pos);
                    showDir(paths.get(pos));
                }

            }

        }
        // TODO Auto-generated method stub            
    });

    /*
    final AlertDialog.Builder dialog = new AlertDialog.Builder(this);
    dialog.setTitle("WiFi Direct File Transfer");
    */

}