Example usage for android.widget ListView ListView

List of usage examples for android.widget ListView ListView

Introduction

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

Prototype

public ListView(Context context) 

Source Link

Usage

From source file:org.totschnig.myexpenses.dialog.TransactionListDialogFragment.java

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    //Context wrappedCtx = DialogUtils.wrapContext2(getActivity());

    mListView = new ListView(getActivity());
    mListView.setScrollBarStyle(ListView.SCROLLBARS_OUTSIDE_INSET);
    // Create an array to specify the fields we want to display in the list
    String[] from = new String[] { KEY_LABEL_MAIN, KEY_DATE, KEY_AMOUNT };

    // and an array of the fields we want to bind those fields to 
    int[] to = new int[] { R.id.category, R.id.date, R.id.amount };
    mAdapter = new TransactionAdapter(mAccount, (Grouping) getArguments().getSerializable(KEY_GROUPING),
            getActivity(), R.layout.expense_row, null, from, to, 0) {
        @Override/*from  w  ww.j  a va  2s. co m*/
        protected CharSequence getCatText(CharSequence catText, String label_sub) {
            return (isMain && label_sub != null) ? label_sub : "";
        }
    };
    mListView.setAdapter(mAdapter);
    getLoaderManager().initLoader(TRANSACTION_CURSOR, null, this);
    getLoaderManager().initLoader(SUM_CURSOR, null, this);
    mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> a, View v, int position, long id) {
            FragmentManager fm = getFragmentManager();
            DialogFragment f = (DialogFragment) fm.findFragmentByTag(TransactionDetailFragment.class.getName());
            if (f == null) {
                FragmentTransaction ft = fm.beginTransaction();
                TransactionDetailFragment.newInstance(id).show(ft, TransactionDetailFragment.class.getName());
            }
        }
    });
    //TODO pretify layout
    //    View titleView = LayoutInflater.from(getActivity()).inflate(R.layout.transaction_list_dialog_title, null);
    //    ((TextView) titleView.findViewById(R.id.label)).setText(getArguments().getString(KEY_LABEL));
    //    ((TextView) titleView.findViewById(R.id.amount)).setText("TBF");

    return new AlertDialog.Builder(getActivity()).setTitle(getArguments().getString(KEY_LABEL))
            .setView(mListView).setPositiveButton(android.R.string.ok, null).create();
}

From source file:com.fibrobook.viewpager.custom.CardFragment.java

public void symphtomsView(LinearLayout l) {

    LayoutParams params = new LayoutParams(android.view.ViewGroup.LayoutParams.MATCH_PARENT,
            android.view.ViewGroup.LayoutParams.MATCH_PARENT, Gravity.TOP);

    ListView symphtomList = new ListView(getActivity());
    symphtomList.setLayoutParams(params);

    ArrayAdapter<Disease> adapter = new ArrayAdapter<Disease>(getActivity(),
            android.R.layout.simple_list_item_1, symphtoms);
    symphtomList.setAdapter(adapter);/*w ww. j av a2  s.  c om*/
    symphtomList.setClickable(true);

    symphtomList.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
            ratingDialog = new Dialog(getActivity(), com.fibrobook.R.style.FullHeightDialog);
            ratingDialog.setContentView(com.fibrobook.R.layout.rating_dialog);
            ratingDialog.setCancelable(true);
            RatingBar ratingBar = (RatingBar) ratingDialog.findViewById(com.fibrobook.R.id.dialog_ratingbar);

            int i = 0;
            boolean exists = false;
            while (i < ds.size()) {
                if (symphtoms.get(position).getId() == ds.get(i).getDisease().getId()) {
                    ads = ds.get(i);
                    ratingBar.setRating(ads.getIntensity());
                    exists = true;
                    break;
                }
                i++;
            }
            if (!exists)
                ads = new SymphtomSummary(MainActivity.user, symphtoms.get(position), MainActivity.date);

            Button updateButton = (Button) ratingDialog.findViewById(com.fibrobook.R.id.rank_dialog_button);
            updateButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    RatingBar ratingBar = (RatingBar) ratingDialog
                            .findViewById(com.fibrobook.R.id.dialog_ratingbar);
                    ads.setIntensity(ratingBar.getRating());
                    SymphtomSummaryDAO dao = new SymphtomSummaryDAO(getActivity());
                    dao.save(ads);
                    ds = dao.getSymphtomSummary(MainActivity.date);
                    dao.close();
                    ratingDialog.dismiss();
                }
            });
            ratingDialog.show();
        }

    });

    l.addView(symphtomList);
}

