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.skubit.android.example.PurchasesFragment.java

public void displayPurchases(Context context, ListView view, ArrayList<String> purchases) throws JSONException {

    List<Map<String, String>> items = new ArrayList<Map<String, String>>();

    for (String detail : purchases) {
        Map<String, String> map = new HashMap<String, String>();
        JSONObject jo = new JSONObject(detail);
        map.put("title", jo.toString());
        items.add(map);/*from  w w w . j av a 2  s  .c  o  m*/
    }
    String[] from = new String[] { "title" };
    int[] to = new int[] { android.R.id.text1 };

    SimpleAdapter adapter = new SimpleAdapter(context, items, android.R.layout.simple_list_item_1, from, to);
    view.setAdapter(adapter);
}

From source file:com.example.polytech.orientatewatch.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>());
    mForecastAdapterDetails = 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 a  v  a2s .  com
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bmpIcon.compress(Bitmap.CompressFormat.PNG, 100, stream);
            final byte[] byteArray = stream.toByteArray();
            String forecast = mForecastAdapterDetails.getItem(position);
            Intent intent = new Intent(getActivity(), DetailActivity.class)
                    .putExtra(Intent.EXTRA_TEXT, forecast).putExtra("icon", byteArray);
            startActivity(intent);
        }
    });

    return rootView;
}

From source file:com.dosse.bwentrain.androidPlayer.MainActivity.java

private void populatePresetList() {
    final ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
    ArrayList<String> fileList = new ArrayList<String>();
    String[] downloads = fileList(); //get all files downloaded from the preset sharing platform
    for (String s : downloads) //add all downloaded presets
        if (s.toLowerCase().endsWith(".sin")) {
            fileList.add(s);//from w  w w.  j  a  v a 2 s .co m
        }

    //generate items for ListView
    //add link to preset sharing platform
    HashMap<String, String> siteLink = new HashMap<String, String>();
    siteLink.put("title", getString(R.string.download_presets));
    siteLink.put("author", getString(R.string.theyre_free));
    siteLink.put("description", "");
    siteLink.put("path", getString(R.string.presets_url));
    list.add(siteLink);

    for (String f : fileList) {
        Preset p = loadPreset(f);
        if (p == null)
            continue;
        HashMap<String, String> item = new HashMap<String, String>();
        item.put("title", p.getTitle());
        item.put("author", p.getAuthor());
        item.put("description", p.getDescription());
        item.put("length", "" + Utils.toHMS(p.getLength()));
        item.put("loop", "" + (p.loops() ? Utils.toHMS(p.getLoop()) : ""));
        item.put("path", f);
        list.add(item);
    }

    //insert all into ListView
    ListView files = (ListView) findViewById(R.id.listView1);
    files.setAdapter(new SimpleAdapter(this, list, android.R.layout.simple_list_item_2,
            new String[] { "title", "author" }, new int[] { android.R.id.text1, android.R.id.text2 }));

    //tap on item
    files.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            HashMap<String, String> item = list.get(position);
            if (item.get("path").startsWith("http://")) { //link
                Intent i = new Intent(MainActivity.this, BrowserActivity.class);
                i.putExtra("path", item.get("path"));
                startActivity(i);
            } else { //preset
                Intent i = new Intent(MainActivity.this, DetailsActivity.class);
                i.putExtra("title", item.get("title"));
                i.putExtra("author", item.get("author"));
                i.putExtra("description", item.get("description"));
                i.putExtra("length", item.get("length"));
                i.putExtra("loop", item.get("loop"));
                i.putExtra("path", item.get("path"));
                startActivity(i);
            }
        }
    });
    //long tap on item
    files.setOnItemLongClickListener(new OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            HashMap<String, String> item = list.get(position);
            final String path = item.get("path");
            if (path.startsWith("http://"))
                return true; //can't delete links
            //delete preset
            //ask for confirm
            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            DialogInterface.OnClickListener l = new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (which == DialogInterface.BUTTON_POSITIVE) { //delete confirmed
                        if (new File(path).exists()) { //delete file on sdcard
                            new File(path).delete();
                        } else {//delete from app data
                            deleteFile(path);
                        }
                        populatePresetList(); //repopulate preset list
                    }
                }
            };
            builder.setMessage(getString(R.string.delete_confirm) + " \"" + item.get("title") + "\"?")
                    .setPositiveButton(getString(R.string.yes), l).setNegativeButton(getString(R.string.no), l)
                    .show();
            return true;
        }
    });
}

From source file:com.tutor.fragment.ViewPageFragment.java

private void setFDListViewAdapter(TabHost tabHost) {
    View view = (View) tabHost.getCurrentView();
    ListView listView = (ListView) view.findViewById(R.id.lv_products);
    listView.setAdapter(new ProductListAdapter(getActivity(), listItems));
    listView.setOnItemClickListener(new OnItemClickListener() {

        @Override/*from  w ww  . j ava 2  s  .c o m*/
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent = new Intent(getActivity(), AppDetailActivity.class);
            setCurrentFDItemInf(position);
            intent.putExtra("position", position);
            startActivity(intent);
        }
    });
}

