2015-07-11 128 views
1

基於我的列表視圖,由於單擊事件中的錯誤,我無法調試我的應用程序。我在其他問題中看到過這個錯誤,但答案與我的問題不相關。我不確定是否有任何必要的代碼丟失或不需要。可以做些什麼來解決這個錯誤?所有的幫助將不勝感激。由於「方法未覆蓋其超類的方法」,點擊事件不能工作

<string-array name="continent_names"> 
    //item 0 <item>@string/africa</item> 
    //item 1 <item>@string/asia</item> 
    //item 2 <item>@string/europe</item> 
</string-array> 

<string-array name="continent_descriptions"> 
    //item 0 <item>@string/africa_description</item> 
    //item 1 <item>@string/asia_description </item> 
    //item 2 <item>@string/europe_description </item> 
</string-array> 

FragmentWorld.java

public class FragmentWorld extends ListFragment implements SearchView.OnQueryTextListener { 

    private WorldListAdapter mAdapter; 

    public FragmentWorld() { 
     // Required empty constructor 
    } 

    public static FragmentWorld newInstance() { 
     return new FragmentWorld(); 
    } 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment_world, container, false); 
     setHasOptionsMenu(true); 
     initialize(view); 
     return view; 
    } 

    List<World> list = new ArrayList<World>(); 
    private void initialize(View view) { 
     String[] items = getActivity().getResources().getStringArray(R.array.continent_names); 
     String[] itemDescriptions = getActivity().getResources().getStringArray(R.array.continent_descriptions); 
     for (int n = 0; n < items.length; n++){ 
      World world = new World(); 
      world.setID(); 
      world.setName(items[n]); 
      world.setDescription(itemDescriptions[n]); 
      list.add(world); 
     } 

     mAdapter = new WorldListAdapter(list, getActivity()); 
     setListAdapter(mAdapter); 
    } 

      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

       // get the adapter, then get the name from the adapter at that position 
       WorldListAdapter adapter = (WorldListAdapter) parent.getAdapter(); 
       String country = adapter.getItem(position); 

       if (mTwoPane) { 
        setItemNormal(); 
        View rowView = view; 
        setItemSelected(rowView); 

        Fragment newFragment; 
        if (country.equals(view.getResources().getString(R.string.africa))) { 
         newFragment = new FragmentAfrica(); 
        } else if (country.equals(view.getResources().getString(R.string.asia))) { 
         newFragment = new FragmentAsia(); 
        } else if (country.equals(view.getResources().getString(R.string.europe))) { 
         newFragment = new FragmentEurope(); 
        } else { 
         newFragment = new FragmentAfrica(); 
        } 
        WorldActivity activity = (WorldActivity) view.getContext(); 
        FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction(); 
        transaction.replace(R.id.detail_container, newFragment); 
        transaction.commit(); 
       } else { 
        Intent intent; 
        if (country.equals(view.getResources().getString(R.string.africa))) { 
         intent = new Intent(getActivity(), AfricaActivity.class); 
        } else if (country.equals(view.getResources().getString(R.string.asia))) { 
         intent = new Intent(getActivity(), AsiaActivity.class); 
        } else if (country.equals(view.getResources().getString(R.string.europe))) { 
         intent = new Intent(getActivity(), EuropeActivity.class); 
        } else { 
         intent = new Intent(getActivity(), AfricaActivity.class); 
        } 
        startActivity(intent); 
       } 
      } 

    @Override 
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 
     // Set up search view 
     inflater.inflate(R.menu.menu_world, menu); 
     MenuItem item = menu.findItem(R.id.action_search); 
     SearchView searchView = (SearchView) MenuItemCompat.getActionView(item); 
     searchView.setIconifiedByDefault(true); 
     searchView.clearAnimation(); 
     searchView.setOnQueryTextListener(this); 
     searchView.setQueryHint(getResources().getString(R.string.search_hint)); 

     View close = searchView.findViewById(R.id.search_close_btn); 
     close.setBackgroundResource(R.drawable.ic_action_content_clear); 
    } 

    @Override 
    public boolean onQueryTextSubmit(String newText) { 
     return false; 
    } 

    @Override 
    public boolean onQueryTextChange(String newText) { 
     mAdapter.getFilter().filter(newText); 
     return false; 
    } 
} 

WorldListAdapter.java

public class WorldListAdapter extends BaseAdapter implements Filterable { 

    private List<World> mData; 
    private List<World> mFilteredData; 
    private LayoutInflater mInflater; 
    private ItemFilter mFilter; 

    public WorldListAdapter (List<World> data, Context context) { 
     mData = data; 
     mFilteredData = new ArrayList(mData); 
     mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

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

    @Override 
    public String getItem(int position) { 
     return mFilteredData.get(position).getName(); 
    } 

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

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

     ViewHolder holder; 
     if (convertView == null) { 
      convertView = mInflater.inflate(R.layout.list_item_dualline, parent, false); 
      holder = new ViewHolder(); 

      holder.title = (TextView) convertView.findViewById(R.id.item_name); 
      holder.description = (TextView) convertView.findViewById(R.id.item_description); 

      convertView.setTag(holder); 
     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 

     holder.title.setText(mFilteredData.get(position).getName()); 
     holder.description.setText(mFilteredData.get(position).getDescription()); 

     return convertView; 
    } 

    @Override 
    public Filter getFilter() { 
     if (mFilter == null) { 
      mFilter = new ItemFilter(); 
     } 
     return mFilter; 
    } 

    /** 
    * View holder 
    */ 
    static class ViewHolder { 
     private TextView title; 
     private TextView description; 
    } 

    private class ItemFilter extends Filter { 
     @Override 
     protected FilterResults performFiltering(CharSequence constraint) { 
      FilterResults results = new FilterResults(); 

      if (TextUtils.isEmpty(constraint)) { 
       results.count = mData.size(); 
       results.values = new ArrayList(mData); 
      } else { 
       //Create a new list to filter on 
       List<World> resultList = new ArrayList<World>(); 
       for (World str : mData) { 
        if (str.getName().toLowerCase().contains(constraint.toString().toLowerCase())) { 
         resultList.add(str); 
        } 
       } 
       results.count = resultList.size(); 
       results.values = resultList; 
      } 
      return results; 
     } 


     @SuppressWarnings("unchecked") 
     @Override 
     protected void publishResults(CharSequence constraint, FilterResults results) { 
      if (results.count == 0) { 
       mFilteredData.clear(); 
       notifyDataSetInvalidated(); 
      } else { 
       mFilteredData = (ArrayList<World>)results.values; 
       notifyDataSetChanged(); 
      } 
     } 
    } 
} 

錯誤

方法不覆蓋其超類

+0

錯誤指定了一個特定的方法 - 查看它的簽名,你必須改變了參數類型或返回類型之一。對於'@ Override'註釋的方法,它們必須完全匹配重寫的方法,否則將引發此例外 – drewmoore

回答

1

ListFragment的方法不具有onItemClick()方法。它有一個onListItemClick方法。將第一個參數從AdapterView<?> parent更改爲ListView parent

+0

讓我們[在聊天中繼續討論](http://chat.stackoverflow.com/rooms/83035/discussion-between -sweder-schellens和-macaronlover)。 –