当我点击我的列表视图项目时,我想要一个弹出菜单
i want a popup menu when i click on my listview item
我的 activity 包含列表视图我在这个列表视图中有五个项目。 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;
}
}
点击项目时调用此方法
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();
}
This is how i implemented the method and the suggestion also
我的 activity 包含列表视图我在这个列表视图中有五个项目。 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;
}
}
点击项目时调用此方法
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();
}
This is how i implemented the method and the suggestion also