1

我在带有自定义单元格适配器的片段中有一个列表,

问题是 onItemSelected 没有响应,

请问如何解决这个问题?

public class PhoneMenuList extends SherlockFragment implements OnItemSelectedListener {

Fragment newContent = null;
ListView productList;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ 
    View mView = inflater.inflate(R.layout.list, container, false);
    return mView; 
}

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

    //SET THE LIST ADAPTER!
    Hamburger hambu_data[] = new Hamburger[] {
                new Hamburger(R.drawable.icon_hambu_folder, "My Documents"),
                new Hamburger(R.drawable.icon_hambu_favs, "Top 10 viewed"),
                new Hamburger(R.drawable.icon_hambu_validate, "Validate Document"),
                new Hamburger(R.drawable.icon_hambu_how, "How to use"),
                new Hamburger(R.drawable.icon_hambu_about, "About")
            };              

      productList= (ListView) getActivity().findViewById(R.id.listView1);

      HamburgerAdapter adapter = new HamburgerAdapter(getActivity(), R.layout.hamburger_item_row, hambu_data);          

        productList= (ListView) getActivity().findViewById(R.id.listView1);  
        View header = (View)getLayoutInflater(savedInstanceState).inflate(R.layout.hamburger_item_row, null);
        productList.addHeaderView(header);
        productList.setAdapter(adapter);

        //listener          
        productList.setOnItemSelectedListener(this);
}

 //@Override
 public void onListItemClick(ListView l, View v, int position, long id) {
    Log.d("mensa", "chapuzea");
 }

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
        long arg3) {
    // TODO Auto-generated method stub
    Log.d("mensa", "abacus");
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub
    Log.d("mensa", "semper");
}

}
4

3 回答 3

1

你为什么要初始化 listview 两次?

productList= (ListView) getActivity().findViewById(R.id.listView1);

并使用setOnItemClick()而不是setOnItemSelected()

消除productList.setOnItemSelectedListener(this);

利用 productList.setOnItemClickListener(this);

于 2013-09-12T07:31:58.793 回答
0

您可以覆盖 SherlockListFragment 的 onListItemClick 方法。

试试这个代码。

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
   super.onListItemClick(l, v, position, id);
   System.out.println("onListItemClick");
}

或代替使用OnItemSelectedListeneruse OnItemClickedListener

于 2013-09-12T07:32:42.703 回答
0

您必须为 ListView 设置选择模式。设置选择模式

于 2013-09-12T07:29:14.687 回答