为什么我的onClick方法没有在这个片段中到达

时间:2017-02-19 09:33:54

标签: java android android-fragments

我正在尝试在Android中的片段中创建列表视图。我已成功使用俱乐部数组填充列表视图,但是当我点击列表时,我的方法OnClick永远不会到达。我希望看到一个带有行号的吐司。

请参阅以下代码:

public interface Api { 
@POST("bus/station") 
Call<Result> check(@Query("dtype") String dtype, @Query("city") String city, @Query("station") String station, @Query("key") String key);
}

4 个答案:

答案 0 :(得分:1)

你的onCreateView()中的

使用OnItemClickListener

设置适配器后

  MyList.setOnItemClickListener(new OnItemClickListener()
   {
      @Override
      public void onItemClick(AdapterView<?> adapter, View v, int position,
            long arg3) 
      {
            String value = (String)adapter.getItemAtPosition(position); 
             Toast.makeText(this,value,Toast.LENGTH_SHORT).show();
             list.remove(position); 
             adapter.notifyDataSetChanged();
            // do what you intend to do on click of listview row
      }
   });

删除项目:

ArrayList<String> list =new ArrayList<String>(Arrays.asList(mClubArray)); //declare globally so that u can access it 
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1, list);

 MyList.setAdapter(adapter);
    MyList.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(getActivity(), "You selected : " + item, Toast.LENGTH_SHORT).show();
            String item = list.get(position);
            list.remove(position);
            adapter.notifyDataSetChanged();

        }
    });

答案 1 :(得分:1)

在设置onClick listenser之后实现Onclick方法:像这样

ArrayAdapter<Object> ad = new ArrayAdapter<Object>(this,
                android.R.layout.simple_list_item_checked, items);
        setListAdapter(ad);
        ListView list = getListView();
        list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        //list.setItemChecked(0, true);
        list.setOnItemClickListener(this);

答案 2 :(得分:1)

试试这个:

MyList.setOnItemClickListener(new OnItemClickListener()
   {
      @Override
      public void onItemClick(AdapterView<?> adapter, View v, int position,
            long arg3) 
      {
            String value = (String)adapter.getItemAtPosition(position); 
             Toast.makeText(this,value,Toast.LENGTH_SHORT).show();

      }
   });

答案 3 :(得分:1)

<强>问题

  

单击我的列表时从未到达OnClick。

OnClickListenerOnItemClickListener分别是callbackView的clickevent AdapterView界面。发生特定事件时调用回调方法。因此,您不会在setOnItemClickListener中实现OnClick(View.OnClickListener interface)

<强>解决方案

删除与View.OnClickListener相关的来源

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

    //...

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

            Toast.makeText(getContext(),
                    "Click ListItem Number " + position, Toast.LENGTH_LONG)
                    .show();


        }
    });

    //...
}

<强>参考

Callback functions in Java