Favorites.Class
public class Favorites extends SherlockFragment {
ListView lv_favorites;
ArrayList<FavoritesItems> favorites_array;
ArrayAdapter<FavoritesItems> adapter;
public Favorites() {
// TODO Auto-generated constructor stub
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View vi = inflater.inflate(R.layout.ac_favorites, container, false);
return vi;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onViewCreated(view, savedInstanceState);
init();
loadListview();
listViewListener();
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
Utils.getDeleteFavoritesContextMenu(getSherlockActivity(), menu, v, menuInfo);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
if(item.getTitle()==getResources().getString(R.string.delete_favorites)){
}
return super.onContextItemSelected(item);
}
private void listViewListener() {
lv_favorites.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
FavoritesItems item = favorites_array.get(position);
Utils.gotoNewspaper(getSherlockActivity(), item.getUrl(),item.getName());
}
});
}
@Override
public void onSaveInstanceState(Bundle outState) {
if (outState != null) {
super.onSaveInstanceState(outState);
}
}
private void init() {
favorites_array = new ArrayList<FavoritesItems>();
lv_favorites = (ListView) getView().findViewById(R.id._lv_favorites);
}
private void loadListview() {
favorites_array = Utils.getFavorites(getSherlockActivity());
adapter = new MyAdpater(getSherlockActivity());
lv_favorites.setAdapter(adapter);
registerForContextMenu(lv_favorites);
}
public class MyAdpater extends ArrayAdapter<FavoritesItems> {
private LayoutInflater inf;
public MyAdpater(Context context) {
super(getSherlockActivity(), R.layout.item_favorites, favorites_array);
this.inf = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (vi == null) {
vi = inf.inflate(R.layout.item_favorites, parent, false);
}
FavoritesItems item = favorites_array.get(position);
TextView tv_name = (TextView) vi
.findViewById(R.id._tv_item_favorites_newspapersname);
tv_name.setText(item.getName());
ImageView img_flag = (ImageView) vi
.findViewById(R.id._img_item_favorites_flag);
UrlImageViewHelper.setUrlDrawable(img_flag, item.getFlag());
return vi;
}
}
}
错误
03-28 07:46:50.810: E/AndroidRuntime(2209): FATAL EXCEPTION: main
03-28 07:46:50.810: E/AndroidRuntime(2209): Process: com.brnbykinc.worldnewspapers, PID: 2209
03-28 07:46:50.810: E/AndroidRuntime(2209): java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0
03-28 07:46:50.810: E/AndroidRuntime(2209): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
03-28 07:46:50.810: E/AndroidRuntime(2209): at java.util.ArrayList.get(ArrayList.java:308)
03-28 07:46:50.810: E/AndroidRuntime(2209): at com.brnbykinc.worlnewspapers.Search.onContextItemSelected(Search.java:225)
03-28 07:46:50.810: E/AndroidRuntime(2209): at android.support.v4.app.Fragment.performContextItemSelected(Fragment.java:1623)
03-28 07:46:50.810: E/AndroidRuntime(2209): at android.support.v4.app.FragmentManagerImpl.dispatchContextItemSelected(FragmentManager.java:2015)
03-28 07:46:50.810: E/AndroidRuntime(2209): at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:381)
03-28 07:46:50.810: E/AndroidRuntime(2209): at com.actionbarsherlock.app.SherlockFragmentActivity.onMenuItemSelected(SherlockFragmentActivity.java:210)
03-28 07:46:50.810: E/AndroidRuntime(2209): at com.android.internal.policy.impl.PhoneWindow$DialogMenuCallback.onMenuItemSelected(PhoneWindow.java:3864)
03-28 07:46:50.810: E/AndroidRuntime(2209): at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
03-28 07:46:50.810: E/AndroidRuntime(2209): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
03-28 07:46:50.810: E/AndroidRuntime(2209): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
03-28 07:46:50.810: E/AndroidRuntime(2209): at com.android.internal.view.menu.MenuDialogHelper.onClick(MenuDialogHelper.java:167)
03-28 07:46:50.810: E/AndroidRuntime(2209): at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:941)
03-28 07:46:50.810: E/AndroidRuntime(2209): at android.widget.AdapterView.performItemClick(AdapterView.java:299)
03-28 07:46:50.810: E/AndroidRuntime(2209): at android.widget.AbsListView.performItemClick(AbsListView.java:1113)
03-28 07:46:50.810: E/AndroidRuntime(2209): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2904)
03-28 07:46:50.810: E/AndroidRuntime(2209): at android.widget.AbsListView$3.run(AbsListView.java:3638)
03-28 07:46:50.810: E/AndroidRuntime(2209): at android.os.Handler.handleCallback(Handler.java:733)
03-28 07:46:50.810: E/AndroidRuntime(2209): at android.os.Handler.dispatchMessage(Handler.java:95)
03-28 07:46:50.810: E/AndroidRuntime(2209): at android.os.Looper.loop(Looper.java:136)
03-28 07:46:50.810: E/AndroidRuntime(2209): at android.app.ActivityThread.main(ActivityThread.java:5017)
03-28 07:46:50.810: E/AndroidRuntime(2209): at java.lang.reflect.Method.invokeNative(Native Method)
03-28 07:46:50.810: E/AndroidRuntime(2209): at java.lang.reflect.Method.invoke(Method.java:515)
03-28 07:46:50.810: E/AndroidRuntime(2209): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-28 07:46:50.810: E/AndroidRuntime(2209): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-28 07:46:50.810: E/AndroidRuntime(2209): at dalvik.system.NativeStart.main(Native Method)
我的问题是什么?谢谢你...
* 编辑:评论部分*
public static void getAddFavoritesContextMenu(Context context, ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
String[] con_menu = context.getResources().getStringArray( R.array.context_menu);
int con_menu_length = context.getResources().getStringArray( R.array.context_menu).length;
// menu.setHeaderTitle(context.getResources().getString( // R.string.context_menu_title));
/** i=0 info, i=1 add favorites **/
for (int i = 0; i < con_menu_length; i++) {
menu.add(Menu.NONE, i, i, con_menu[i]);
}
}