From source file:idv.funnybrain.bike.FunnyActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_v2);/*from   w  w w.j a  v  a 2 s . c om*/
    self = this;

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

    String[] list_content = { getResources().getString(R.string.tai_or_kao),
            getResources().getString(R.string.back_to_map), getResources().getString(R.string.cc),
            getResources().getString(R.string.map_license), getResources().getString(R.string.close_app) };
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
            list_content);

    listView.setAdapter(adapter);

    final Intent intent = new Intent(FunnyActivity.this, BikeStationMapActivity.class);
    //startActivity(intent);

    int mode = (new Utils()).getMapSourceMode(this);
    if (mode == 9) {
        //Toast.makeText(this, "HIHI", Toast.LENGTH_SHORT).show();
        new AlertDialog.Builder(this).setTitle(R.string.tai_or_kao).setMessage(R.string.tai_or_kao_msg)
                .setPositiveButton(R.string.kaohsiung, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        (new Utils()).setMapSourceMode(self, 1);
                        setUpKaohsiung();
                        intent.putExtra("MODE", 1);
                        startActivity(intent);
                    }
                }).setNegativeButton(R.string.taipei, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        (new Utils()).setMapSourceMode(self, 2);
                        setUpTaipei();
                        intent.putExtra("MODE", 2);
                        startActivity(intent);
                    }
                }).show();
    } else {
        startActivity(intent);
    }

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            switch (position) {
            case 0:
                new AlertDialog.Builder(self).setTitle(R.string.tai_or_kao).setMessage(R.string.tai_or_kao_msg)
                        .setPositiveButton(R.string.kaohsiung, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                (new Utils()).setMapSourceMode(self, 1);
                                setUpKaohsiung();
                                intent.putExtra("MODE", 1);
                                startActivity(intent);
                            }
                        }).setNegativeButton(R.string.taipei, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                (new Utils()).setMapSourceMode(self, 2);
                                setUpTaipei();
                                intent.putExtra("MODE", 2);
                                startActivity(intent);
                            }
                        }).show();
                break;
            case 1:
                Intent intent = new Intent(FunnyActivity.this, BikeStationMapActivity.class);
                startActivity(intent);
                break;
            case 2:
                ListView list = new ListView(self);
                list.setAdapter(new CreativeCommonsAdapter(getLayoutInflater()));
                new AlertDialog.Builder(self).setTitle(R.string.cc).setView(list)
                        .setPositiveButton(android.R.string.ok, null).show();
                break;
            case 3:
                new AlertDialog.Builder(self).setTitle(R.string.map_license)
                        .setMessage(GooglePlayServicesUtil.getOpenSourceSoftwareLicenseInfo(self))
                        .setPositiveButton(android.R.string.ok, null).show();
                break;
            case 4:
                finish();
                break;
            }
        }
    });
}

