package com.xiehande.listViewHomework.activity;import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;public class MaiinActivity extends ListActivity {
ListAdapter adapter;
String mColumnNames[] = new String[] { "name", "address", "phone" };
int mViewIds[] = new int[] { R.id.txtName, R.id.txtAddress, R.id.txtPhone };
TextView txtNameTV;
public Menu menu;
private int menuItemId=Menu.FIRST;//初始值为1
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
private List<Map<String, Object>> getData() {

Map<String, Object> map = new HashMap<String, Object>();

map.put("name", "aaa");
map.put("address", "ccv");
map.put("phone", "12345");
list.add(map);

map = new HashMap<String, Object>();
map.put("name", "bbb");
map.put("address", "sdd");
map.put("phone", "114");
list.add(map); map = new HashMap<String, Object>();
map.put("name", "ccc");
map.put("address", "ssc");
map.put("phone", "321");
list.add(map);

map = new HashMap<String, Object>();
map.put("name", "ddd");
map.put("address", "ssc");
map.put("phone", "821");
list.add(map);
return list;
} /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.getListView().addHeaderView(this.getLayoutInflater().inflate(R.layout.header_view, null), null, false);
 adapter = new SimpleAdapter(this, this.getData(), R.layout.row_ui, mColumnNames, mViewIds);
this.setListAdapter(adapter);
//注册上下文菜单
registerForContextMenu(getListView()); } @Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
this.menu=menu;
addMenu();
return super.onCreateOptionsMenu(menu);
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("上下文菜单");
menu.setHeaderIcon(R.drawable.face);
menu.add(1, menuItemId, 1, "删除该联系人");
menu.add(1, menuItemId, 2, "修改该联系人");
menu.add(1, menuItemId, 3, "标记该联系人");

}


private  int selectedId=0;
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
selectedId=position;
Log.i("MaiinActivity", String.valueOf(selectedId));
} @Override
public boolean onContextItemSelected(MenuItem item) {
//这样可以获取长按着弹出上下文菜单的LIstView点击的ID值
AdapterView.AdapterContextMenuInfo menuInfo=(AdapterContextMenuInfo) item.getMenuInfo();
    final int id=(int) menuInfo.id;
    if (-1 == id) {
        super.onContextItemSelected(item); 
       }     switch (item.getItemId()) {
case 1:
Log.i("MaiinActivity", String.valueOf(menuInfo.position));
Log.i("MaiinActivity", String.valueOf(id));
list.remove(id);
//this.adapter.notifyAll();
break;
case 2:
break;
default:
break;
}
   
return super.onContextItemSelected(item);
} private void addMenu() {
MenuItem addMenuItem=menu.add(0, menuItemId++, 1, "添加联系人");
addMenuItem.setIcon(R.drawable.file);
addMenuItem.setOnMenuItemClickListener(menuItemClickListener);
MenuItem deleteMenuItem=menu.add(0, menuItemId, 2, "联系人设置");
deleteMenuItem.setIcon(R.drawable.icon);
addMenuItem.setOnMenuItemClickListener(menuItemClickListener);
}
private OnMenuItemClickListener menuItemClickListener=new OnMenuItemClickListener() {

@Override
public boolean onMenuItemClick(MenuItem item) {
// TODO Auto-generated method stub
Intent intent;
if(item.getItemId()==1){
     intent=new Intent(MaiinActivity.this, ProcessActivity.class);
    startActivityForResult(intent, R.layout.process);
}else if(item.getItemId()==2){
   //联系人设置没有处理
}
return true;
}
}; @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case R.layout.process:
if (resultCode == 20) {
Bundle bundle=data.getExtras();
String name=bundle.getString("name");
String address=bundle.getString("address");
String phone=bundle.getString("phone");
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", name);
map.put("address", address);
map.put("phone",phone);
list.add(map);
} else if (resultCode == 21) {
                Toast.makeText(MaiinActivity.this,"你取消了操作", Toast.LENGTH_LONG).show();
                
}
break; default:
break;
} }
}代码已经附上去了。千万不要在网上找人家的例子给我浮上来。因为我也是搜寻了很多的解决方法。按理说,应该正确。但是,只要我加上红色的那两句代码的其中一句就会崩溃了。不知道为啥,麻烦大家教我如何解决。为什么就删除不了list中的选项了都

