java - 由于 "Method does not override method of its superclass"错误,单击事件无法正常工作

标签 java android xml android-listview android-listfragment

根据我的 ListView ,由于点击事件中的错误,我无法调试我的应用程序。我在其他问题中看到过这个错误,但答案与我的问题无关。我不确定是否缺少或不需要任何必要的代码。如何解决此错误?我们将不胜感激。

字符串

<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>

fragment 世界.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();
            }
        }
    }
}

错误

Method does not override method of its superclass

最佳答案

ListFragment没有 onItemClick()方法。它有一个 onListItemClick方法。将第一个参数从 AdapterView<?> parent 更改为至 ListView parent .

关于java - 由于 "Method does not override method of its superclass"错误,单击事件无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31357349/

相关文章:

java - 即使线程当前不在方法中,inputstream.read() 超时秒数也会计数

java - 带 || 的递归返回语句在里面

java - 更改密码 Activity

java - 无法通过 maven 解码 xml

java - 将任意数字转换为一位数

java - 对于 Wildfly 中以 .html 文件结尾的 URL,此 URL 不支持 HTTP 方法 POST

java - 在android广播接收器中创建数据库的代码

android - Intent.ACTION_CALL 启动 skype 通话而不是 "normal"电话

php - MySQL php从数据库获取项目并存储在XML中(重复项目)

java - 我可以将java测试复制到Katalon studio吗