From source file:com.wms.opensource.shopfast.fragment.ProductsInCollectionFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    if (layout != null) {
        ViewGroup parent = (ViewGroup) layout.getParent();
        parent.removeView(layout);//www  .  j ava 2s  .  c o m
        return layout;
    }

    if (listView == null) {
        layout = new RelativeLayout(getActivity());

        listView = new ListView(getActivity());

        layout.addView(listView);

        imageViewPreview = new ImageView(getActivity());
        RelativeLayout.LayoutParams imageViewParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
        imageViewParams.addRule(RelativeLayout.CENTER_IN_PARENT);
        imageViewPreview.setLayoutParams(imageViewParams);
        imageViewPreview.setVisibility(View.INVISIBLE);
        imageViewPreview.setOnClickListener(new ImageView.OnClickListener() {

            @Override
            public void onClick(View v) {
                // Dismiss the image view
                v.setVisibility(View.INVISIBLE);
                Animation myAnim = AnimationUtils.loadAnimation(ProductsInCollectionFragment.this.getActivity(),
                        R.anim.fadeout);
                v.startAnimation(myAnim);
            }

        });
        layout.addView(imageViewPreview);

        progressBar = new ProgressBar(getActivity(), null, android.R.attr.progressBarStyleLarge);

        RelativeLayout.LayoutParams progressBarBarams = new RelativeLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        progressBarBarams.addRule(RelativeLayout.CENTER_IN_PARENT);
        progressBar.setLayoutParams(progressBarBarams);
        progressBar.setVisibility(View.INVISIBLE);
        layout.addView(progressBar);

        // Before reloading products, display products if they have been saved before.
        String productsFilePath = StorageUtil.getTempDirectoryPath(getActivity()) + "/"
                + PersistFileNameProvider.getProductsFileName(collectionID, page);
        boolean productsFileExists = FileUtil.fileExist(productsFilePath);
        if (productsFileExists) {
            loadProductsFromLocalTask = new LoadCachedProductsInCollectionTask(getActivity(),
                    loadProductsHandler, progressBar, page);
            loadProductsFromLocalTask.execute(collectionID);
        } else {
            NetworkStatus networkStatus = NetworkUtil.getNetworkConnection(getActivity());
            if (networkStatus.equals(NetworkStatus.WIFI_CONNECTED)
                    || networkStatus.equals(NetworkStatus.MOBILE_CONNECTED)) {
                loadShopifyProductsTask = new LoadProductsInCollectionTask(getActivity(), loadProductsHandler,
                        progressBar, page);
                loadShopifyProductsTask.execute(collectionID);
            } else {
                Toast.makeText(getActivity(), getString(R.string.noNetworkAvailable), Toast.LENGTH_LONG).show();
            }
        }
    }
    return layout;
}

From source file:com.agateau.equiv.ui.MealItemDetailActivity.java

private void setupTabs() {
    ListView fullListView = new ListView(this);
    ListView favoriteListView = new ListView(this);

    mFullListAdapter = new ProductListAdapter(this, mKernel, mProductStore.getItems());
    mFavoritesListAdapter = new ProductListAdapter(this, mKernel, mProductStore.getFavoriteItems());
    mProductStore.setOnProductStoreChangedListener(new ProductStore.OnProductStoreChangedListener() {
        @Override/*from   w w w.jav  a2  s .  c o  m*/
        public void onFavoriteChanged() {
            mFavoritesListAdapter.notifyDataSetChanged();

            // Notify mFullListAdapter as well because it must update the state of its checkboxes
            mFullListAdapter.notifyDataSetChanged();
        }

        @Override
        public void onItemListChanged() {
            mFullListAdapter.notifyDataSetChanged();
        }
    });

    fullListView.setAdapter(mFullListAdapter);
    favoriteListView.setAdapter(mFavoritesListAdapter);

    AdapterView.OnItemClickListener listener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Product product = (Product) parent.getAdapter().getItem(position);
            onSelectProduct(product);
        }
    };
    fullListView.setOnItemClickListener(listener);
    favoriteListView.setOnItemClickListener(listener);

    AdapterView.OnItemLongClickListener longClickListener = new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            Product product = (Product) parent.getItemAtPosition(position);
            editCustomProduct(product);
            return true;
        }
    };

    fullListView.setLongClickable(true);
    favoriteListView.setLongClickable(true);

    fullListView.setOnItemLongClickListener(longClickListener);
    favoriteListView.setOnItemLongClickListener(longClickListener);

    final ActionBar actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
    ActionBarViewTabBuilder builder = new ActionBarViewTabBuilder(actionBar, viewPager);
    builder.addTab(fullListView).setText(R.string.add_meal_item_tab_all);
    builder.addTab(favoriteListView).setText(R.string.add_meal_item_tab_favorites);
}