From source file:com.tutor.fragment.ViewPageFragment.java

private void setJFListViewAdapter(TabHost tabHost) {
    View view = (View) tabHost.getCurrentView();
    ListView listView = (ListView) view.findViewById(R.id.lv_products);
    listView.setAdapter(new JFListAdapter(getActivity(), jf_listItems));
    listView.setOnItemClickListener(new OnItemClickListener() {

        @Override/*  w w  w.j  av a  2s.  c  om*/
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent = new Intent(getActivity(), JfAppDetailActivity.class);
            setCurrentJFItemInf(position);
            intent.putExtra("position", position);
            startActivity(intent);
        }
    });
}

From source file:com.apotheosis.acceleration.monitor.MainMenuActivity.java

private void setUpListView() {
    ListView lv = (ListView) findViewById(R.id.fileList);

    List<String> fileNames = FileUtilities.getFileList();

    if (fileNames != null) {
        FileListAdapter listAdapter = new FileListAdapter(this);

        lv.setAdapter(listAdapter);
        lv.setTextFilterEnabled(true);//from   www  .j  ava 2  s .  c o  m

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.mainDrawerLayout);
                boolean isDrawerOpen = drawerLayout.isDrawerOpen(findViewById(R.id.side_drawer));

                if (isDrawerOpen)
                    drawerLayout.closeDrawers();

                new LoadData(TimeXYZDataPackage.DataType.ACCELERATION, MainMenuActivity.this,
                        parent.getAdapter().getItem(position).toString()).execute((Void[]) null);
            }
        });

        lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {
                final String fileName = parent.getAdapter().getItem(position).toString();
                final AlertDialog.Builder optionsMenu = new AlertDialog.Builder(MainMenuActivity.this);
                optionsMenu.setItems(new String[] { "Open Acceleration Graph", "Open Raw Data", "Share Data",
                        "Delete Data", "Cancel" }, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {

                                DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.mainDrawerLayout);
                                boolean isDrawerOpen = drawerLayout
                                        .isDrawerOpen(findViewById(R.id.side_drawer));

                                Intent i;
                                Tracker tracker;
                                switch (which) {
                                case 0:
                                    dialog.dismiss();

                                    if (isDrawerOpen)
                                        drawerLayout.closeDrawers();

                                    new LoadData(TimeXYZDataPackage.DataType.ACCELERATION,
                                            MainMenuActivity.this, fileName).execute((Void[]) null);
                                    break;

                                case 1:

                                    if (isDrawerOpen)
                                        drawerLayout.closeDrawers();

                                    new LoadData(TimeXYZDataPackage.DataType.RAW_DATA, MainMenuActivity.this,
                                            fileName).execute((Void[]) null);

                                    break;

                                case 2:

                                    if (isDrawerOpen)
                                        drawerLayout.closeDrawers();

                                    tracker = AnalyticsTrackers.getInstance().get(AnalyticsTrackers.Target.APP);
                                    tracker.send(new HitBuilders.EventBuilder().setCategory("Data Function")
                                            .setAction("Share Data").build());

                                    i = new Intent(Intent.ACTION_SEND);
                                    i.setType("text/xml");
                                    i.putExtra(Intent.EXTRA_SUBJECT, "Sending " + fileName + "as attachment");
                                    i.putExtra(Intent.EXTRA_TEXT, fileName + "is attached.");
                                    File f = new File(FileUtilities.path + fileName + ".csv");
                                    i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f));
                                    startActivity(Intent.createChooser(i, "Choose an application..."));
                                    break;

                                case 3:
                                    final AlertDialog.Builder confirmDelete = new AlertDialog.Builder(
                                            MainMenuActivity.this);
                                    confirmDelete
                                            .setMessage("Are you sure you want to delete " + fileName + "?");
                                    confirmDelete.setPositiveButton("Yes",
                                            new DialogInterface.OnClickListener() {
                                                public void onClick(DialogInterface dialog, int which) {
                                                    File f = new File(FileUtilities.path + fileName + ".csv");
                                                    Log.d("DELETION_SUCESS", String.valueOf(f.delete()));
                                                    setUpListView();
                                                }
                                            });
                                    confirmDelete.setNegativeButton("No",
                                            new DialogInterface.OnClickListener() {
                                                public void onClick(DialogInterface dialog, int which) {
                                                    dialog.dismiss();
                                                }
                                            });
                                    confirmDelete.setOnCancelListener(new DialogInterface.OnCancelListener() {
                                        public void onCancel(DialogInterface dialog) {
                                            dialog.dismiss();
                                        }
                                    });
                                    confirmDelete.show();
                                    break;
                                }
                            }
                        });

                optionsMenu.show();
                return true;
            }
        });
    }
}

