简体   繁体   中英

Android open spinner by listview OnItemLongClickListener

I am developing my first android application, but face a difficult problem. In my application, there is a page that show all sqlite database records by using listview. I want the spinner can pop up by user long click the listview, but it force close.

    private ListView.OnItemLongClickListener modItem = new ListView.OnItemLongClickListener()
    public boolean onItemLongClick(AdapterView<?> arg0, View v,int index, long arg3)
    {
        //I don't know what should I put in the parameter of the spinner constructor 
        sp_choice = new Spinner(<???>);
        //Same problem that I don't know what should I put in <???>
        ArrayAdapter<String> adt = new ArrayAdapter<String>(<???>,android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.choice_array));
        adt.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        sp_choice.setAdapter(adt);
        return true;
    } 
    };

Sorry, I can't understand your question...

Let me see if i got this right... You want a dialog with a list of options or registers when long-clicking an item of your listview or are you trying to instantiate a spinner from scratch?

If you want to open a list of options, I would recommend some kind of ListDialog. You can instantiate it adapting the code below:

Builder dialog = new AlertDialog.Builder(getSherlockActivity());
dialog.setTitle(<TITLE>)
            .setItems(<YOUR_LIST>, <Click Listener>)
            .create().show();

If not,

You want to create a Spinner from code? Aren't you using a xml resource? To instantiate a Spinner from code, you must pass the Context on the , as well as the layout params and the mode of the spinner (Dropdown or Dialog), and the string List with your elements on the of the adapter... It seems a lot of hard work for a beginner, though... If not necessary, I would recommend you to use a xml resource...

If I didn't get it, please, clarify your question...

Greetings!

I think will better to use popup menu or dialog for popup choice instead of making spinner in runtime.

For dialog it looks like this:

listView.setOnItemLongClickListener(new OnItemLongClickListener() {
    @Override
    public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
        AlertDialog.Builder choiceDialogBuilder = new Builder(YourActivity.this);
        choiceDialogBuilder.setSingleChoiceItems(R.array.app_array, 0, new OnClickListener() {

         @Override
         public void onClick(DialogInterface dialog, int which) {
        Toast.makeText(YourActivity.this, 
            getResources().getStringArray(R.array.app_array)[which], 
            Toast.LENGTH_SHORT).show();
        dialog.dismiss();
         }
    });
    choiceDialogBuilder.create().show();
    return true;
    } 
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM