如何在android中选择上下文项获得位置?
您可以使用如何在android中选择上下文项获得位置?,android,position,android-context,selected,Android,Position,Android Context,Selected,您可以使用AdapterContextMenuInfo public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); menu.setHeaderTitle("Playlist Option"); MenuInflater inflater = g
AdapterContextMenuInfo
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Playlist Option");
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
public boolean onContextItemSelected(MenuItem item) {
// here I select the particular list value, that the value position I need that is only I delete that position from server. what are the data i fetch here? and its possible to get position from array list?
}
公共类DataDemo扩展活动{
public ArrayList ContactList=new ArrayList();
私有静态最终字符串标记\u ID=“ID”;
私有静态最终字符串标记_NAME=“NAME”;
私有静态最终字符串标记_PHNO=“PHNO”;
私有静态int项索引;
私有ListView lst;
私人名单联系人;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u database\u demo);
lst=(ListView)findViewById(R.id.lstContact);
}
@凌驾
public void onCreateContextMenu(ContextMenu,视图v,
上下文菜单信息(menuInfo){
//TODO自动生成的方法存根
super.onCreateContextMenu(menu,v,menuInfo);
if(v.getId()==R.id.lstContact){
menu.setHeaderIcon(R.drawable.message);
菜单。设置标题(“联系人”);
menu.add(0,1,menu.NONE,“编辑联系人”);
menu.add(0,2,menu.NONE,“删除联系人”);
menu.add(0,3,menu.NONE,“取消”);
}
}
@凌驾
公共布尔值onContextItemSelected(MenuItem项){
//TODO自动生成的方法存根
AdapterView.AdapterContextMenuInfo menuInfo;//您可以在选定的上下文项上进行选择
menuInfo=(AdapterContextMenuInfo)项。getMenuInfo();
itemIndex=menuInfo.position;//您将获得所选项目的位置
最终联系人con=contacts.get(itemIndex);
开关(item.getItemId()){
案例1:
Intent editIntent=新的Intent(DataDemo.this、UpdateContact.class);
editIntent.putExtra(TAG_ID,con.getID());
editIntent.putExtra(TAG_NAME,con.getName());
putExtra(TAG_PHNO,con.getPhNo());
星触觉(编辑意图);
打破
案例2:
新建AlertDialog.Builder(此)
.setIcon(R.drawable.document_delete)
.setTitle(“确认删除”)
.setMessage(“您确定吗?”)
.setPositiveButton(“是”,
新建DialogInterface.OnClickListener(){
公共void onClick(对话框接口对话框,
int(其中){
//TODO自动生成的方法存根
DatabaseHandler db=新的DatabaseHandler(
DataDemo.this);
db.删除联系人(con);
Toast.makeText(DataDemo.this,con.getName()+“已删除”,
吐司。长度(短)。show();
填充列表();
}
}).setNegativeButton(“否”,null).show();
打破
违约:
打破
}
返回super.onContextItemSelected(项目);
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
MenuItem addNew=menu.add(0,1,0,“创建联系人”);
addNew.setOnMenuItemClickListener(新的OnMenuItemClickListener(){
公共布尔onMenuItemClick(菜单项){
//TODO自动生成的方法存根
Intent iCreate=newintent(DataDemo.this,CreateContact.class);
星触觉(iCreate);
返回true;
}
});
返回true;
}
受保护的无效填充列表(){
DatabaseHandler db=新的DatabaseHandler(此);
列表适配器adap;
ContactList.clear();
contacts=db.ReadAllContact();
用于(联系人cn:联系人){
HashMap hmap=新的HashMap();
hmap.put(TAG_NAME,cn.getName());
hmap.put(TAG_PHNO,cn.getPhNo());
联系人列表。添加(hmap);
adap=新的SimpleAdapter(此,联系人列表,
R.layout.list\u item\u视图,
新字符串[]{TAG_NAME,TAG_PHNO},新int[]{
R.id.lstName,R.id.lstPhNo});
1.设置适配器(adap);
registerForContextMenu(lst);
}
}
@凌驾
受保护的void onResume(){
//TODO自动生成的方法存根
super.onResume();
填充列表();
}
}
有一种方法,indexOf(objectobj),试试看。在隐藏内容时给出删除方法。如何?请帮助。歌曲列表显示我给删除方式的时候隐藏歌曲名如何?但我的代码给出的删除方式是从服务器端删除然后再返回后才显示结果。我想在给删除的时候意味着名字是怎么隐藏的?
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Playlist Option");
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
public boolean onContextItemSelected(MenuItem item) {
// here I select the particular list value, that the value position I need that is only I delete that position from server. what are the data i fetch here? and its possible to get position from array list?
}
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.delete:
String itemName = r.get[(int) info.id]; // This item you will delete
// from list
return true;
}
}
public class DataDemo extends Activity {
public ArrayList<HashMap<String, String>> ContactList = new ArrayList<HashMap<String, String>>();
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
private static final String TAG_PHNO = "phNo";
private static int itemIndex;
private ListView lst;
private List<Contact> contacts;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_database_demo);
lst = (ListView) findViewById(R.id.lstContact);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
super.onCreateContextMenu(menu, v, menuInfo);
if (v.getId() == R.id.lstContact) {
menu.setHeaderIcon(R.drawable.message);
menu.setHeaderTitle("Contacts");
menu.add(0, 1, Menu.NONE, "Edit Contact");
menu.add(0, 2, Menu.NONE, "Delete Contact");
menu.add(0, 3, Menu.NONE, "Cancel");
}
}
@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
AdapterView.AdapterContextMenuInfo menuInfo;//you can select on context item selected
menuInfo = (AdapterContextMenuInfo) item.getMenuInfo();
itemIndex = menuInfo.position;//you will get position of selected item
final Contact con = contacts.get(itemIndex);
switch (item.getItemId()) {
case 1:
Intent editIntent = new Intent(DataDemo.this, UpdateContact.class);
editIntent.putExtra(TAG_ID, con.getID());
editIntent.putExtra(TAG_NAME, con.getName());
editIntent.putExtra(TAG_PHNO, con.getPhNo());
startActivity(editIntent);
break;
case 2:
new AlertDialog.Builder(this)
.setIcon(R.drawable.document_delete)
.setTitle("Confirm Delete")
.setMessage("Are you sure?")
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
DatabaseHandler db = new DatabaseHandler(
DataDemo.this);
db.DeleteContact(con);
Toast.makeText(DataDemo.this, con.getName() + " Deleted",
Toast.LENGTH_SHORT).show();
fillList();
}
}).setNegativeButton("No", null).show();
break;
default:
break;
}
return super.onContextItemSelected(item);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem addNew = menu.add(0, 1, 0, "Create Contact");
addNew.setOnMenuItemClickListener(new OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
// TODO Auto-generated method stub
Intent iCreate = new Intent(DataDemo.this, CreateContact.class);
startActivity(iCreate);
return true;
}
});
return true;
}
protected void fillList() {
DatabaseHandler db = new DatabaseHandler(this);
ListAdapter adap;
ContactList.clear();
contacts = db.ReadAllContact();
for (Contact cn : contacts) {
HashMap<String, String> hmap = new HashMap<String, String>();
hmap.put(TAG_NAME, cn.getName());
hmap.put(TAG_PHNO, cn.getPhNo());
ContactList.add(hmap);
adap = new SimpleAdapter(this, ContactList,
R.layout.list_item_view,
new String[] { TAG_NAME, TAG_PHNO }, new int[] {
R.id.lstName, R.id.lstPhNo });
lst.setAdapter(adap);
registerForContextMenu(lst);
}
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
fillList();
}
}