From source file:com.irccloud.android.activity.PastebinsActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (Build.VERSION.SDK_INT >= 21) {
        Bitmap cloud = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
        setTaskDescription(new ActivityManager.TaskDescription(getResources().getString(R.string.app_name),
                cloud, 0xFFF2F7FC));//  w ww .j av  a  2  s .  c  o  m
        cloud.recycle();
    }

    setContentView(R.layout.ignorelist);

    if (getSupportActionBar() != null) {
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeAsUpIndicator(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
        getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.actionbar));
        getSupportActionBar().setElevation(0);
    }

    if (savedInstanceState != null && savedInstanceState.containsKey("adapter")) {
        try {
            page = savedInstanceState.getInt("page");
            Pastebin[] pastebins = (Pastebin[]) savedInstanceState.getSerializable("adapter");
            for (Pastebin p : pastebins) {
                adapter.addPastebin(p);
            }
            adapter.notifyDataSetChanged();
        } catch (Exception e) {
            page = 0;
            adapter.clear();
        }
    }

    footer = getLayoutInflater().inflate(R.layout.messageview_header, null);
    ListView listView = (ListView) findViewById(android.R.id.list);
    listView.setAdapter(adapter);
    listView.addFooterView(footer);
    listView.setOnScrollListener(new AbsListView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(AbsListView absListView, int i) {

        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            if (canLoadMore && firstVisibleItem + visibleItemCount > totalItemCount - 4) {
                canLoadMore = false;
                new FetchPastebinsTask().execute((Void) null);
            }
        }
    });
    listView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    });
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            final Pastebin p = (Pastebin) adapter.getItem(i);

            Intent intent = new Intent(PastebinsActivity.this, PastebinViewerActivity.class);
            intent.setData(Uri.parse(p.url + "?id=" + p.id + "&own_paste=" + (p.own_paste ? "1" : "0")));
            startActivity(intent);
        }
    });

    Toast.makeText(this, "Tap a pastebin to view full text with syntax highlighting", Toast.LENGTH_LONG).show();
}

From source file:edgargtzg.popularmovies.MovieDetailsFragment.java

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

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

    if (mMovieItem != null) {
        String value;/* ww  w  .  jav  a 2 s. com*/
        // Populates movie original title text view value.
        value = mMovieItem.getOriginalTitle();
        if (!(value.isEmpty())) {
            TextView originalTitleTextView = (TextView) rootView.findViewById(R.id.movie_title_textView);
            originalTitleTextView.setText(value);
        }

        // Populates movie poster image.
        value = mMovieItem.getMoviePoster();
        if (!(value.isEmpty())) {
            ImageView moviePosterImageView = (ImageView) rootView.findViewById(R.id.movie_poster_imageView);
            Picasso.with(getActivity())
                    .load(rootView.getResources().getString(R.string.details_poster_api_call) + value)
                    .into(moviePosterImageView);
        }

        // Populates movie plot synopsis.
        value = mMovieItem.getPlotSynopsis();
        if (!(value.isEmpty())) {
            TextView moviePlotTextView = (TextView) rootView.findViewById(R.id.movie_plot_textView);
            moviePlotTextView.setText(value);
        }

        // Populates movie user rating.
        value = mMovieItem.getUserRating();
        if (!(value.isEmpty())) {
            RatingBar movieRateBar = (RatingBar) rootView.findViewById(R.id.movie_ratingBar);
            movieRateBar.setRating((Float.parseFloat(value) / 2));
        }

        // Populates movie release date value.
        value = mMovieItem.getReleaseDate();
        if (!(value.isEmpty())) {
            TextView movieReleaseTextView = (TextView) rootView.findViewById(R.id.movie_release_textView);
            movieReleaseTextView.setText(value);
        }

        ListView videoListView = (ListView) rootView.findViewById(R.id.movie_details_videos_listview);
        videoListView.setAdapter(mMovieVideoAdapter);
        // Adds play trailer using Youtube app or web browser.
        videoListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                MovieItemVideo videoItem = mMovieVideoAdapter.getItem(position);
                Intent intent = new Intent(Intent.ACTION_VIEW,
                        Uri.parse("http://www.youtube.com/watch?v=" + videoItem.getVideoKey()));
                startActivity(intent);
            }
        });

        ListView reviewListView = (ListView) rootView.findViewById(R.id.movie_details_reviews_listview);
        reviewListView.setAdapter(mMovieReviewAdapter);
        reviewListView.setEnabled(false);
    }
    return rootView;
}

From source file:com.laquysoft.bycon.GoogleCardsActivity.java

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

    ListView listView = (ListView) findViewById(R.id.activity_googlecards_listview);

    mGoogleCardsAdapter = new GoogleCardsAdapter(this);
    SwingBottomInAnimationAdapter swingBottomInAnimationAdapter = new SwingBottomInAnimationAdapter(
            new SwipeDismissAdapter(mGoogleCardsAdapter, this));
    swingBottomInAnimationAdapter.setInitialDelayMillis(300);
    swingBottomInAnimationAdapter.setAbsListView(listView);

    listView.setAdapter(swingBottomInAnimationAdapter);

    mGoogleCardsAdapter.addAll(getItems());
}