2016-05-11 9 views
2

ListViewから複数の項目を選択してMultiChoiceModeListenerを実装しようとしています。私の現在の進歩は以下の通りです。しかし、それは動作しません。それは長いプレスでさえも何の行動も起こさない。私のコードや何か間違っているものはありませんか?どんな助けでも大歓迎です。MultiChoiceModeListenerが機能しません

public class FragmentFavorite extends Fragment { 

    ListView lvFavoriteItems; 
    Activity mActivity = null; 
    Context mContext; 
    ArrayList<String> names = new ArrayList<>(Arrays.asList("My name is a", "My name is b", 
      "My name is c", "My name is d", "My name is e", "My name is f")); 
    ArrayList<String> phone = new ArrayList<>(Arrays.asList("9895653263", "9895653264", "9895653265", 
      "9895653266", "9895653267", "9895653267")); 

    @Override 
    public void onAttach(Context context) { 
     super.onAttach(context); 
     if(mActivity == null) { 
      this.mActivity = getActivity(); 
     } 
     this.mContext = context; 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View root = inflater.inflate(R.layout.favorite_list, container, false); 
     lvFavoriteItems = (ListView) root.findViewById(R.id.lv_item_list); 
     FavoriteAdapter fav = new FavoriteAdapter(); 
     lvFavoriteItems.setAdapter(fav); 
     lvFavoriteItems.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL); 
     lvFavoriteItems.setMultiChoiceModeListener(new FavMultiChoiceModeListener()); 
     return root; 
    } 

    private class FavoriteAdapter extends BaseAdapter { 

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

     @Override 
     public Object getItem(int i) { 
      return null; 
     } 

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

     @Override 
     public View getView(int position, View convertView, ViewGroup viewGroup) { 
      View listItem; 
      LayoutInflater inflater = (LayoutInflater) mContext 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      if (convertView == null) { 
       listItem = inflater.inflate(R.layout.contact_item_2_line, null); 
      } else { 
       listItem = convertView; 
      } 
      TextView tv1 = (TextView) listItem.findViewById(R.id.tv_item_name); 
      tv1.setText(names.get(position)); 
      TextView tv2 = (TextView) listItem.findViewById(R.id.tv_phone); 
      tv2.setText(phone.get(position)); 
      listItem.setId(position); 
      return listItem; 
     } } 

    private class FavMultiChoiceModeListener implements ListView.MultiChoiceModeListener { 

     @Override 
     public void onItemCheckedStateChanged(ActionMode mode, int i, long l, boolean b) { 
      final int checkedCount = lvFavoriteItems.getCheckedItemCount(); 
      mode.setSubtitle("" + checkedCount + " items selected "); 
     } 

     @Override 
     public boolean onCreateActionMode(ActionMode actionMode, Menu menu) { 
      MenuInflater inflater = mActivity.getMenuInflater(); 
      inflater.inflate(R.menu.selection_menu, menu); 
      actionMode.setTitle("Select Items"); 
      return true; 

     } 

     @Override 
     public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) { 
      return true; 
     } 

     @Override 
     public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) { 
      Toast.makeText(mActivity, "Clicked " + menuItem.getTitle(), 
        Toast.LENGTH_SHORT).show(); 
      return true; 
     } 

     @Override 
     public void onDestroyActionMode(ActionMode actionMode) { 

     } 
    } 
} 

答えて

0

あなたはBaseAdapterから延びるしているので、あなたはあなたのgetView方法でconvertView.setLongClickable(true)を呼び出す必要があります。

関連する問題