From source file:net.pocketmagic.android.eventinjector.MainActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d(LT, "App created.");
    Events.intEnableDebug(1);//  ww w.  j a v a 2 s.c  o  m
    // disable the titlebar
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    // create a basic user interface
    LinearLayout panel = new LinearLayout(this);
    panel.setOrientation(LinearLayout.VERTICAL);
    setContentView(panel);

    EditText v = new EditText(this);
    v.setId(idTextView);
    v.setOnClickListener(this);
    panel.addView(v);

    // --
    Button b = new Button(this);
    b.setText("Scan Input Devs");
    b.setId(idButScan);
    b.setOnClickListener(this);
    panel.addView(b);

    // put list in a scroll view
    LinearLayout listLayout = new LinearLayout(this);
    listLayout.setLayoutParams(
            new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1.0f));
    m_lvDevices = new ListView(this);
    LayoutParams lvLayoutParam = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
    m_lvDevices.setLayoutParams(lvLayoutParam);
    m_lvDevices.setId(idLVDevices);
    m_lvDevices.setDividerHeight(0);
    m_lvDevices.setFadingEdgeLength(0);
    m_lvDevices.setCacheColorHint(0);
    m_lvDevices.setAdapter(null);

    listLayout.addView(m_lvDevices);
    panel.addView(listLayout);
    // --
    LinearLayout panelH = new LinearLayout(this);
    panelH.setOrientation(LinearLayout.HORIZONTAL);
    panel.addView(panelH);
    // --
    m_selDevSpinner = new Spinner(this);
    m_selDevSpinner.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    m_selDevSpinner.setId(idSelSpin);
    m_selDevSpinner.setOnItemSelectedListener((OnItemSelectedListener) this);
    panelH.addView(m_selDevSpinner);
    // -- simulate key event
    b = new Button(this);
    b.setText(">Key");
    b.setId(idButInjectKey);
    b.setOnClickListener(this);
    panelH.addView(b);
    // -- simulate touch event
    b = new Button(this);
    b.setText(">Tch");
    b.setId(idButInjectTouch);
    b.setOnClickListener(this);
    panelH.addView(b);
    // --
    m_tvMonitor = new TextView(this);
    m_tvMonitor.setText("Event Monitor stopped.");
    panel.addView(m_tvMonitor);
    // --
    panelH = new LinearLayout(this);
    panelH.setOrientation(LinearLayout.HORIZONTAL);
    panel.addView(panelH);
    // --
    b = new Button(this);
    b.setText("Monitor Start");
    b.setId(idButMonitorStart);
    b.setOnClickListener(this);
    panelH.addView(b);
    // --
    b = new Button(this);
    b.setText("Monitor Stop");
    b.setId(idButMonitorStop);
    b.setOnClickListener(this);
    panelH.addView(b);
    // -- simulate test event
    b = new Button(this);
    b.setText(">Test");
    b.setId(idButTest);
    b.setOnClickListener(this);
    panelH.addView(b);
}

From source file:com.theelix.libreexplorer.FileManagerActivity.java