解决方案 »

  1.   

    12-26 05:22:28.670: ERROR/AndroidRuntime(979): FATAL EXCEPTION: main
    12-26 05:22:28.670: ERROR/AndroidRuntime(979): java.lang.IllegalStateException: Cannot add header view to list -- setAdapter has already been called.
    12-26 05:22:28.670: ERROR/AndroidRuntime(979):     at android.widget.ListView.addHeaderView(ListView.java:261)
    12-26 05:22:28.670: ERROR/AndroidRuntime(979):     at com.xiehande.listViewHomework.activity.MaiinActivity.onContextItemSelected(MaiinActivity.java:126)
    12-26 05:22:28.670: ERROR/AndroidRuntime(979):     at android.app.Activity.onMenuItemSelected(Activity.java:2199)
    12-26 05:22:28.670: ERROR/AndroidRuntime(979):     at com.android.internal.policy.impl.PhoneWindow$ContextMenuCallback.onMenuItemSelected(PhoneWindow.java:2744)
    12-26 05:22:28.670: ERROR/AndroidRuntime(979):     at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:143)
    12-26 05:22:28.670: ERROR/AndroidRuntime(979):     at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:855)
    12-26 05:22:28.670: ERROR/AndroidRuntime(979):     at com.android.internal.view.menu.MenuDialogHelper.onClick(MenuDialogHelper.java:137)
    12-26 05:22:28.670: ERROR/AndroidRuntime(979):     at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:874)
    12-26 05:22:28.670: ERROR/AndroidRuntime(979):     at android.widget.AdapterView.performItemClick(AdapterView.java:284)
    12-26 05:22:28.670: ERROR/AndroidRuntime(979):     at android.widget.ListView.performItemClick(ListView.java:3382)
    12-26 05:22:28.670: ERROR/AndroidRuntime(979):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:1696)
    12-26 05:22:28.670: ERROR/AndroidRuntime(979):     at android.os.Handler.handleCallback(Handler.java:587)
    12-26 05:22:28.670: ERROR/AndroidRuntime(979):     at android.os.Handler.dispatchMessage(Handler.java:92)
    12-26 05:22:28.670: ERROR/AndroidRuntime(979):     at android.os.Looper.loop(Looper.java:123)
    12-26 05:22:28.670: ERROR/AndroidRuntime(979):     at android.app.ActivityThread.main(ActivityThread.java:4627)
    12-26 05:22:28.670: ERROR/AndroidRuntime(979):     at java.lang.reflect.Method.invokeNative(Native Method)
    12-26 05:22:28.670: ERROR/AndroidRuntime(979):     at java.lang.reflect.Method.invoke(Method.java:521)
    12-26 05:22:28.670: ERROR/AndroidRuntime(979):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    12-26 05:22:28.670: ERROR/AndroidRuntime(979):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    12-26 05:22:28.670: ERROR/AndroidRuntime(979):     at dalvik.system.NativeStart.main(Native Method)
    12-26 05:32:58.490: ERROR/AndroidRuntime(994): FATAL EXCEPTION: main
    12-26 05:32:58.490: ERROR/AndroidRuntime(994): java.lang.IllegalStateException: Cannot add header view to list -- setAdapter has already been called.
    12-26 05:32:58.490: ERROR/AndroidRuntime(994):     at android.widget.ListView.addHeaderView(ListView.java:261)
    12-26 05:32:58.490: ERROR/AndroidRuntime(994):     at com.xiehande.listViewHomework.activity.MaiinActivity.onContextItemSelected(MaiinActivity.java:126)
    12-26 05:32:58.490: ERROR/AndroidRuntime(994):     at android.app.Activity.onMenuItemSelected(Activity.java:2199)
    12-26 05:32:58.490: ERROR/AndroidRuntime(994):     at com.android.internal.policy.impl.PhoneWindow$ContextMenuCallback.onMenuItemSelected(PhoneWindow.java:2744)
    12-26 05:32:58.490: ERROR/AndroidRuntime(994):     at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:143)
    12-26 05:32:58.490: ERROR/AndroidRuntime(994):     at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:855)
    12-26 05:32:58.490: ERROR/AndroidRuntime(994):     at com.android.internal.view.menu.MenuDialogHelper.onClick(MenuDialogHelper.java:137)
    12-26 05:32:58.490: ERROR/AndroidRuntime(994):     at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:874)
    12-26 05:32:58.490: ERROR/AndroidRuntime(994):     at android.widget.AdapterView.performItemClick(AdapterView.java:284)
    12-26 05:32:58.490: ERROR/AndroidRuntime(994):     at android.widget.ListView.performItemClick(ListView.java:3382)
    12-26 05:32:58.490: ERROR/AndroidRuntime(994):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:1696)
    12-26 05:32:58.490: ERROR/AndroidRuntime(994):     at android.os.Handler.handleCallback(Handler.java:587)
    12-26 05:32:58.490: ERROR/AndroidRuntime(994):     at android.os.Handler.dispatchMessage(Handler.java:92)
    12-26 05:32:58.490: ERROR/AndroidRuntime(994):     at android.os.Looper.loop(Looper.java:123)
    12-26 05:32:58.490: ERROR/AndroidRuntime(994):     at android.app.ActivityThread.main(ActivityThread.java:4627)
    12-26 05:32:58.490: ERROR/AndroidRuntime(994):     at java.lang.reflect.Method.invokeNative(Native Method)
    12-26 05:32:58.490: ERROR/AndroidRuntime(994):     at java.lang.reflect.Method.invoke(Method.java:521)
    12-26 05:32:58.490: ERROR/AndroidRuntime(994):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    12-26 05:32:58.490: ERROR/AndroidRuntime(994):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    12-26 05:32:58.490: ERROR/AndroidRuntime(994):     at dalvik.system.NativeStart.main(Native Method)
      

  2.   

    更新adapter呗 adapter.notifyDataSetChanged();
      

  3.   

    更新adapter呗 adapter.notifyDataSetChanged(); 嗯,楼上说的对。确实是需要更新.但是我的adapter是SimpleAdapter,没有这个方法。我之前还想找这个方法试试。但是就是找不到,就只要notify(),notifyAll()。
    那么,怎么解决呢?改下SimpleAdapter还是???
      

  4.   

    notify(),notifyAll()是Object的方法~
      

  5.   


    继承的BaseAdapter怎么会没有这方法
      

  6.   

    问题已经解决了。解决的方法就是 不能使用ListAdapter adapter=new SimpleAdapter(),而是:SimpleAdapter adapter=new SimpleAdapter()
    这个朋友说的好。看文档可知:
    public interface
    ListAdapter
    implements Adapter的确,ListAdapter是接口,实现了这个接口的有 ArrayAdapter<T>, BaseAdapter, CursorAdapter, HeaderViewListAdapter, ResourceCursorAdapter, SimpleAdapter, SimpleCursorAdapter, WrapperListAdapter 
    其中就有SimpleAdapter
    按理说:ListAdapter adapter=new SimpleAdapter();那么,应该是有这个方法notifyDataSetChanged
    但是,就是没有,只有notify()和notifyAll()。这个不合理呀不知道是不是android本身的问题了。
    所以,只能使用SimpleAdapter而不能使用面向接口编程的方式。个人觉得是个bug