我试图解决本书第18章的最后一个挑战,即使用ActionBarSherlock,我安装了它,但是当我试图让它工作时,当我长按列表中的一个元素时选择但不显示操作栏选项。
这基本上就是我所做的:
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
getSherlockActivity().startActionMode(new com.actionbarsherlock.view.ActionMode.Callback() {
@Override
public boolean onPrepareActionMode(
com.actionbarsherlock.view.ActionMode mode,
com.actionbarsherlock.view.Menu menu) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onDestroyActionMode(com.actionbarsherlock.view.ActionMode mode) {
// TODO Auto-generated method stub
}
@Override
public boolean onCreateActionMode(
com.actionbarsherlock.view.ActionMode mode,
com.actionbarsherlock.view.Menu menu) {
com.actionbarsherlock.view.MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.crime_list_item_context, menu);
return true;
}
@Override
public boolean onActionItemClicked(
com.actionbarsherlock.view.ActionMode mode,
com.actionbarsherlock.view.MenuItem item) {
if(item.getItemId() == R.id.menu_item_delete_crime){
CrimeAdapter adapter = (CrimeAdapter)getListAdapter();
CrimeLab crimeLab = CrimeLab.get(getActivity());
for(int i = adapter.getCount() - 1; i >= 0; i--){
if(getListView().isItemChecked(i))
crimeLab.DeleteCrime(adapter.getItem(i));
}
mode.finish();
adapter.notifyDataSetChanged();
return true;
}
return false;
}
});
编辑:
上面的代码在oncreateView中调用,这是函数的整个代码:
@TargetApi(11)
@Override
public View onCreateView(LayoutInflater inflater , ViewGroup p , Bundle args){
View v = super.onCreateView(inflater, p, args);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
if(mSubtitleVisable)
getSherlockActivity().getActionBar().setSubtitle(R.string.subtitle);
}
ListView listView = (ListView)v.findViewById(android.R.id.list);
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB){
registerForContextMenu(listView);
}
//
//
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
getSherlockActivity().startActionMode(new com.actionbarsherlock.view.ActionMode.Callback() {
@Override
public boolean onPrepareActionMode(
com.actionbarsherlock.view.ActionMode mode,
com.actionbarsherlock.view.Menu menu) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onDestroyActionMode(com.actionbarsherlock.view.ActionMode mode) {
// TODO Auto-generated method stub
}
@Override
public boolean onCreateActionMode(
com.actionbarsherlock.view.ActionMode mode,
com.actionbarsherlock.view.Menu menu) {
com.actionbarsherlock.view.MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.crime_list_item_context, menu);
return true;
}
@Override
public boolean onActionItemClicked(
com.actionbarsherlock.view.ActionMode mode,
com.actionbarsherlock.view.MenuItem item) {
if(item.getItemId() == R.id.menu_item_delete_crime){
CrimeAdapter adapter = (CrimeAdapter)getListAdapter();
CrimeLab crimeLab = CrimeLab.get(getActivity());
for(int i = adapter.getCount() - 1; i >= 0; i--){
if(getListView().isItemChecked(i))
crimeLab.DeleteCrime(adapter.getItem(i));
}
mode.finish();
adapter.notifyDataSetChanged();
return true;
}
return false;
}
});
return true;
}
});
return v;
}
the mainifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.criminalintent"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.Sherlock"
>
<activity android:name=".CrimeListActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.criminalintent.CrimeActivity"
android:label="@string/app_name" >
</activity>
<activity
android:name="com.example.criminalintent.CrimePagerActivity"
android:label="@string/app_name"
>
<meta-data android:name="android.support.PARENT_ACTIVITY"
android:value=".CrimeListActivity"/>
</activity>
</application>
</manifest>
答案 0 :(得分:-1)
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
((MyAdapter)getListAdapter()).notifyDataSetChanged();
}
protected ListView getListView() {
if (mListView == null) {
mListView = (ListView) findViewById(android.R.id.list);
}
return mListView;
}
protected ListAdapter getListAdapter() {
ListAdapter adapter = getListView().getAdapter();
if (adapter instanceof HeaderViewListAdapter) {
return ((HeaderViewListAdapter)adapter).getWrappedAdapter();
} else {
return adapter;
}
}
可以工作