/** This function reads the current Folder, and populates the ListView with the content of that folder */
public void refresh() {
    ((ViewGroup) findViewById(R.id.list_layout)).removeView(itemView);
    if (FileManager.getCurrentDirectory().getName().equals("")) {
        mActionBar.setTitle(getString(R.string.app_name));
    } else {//from w  w w .  ja v  a  2 s. co m
        mActionBar.setTitle(getString(R.string.app_name) + ": " + FileManager.getCurrentDirectory().getName());

    }
    boolean useGrid = PreferenceManager.getDefaultSharedPreferences(this).getBoolean("pref_layout_grid", false);
    int itemLayout;
    if (useGrid) {
        itemView = new GridView(this);
        itemLayout = R.layout.file_grid_element;
        ((GridView) itemView).setNumColumns(4);
        ((GridView) itemView).setHorizontalSpacing(0);

    } else {
        itemView = new ListView(this);
        itemLayout = R.layout.file_list_element;
    }
    boolean hideHiddenFiles = !PreferenceManager.getDefaultSharedPreferences(this)
            .getBoolean("pref_show_hidden_files", false);
    try {
        ArrayList<File> fileList = new ArrayList<>(
                Arrays.asList(FileManager.getCurrentDirectory().listFiles()));
        for (int i = 0; i < fileList.size(); i++) {
            if (fileList.get(i).isHidden() && hideHiddenFiles) {
                fileList.remove(fileList.get(i));
            }
        }
        ArrayAdapter<File> filelistAdapter = new FileListArrayAdapter(this, itemLayout, fileList);
        filelistAdapter.sort(new FileComparator());
        itemView.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE_MODAL);
        ActionModeCallback callback = new ActionModeCallback();
        callback.setListView(itemView);
        callback.setContext(this);
        itemView.setMultiChoiceModeListener(callback);
        mDrawerLayout.closeDrawers();
        itemView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
                File file = (File) adapter.getItemAtPosition(position);
                if (file.isDirectory()) {
                    FileManager.setCurrentDirectory(file);
                } else {
                    FileManager.openFile(file);

                }
            }
        });

        itemView.setAdapter(filelistAdapter);
        itemView.setTextFilterEnabled(true);
        ((ViewGroup) findViewById(R.id.list_layout)).addView(itemView, new AbsListView.LayoutParams(
                AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.MATCH_PARENT));
    } catch (NullPointerException e) {
        Toast.makeText(this, "Unable to Access File", Toast.LENGTH_LONG).show();
    }
    //Check if SDCard is mounted, if so shows External Storage on the Sidebar
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        if (Environment.getExternalStorageState(new File(System.getenv("SECONDARY_STORAGE")))
                .equals(Environment.MEDIA_MOUNTED)) {
            findViewById(R.id.menu_external_storage).setVisibility(View.VISIBLE);
        } else {
            findViewById(R.id.menu_external_storage).setVisibility(View.GONE);
        }
    } else {
        //Dirty method, but I think is the only option for devices before L
        if (new File(System.getenv("SECONDARY_STORAGE")).canRead()) {
            findViewById(R.id.menu_external_storage).setVisibility(View.VISIBLE);
        } else {
            findViewById(R.id.menu_external_storage).setVisibility(View.GONE);
        }
    }
}

From source file:org.onebusaway.android.ui.ListFragment.java

/**
 * Provide default implementation to return a simple list view.  Subclasses
 * can override to replace with their own layout.  If doing so, the
 * returned view hierarchy <em>must</em> have a ListView whose id
 * is {@link android.R.id#list android.R.id.list} and can optionally
 * have a sibling view id {@link android.R.id#empty android.R.id.empty}
 * that is to be shown when the list is empty.
 *
 * <p>If you are overriding this method with your own custom content,
 * consider including the standard layout {@link android.R.layout#list_content}
 * in your layout file, so that you continue to retain all of the standard
 * behavior of ListFragment.  In particular, this is currently the only
 * way to have the built-in indeterminant progress state be shown.
 *///from   w ww  . java2s . c o  m
