i want a popup menu when i click on my listview item
Asked Answered
E

2

0

my activity which contains listview i have five item in this listview. This is my items in Listview , and this is what i want when i click on my first item in listview , there are some radio button in the popup menu also

package com.example.shivnandan.fit;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity1 extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener

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

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("Home");

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
        registerForContextMenu(list);
        list = (ListView) findViewById(R.id.listView2);
        list.setAdapter(new shivvAdapter(this));
        list.setOnItemClickListener(new AdapterView.OnItemClickListener()

        {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)

            {

                if (position == 0)
                {

                    Toast.makeText(getApplicationContext(), "Gender Selected", Toast.LENGTH_SHORT).show();




                }
                if (position == 1)

                {

                    Toast.makeText(getApplicationContext(), "Age Selected ", Toast.LENGTH_SHORT).show();

                }

                if (position == 2)

                {

                    Toast.makeText(getApplicationContext(), "Height Selected", Toast.LENGTH_SHORT).show();

                }

                if (position == 3)

                {

                    Toast.makeText(getApplicationContext(), "Weight Selected", Toast.LENGTH_SHORT).show();

                }
                if (position == 4)

                {

                    Toast.makeText(getApplicationContext(), "Reset Selected", Toast.LENGTH_SHORT).show();

                }

            }
        });
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);

    }

    @Override
    public void onBackPressed()
    {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START))
        {

            drawer.closeDrawer(GravityCompat.START);
        }
        else
        {

            super.onBackPressed();
        }



    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings)
        {
            Toast.makeText(getApplicationContext(), " Setting ", Toast.LENGTH_SHORT).show();
            return true;
        }

        if(id==R.id.Change_photo)
        {
            Toast.makeText(getApplicationContext(),"Change Your Photo",Toast.LENGTH_SHORT).show();


        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item)
    {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_camera)
        {

        }
        else if (id == R.id.nav_gallery)
        {
            Intent i = new Intent(getApplicationContext(),MainActivity.class);

            startActivity(i);
        }
        else if (id == R.id.nav_slideshow)
        {


        }
    else if (id == R.id.nav_manage)
     {

       }


        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}
class SingleRow2
{
    String title;
    //String description;
    int  image;
    SingleRow2(String title )
    {

        this.title =title;
        // this.description = description;
        //this.image = image;
    }

}
class shivvAdapter extends BaseAdapter
{
    ArrayList<SingleRow> list;
    Context context;
    shivvAdapter(Context c)
    {
        context = c;
        list=new ArrayList<SingleRow>();
        Resources res = c.getResources();
        String[] titles=res.getStringArray(R.array.profiletitle);
        // String[] descriptions=res.getStringArray(R.array.description);
        int[] images = {R.drawable.one,R.drawable.two,R.drawable.three,R.drawable.two,R.drawable.two,R.drawable.two};
        for (int i =0 ; i<5 ; i++)
        {


            list.add(new SingleRow(titles[i],images[i]));
        }



    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup)
    {

        LayoutInflater inflater =(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View  row =  inflater.inflate(R.layout.single_row2,viewGroup,false);
        TextView title= (TextView) row.findViewById(R.id.textView);
        //  TextView description= (TextView) row.findViewById(R.id.textView2);

        //ImageView image= (ImageView) row.findViewById(R.id.imageView);
        SingleRow temp = list.get(i);
        title.setText(temp.title);
        //description.setText(temp.description);
        //image.setImageResource(temp.image);
        return row;
    }

}
Ectomorph answered 19/4, 2016 at 8:31 Comment(2)
see my answer bellowKuster
see my updated answer bro...Kuster
K
2

call this method on Item click

private void openDialogManager() {

        final CharSequence[] items = {"Male", "Female"};

        new AlertDialog.Builder(mActivity)
                .setTitle("Gender")
                .setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        switch (which) {

                            case 0:
                                Utils.showToast(mActivity, "Male");
                                dialog.dismiss();
                                break;

                            case 1:
                                Utils.showToast(mActivity, "Female");
                                dialog.dismiss();
                                break;
                        }
                        dialog.dismiss();
                    }
                })
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                })
                .show();
    }
Kuster answered 19/4, 2016 at 8:37 Comment(14)
I forgot you want single choice mode so check my updated answer @EctomorphKuster
You can see the error below i have got when i pasted above method in my first item of list view @bajirao.Ectomorph
just add your toast bro..@Ectomorph and instead of mActivity add your getApplicationContext()Kuster
what about error in private void openDialogManager?Ectomorph
show me what it is suggesting and how you have called this functionKuster
@Ectomorph call this method like openDialogManager();Kuster
i have uploaded the image please do have look. @BajiraoEctomorph
@Ectomorph you must be fresher dont you?? add above function outside of onCreate...and just call it using openDialogManager (); from onItemClickKuster
haha jus started my andriod... i have pasted outside my onitem click still there are errors. could please see my above code and tell me where to paste.Ectomorph
i have pasted outside oncreate method now but still when i click my first item in list view my app crashes.Ectomorph
its all done now my app is not crashing , i jus used used activity.this instead of getApplicationContext. but i have another separate issue may be you can help me out again just like this time.Ectomorph
#36727660Ectomorph
follow this androidhive.info/2012/02/…Kuster
i think you didnt understand what i want to ask. i know how to design a customlisstview even i already have one ..Ectomorph

© 2022 - 2024 — McMap. All rights reserved.