如何从列表适配器显示上下文操作栏?

时间:2013-12-20 19:15:15

标签: android listview android-actionbar actionbarsherlock contextual-action-bar

我是Android开发的菜鸟,我试图在用户点击listview项目上的按钮时显示来自List_Adapter的CAB。我正在关注ActionbarSherlock sample,但我无法弄清楚为什么我收到错误“方法startActionMode(Child_Locations_ListAdapeter.AnActionModeOfEpicProportions)未定义类型Child_Locations_ListAdapeter”。非常感谢任何帮助。

CODE

public class Child_Locations_ListAdapeter extends BaseAdapter implements OnClickListener {

private ArrayList<HashMap<String, String>> listData;

private LayoutInflater layoutInflater;

Context c;
int selection;
ActionMode mMode;

public Child_Locations_ListAdapeter(Context context, ArrayList listData) {
    this.listData = listData;
    layoutInflater = LayoutInflater.from(context);
    c= context;
}

public void remove(int position) {
    listData.remove(position);
    notifyDataSetChanged();
}

public void insert(int position, String item, String item2 ) {
    HashMap<String, String> map = new HashMap<String, String>();
    map.put("Location",item );
    map.put("Location_Address",item2 );
    listData.add(position, map);
    notifyDataSetChanged();
}

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

@Override
public Object getItem(int position) {
    return listData.get(position);
}

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


public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        convertView = layoutInflater.inflate(R.layout.layout_row_locations, null);
        holder = new ViewHolder();
        //holder.userAvatar = (ImageView) convertView.findViewById(R.id.client_image);
        holder.locationName = (TextView) convertView.findViewById(R.id.tv_locationname);
        holder.locationAddress = (TextView) convertView.findViewById(R.id.tv_locationaddress);
        holder.delete = (ImageButton) convertView.findViewById(R.id.action_delete);
        holder.delete.setOnClickListener(Child_Locations_ListAdapeter.this);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.delete.setTag(position);

    //holder.userAvatar.setText(((Comment_Item) listData.get(position)).getUserName());
    try{
        holder.locationName.setText((listData.get(position)).get("Location"));
        holder.locationAddress.setText((listData.get(position)).get("Location_Address"));
    }catch(Exception e){
        e.printStackTrace();
        //holder.locationName.setText("No Locations For This Child");
        //holder.locationAddress.setText("");
    }
    //Geocoder_Class geocoder = new Geocoder_Class(c);
    //holder.locationAddress.setText(geocoder.getAddress((listData.get(position)).get("Latitude"), (listData.get(position)).get("Longitude")));

    return convertView;
}

static class ViewHolder {
    ImageView location_picture;
    TextView locationName;
    TextView locationAddress;
    ImageButton delete;
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {

    case R.id.action_delete:

        selection = (Integer)v.getTag();
        Log.e("Selection", String.valueOf(selection));

        mMode = startActionMode(new AnActionModeOfEpicProportions());//<--The method startActionMode(Child_Locations_ListAdapeter.AnActionModeOfEpicProportions) is undefined for the type Child_Locations_ListAdapeter
    }
}


public class AnActionModeOfEpicProportions implements ActionMode.Callback {
    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        //Used to put dark icons on light action bar
        //boolean isLight = SampleList.THEME == R.style.Theme_Sherlock_Light;

        menu.add("Save")
            .setIcon( R.drawable.ic_compose)
            .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

        menu.add("Search")
            .setIcon( R.drawable.ic_search)
            .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

        menu.add("Refresh")
            .setIcon(R.drawable.ic_refresh)
            .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

        menu.add("Save")
            .setIcon(R.drawable.ic_compose)
            .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

        menu.add("Search")
            .setIcon(R.drawable.ic_search)
            .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

        menu.add("Refresh")
            .setIcon( R.drawable.ic_refresh)
            .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

        return true;
    }

    @Override
    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        return false;
    }

    @Override
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        Toast.makeText(c, "Got click: " + item, Toast.LENGTH_SHORT).show();
        mode.finish();
        return true;
    }

    @Override
    public void onDestroyActionMode(ActionMode mode) {
    }


}

}

1 个答案:

答案 0 :(得分:0)

方法startActionMode绑定到Activity,而不绑定到ListAdapter

http://developer.android.com/reference/android/app/Activity.html#startActionMode(android.view.ActionMode.Callback)

((Activity) c).startActionMode(new AnActionModeOfEpicProportions());