@Override
@SuppressWarnings("deprecation")
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final Context context = getActivity();

    FrameLayout root = new FrameLayout(context);

    // ------------------------------------------------------------------

    LinearLayout pframe = new LinearLayout(context);
    pframe.setId(R.id.loading);
    pframe.setOrientation(LinearLayout.VERTICAL);
    pframe.setVisibility(View.GONE);
    pframe.setGravity(Gravity.CENTER);

    ProgressBar progress = new ProgressBar(context, null, android.R.attr.progressBarStyleLarge);
    pframe.addView(progress, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT));

    root.addView(pframe, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
            ViewGroup.LayoutParams.FILL_PARENT));

    // ------------------------------------------------------------------

    FrameLayout lframe = new FrameLayout(context);
    lframe.setId(R.id.listContainer);

    TextView tv = new TextView(getActivity());
    tv.setId(R.id.internalEmpty);
    tv.setGravity(Gravity.CENTER);
    lframe.addView(tv, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
            ViewGroup.LayoutParams.FILL_PARENT));

    ListView lv = new ListView(getActivity());
    lv.setId(android.R.id.list);
    lv.setDrawSelectorOnTop(false);
    lframe.addView(lv, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
            ViewGroup.LayoutParams.FILL_PARENT));

    root.addView(lframe, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
            ViewGroup.LayoutParams.FILL_PARENT));

    // ------------------------------------------------------------------

    root.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
            ViewGroup.LayoutParams.FILL_PARENT));

    return root;
}

From source file:org.telegram.ui.myLocationSettingsActivity.java

