如何调用setOnItemClickListener()

时间:2016-03-16 15:08:09

标签: android listview onitemclicklistener

我有一个列表视图,我想在点击它时删除列表视图中的项目 我使用了list.setOnItemClickListener(this);但是这个'用红色加下划线说,

  AdapterView中的

setOnItemClickListener(Android.Widget.AdapterView.OnItemClickListener)无法应用于(com.autohush.www.dah.MainActivityFragment1)

我也尝试getActivity()代替this,但它没有用。我该怎么办?

public class MainActivityFragment1 extends Fragment {

    public MainActivityFragment1() {
    }

    private List<ListItems> LocationList;
    ArrayAdapter<ListItems> adapter;
    ListView list;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    private class MyAdapter extends ArrayAdapter<ListItems> {

        public MyAdapter(Context context, List<ListItems> values) {

            super(context, R.layout.list_item,values);
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            View v = convertView;
            LayoutInflater inflater = LayoutInflater.from(getContext());

            if( v == null){
                v = inflater.inflate(R.layout.list_item, parent, false);
            }

            ListItems currLoc = LocationList.get(position);

            TextView theTextView = (TextView) v.findViewById(R.id.list_item_textview);
            theTextView.setText(currLoc.getLocation());

            ImageView theImageView = (ImageView) v.findViewById(R.id.imageview1);
            theImageView.setImageResource(R.drawable.bullet);

            return v;
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.list_view, container, false);

        final EditText txtinput;

        LocationList = new ArrayList<ListItems>();
        list = (ListView) rootView.findViewById(R.id.list_view);
        adapter = new MyAdapter(getActivity(),LocationList);

        list.setAdapter(adapter);

        txtinput = (EditText) rootView.findViewById(R.id.txtinput);
        ImageButton btnadd = (ImageButton) rootView.findViewById(R.id.imageButton);
        btnadd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String newItem = txtinput.getText().toString();
                if (newItem.equals(""))
                    Toast.makeText(getActivity(), "Enter Location Name", Toast.LENGTH_LONG).show();
                else {
                    LocationList.add(new ListItems(newItem));
                    adapter.notifyDataSetChanged();

                    //Hide Keyboard
                    InputMethodManager inputManager = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                    inputManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
                }
            }
        });
        list.setOnItemClickListener(this);
        setRetainInstance(true);

        return rootView;
    }
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        ListItems item = LocationList.get(position);
        LocationList.remove(item);
        adapter = new MyAdapter(getActivity(),LocationList);
        list.setAdapter(adapter);
    }
}

1 个答案:

答案 0 :(得分:3)

您的错误说setOnItemClickListener无法使用MainActivityFragment1的参数,因为您需要实施AdapterView.OnItemClickListener

MainActivityFragment1 extends Fragment implements AdapterView.OnItemClickListener

或者只是将new AdapterView.OnItemClickListener()设置为ListView。

list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        ListItems item = LocationList.get(position);
        LocationList.remove(item);
        adapter = new MyAdapter(getActivity(),LocationList);
        list.setAdapter(adapter);
    }

});