@Override
public View createView(LayoutInflater inflater) {
    if (fragmentView == null) {
        actionBar.setBackButtonImage(R.drawable.ic_ab_back);
        actionBar.setAllowOverlayTitle(true);
        actionBar.setTitle("My Places");
        actionBar.setActionBarMenuOnItemClick(new ActionBar.ActionBarMenuOnItemClick() {
            @Override//from  w w w  .j  a v  a2  s  .  c  o  m
            public void onItemClick(int id) {
                if (id == -1) {
                    finishFragment();
                }
            }
        });

        listAdapter = new ListAdapter(getParentActivity());

        fragmentView = new FrameLayout(getParentActivity());
        FrameLayout frameLayout = (FrameLayout) fragmentView;
        frameLayout.setBackgroundColor(0xfff0f0f0);

        ListView listView = new ListView(getParentActivity());
        listView.setDivider(null);
        listView.setDividerHeight(0);
        listView.setVerticalScrollBarEnabled(false);
        listView.setDrawSelectorOnTop(true);
        frameLayout.addView(listView);
        FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) listView.getLayoutParams();
        layoutParams.width = FrameLayout.LayoutParams.MATCH_PARENT;
        layoutParams.height = FrameLayout.LayoutParams.MATCH_PARENT;
        layoutParams.gravity = Gravity.TOP;
        listView.setLayoutParams(layoutParams);
        listView.setAdapter(listAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, final int i, long l) {
                if (i == homeRow) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(getParentActivity());
                    builder.setTitle("Home");
                    final ViewGroup.LayoutParams lparams = new ViewGroup.LayoutParams(50, 30);
                    final EditText input = new EditText(getParentActivity());
                    input.setHint("Insert city");
                    input.setLayoutParams(lparams);
                    builder.setView(input);
                    builder.setPositiveButton("confirm", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            if (input.getText().toString().trim().length() > 0) {
                                String city = input.getText().toString().trim();
                                try {
                                    Bundle locationHome = new getCoordinates().execute(city).get();
                                    String latHome = locationHome.getString("lat");
                                    String lonHome = locationHome.getString("lon");
                                    SharedPreferences sharedPreferences = ApplicationLoader.applicationContext
                                            .getSharedPreferences("mainconfig", Activity.MODE_PRIVATE);
                                    SharedPreferences.Editor editor = sharedPreferences.edit();
                                    editor.putString("latHome", latHome);
                                    editor.putString("longHome", lonHome);
                                    editor.commit();
                                    Log.i(TAG, "fine! " + lonHome);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                } catch (ExecutionException e) {
                                    e.printStackTrace();
                                }
                            } else {
                                Toast.makeText(getParentActivity(), "That is empty :(", Toast.LENGTH_SHORT)
                                        .show();
                            }

                        }
                    });
                    builder.setNegativeButton("Why?", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Toast.makeText(getParentActivity(), "To website", Toast.LENGTH_SHORT).show();
                        }
                    });
                    showAlertDialog(builder);
                } else if (i == workRow) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(getParentActivity());
                    builder.setTitle("Work");
                    final EditText input = new EditText(getParentActivity());
                    input.setHint("Insert city");
                    builder.setView(input);
                    builder.setPositiveButton("confirm", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            if (input.getText().toString().trim().length() > 0) {
                                String city = input.getText().toString().trim();
                                try {
                                    Bundle locationWork = new getCoordinates().execute(city).get();
                                    String latWork = locationWork.getString("lat");
                                    String lonWork = locationWork.getString("lon");
                                    SharedPreferences sharedPreferences = ApplicationLoader.applicationContext
                                            .getSharedPreferences("mainconfig", Activity.MODE_PRIVATE);
                                    SharedPreferences.Editor editor = sharedPreferences.edit();
                                    editor.putString("latWork", latWork);
                                    editor.putString("longWork", lonWork);
                                    editor.commit();
                                    Log.i(TAG, "fine! " + latWork);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                } catch (ExecutionException e) {
                                    e.printStackTrace();
                                }
                            } else {
                                Toast.makeText(getParentActivity(), "That is empty :(", Toast.LENGTH_SHORT)
                                        .show();
                            }

                        }
                    });
                    builder.setNegativeButton("Why?", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Toast.makeText(getParentActivity(), "To website", Toast.LENGTH_SHORT).show();
                        }
                    });
                    showAlertDialog(builder);
                } else if (i == entertainmentRow) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(getParentActivity());
                    builder.setTitle("Entertainment");
                    final EditText input = new EditText(getParentActivity());
                    input.setHint("Insert city");
                    builder.setView(input);
                    builder.setPositiveButton("confirm", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            if (input.getText().toString().trim().length() > 0) {
                                String city = input.getText().toString().trim();
                                try {
                                    Bundle locationEntertainment = new getCoordinates().execute(city).get();
                                    String latEntertainment = locationEntertainment.getString("lat");
                                    String lonEntertainment = locationEntertainment.getString("lon");
                                    SharedPreferences sharedPreferences = ApplicationLoader.applicationContext
                                            .getSharedPreferences("mainconfig", Activity.MODE_PRIVATE);
                                    SharedPreferences.Editor editor = sharedPreferences.edit();
                                    editor.putString("latEntertainment", latEntertainment);
                                    editor.putString("longEntertainment", lonEntertainment);
                                    editor.commit();
                                    Log.i(TAG, "fine! " + latEntertainment);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                } catch (ExecutionException e) {
                                    e.printStackTrace();
                                }
                            } else {
                                Toast.makeText(getParentActivity(), "That is empty :(", Toast.LENGTH_SHORT)
                                        .show();
                            }

                        }
                    });
                    builder.setNegativeButton("Why?", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Toast.makeText(getParentActivity(), "To website", Toast.LENGTH_SHORT).show();
                        }
                    });
                    showAlertDialog(builder);
                }
                //TODO find a solution for commuteRow as well!!
            }
        });
    } else {
        ViewGroup parent = (ViewGroup) fragmentView.getParent();
        if (parent != null) {
            parent.removeView(fragmentView);
        }
    }
    return fragmentView;
}

From source file:com.ultramegasoft.flavordex2.dialog.CatListDialog.java

/**
 * Get the layout for the Dialog.//from   ww  w  .  j ava2s . co  m
 *
 * @return The View to place inside the Dialog
 */
@NonNull
private ListView getLayout() {
    final ListView listView = new ListView(getContext());
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            onCatSelected(position, id);
        }
    });
    listView.addFooterView(
            LayoutInflater.from(getContext()).inflate(R.layout.cat_add_list_item, listView, false));

    return listView;
}