ListView中的onListItemClick不起作用

时间:2013-08-13 13:41:28

标签: android listview

我在点击列表项时尝试调用另一个Fragment但是它不起作用,我正在使用滑块菜单,ListView项是滑块菜单中的选项。我做错了什么?

public class MenuNetimoveis_Fragments extends ListFragment {
private ListView listView;

String[] itensMenu = new String[] {
        " Imóveis no mapa",
        "Favoritos",
        "Agências",
        "Dicas para Alugar",
        "Alugar meu imóvel",
        "Sobre"
    };

    int[] imagens = new int[]{
        R.drawable.map_marker,
        R.drawable.star,
        R.drawable.house,
        R.drawable.coffee,
        R.drawable.pricetag,
        R.drawable.pricetag
    };

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


        return inflater.inflate(R.layout.list, null);
}

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

     ImageView tv = new ImageView(getActivity());
     Drawable dw = getResources().getDrawable(R.drawable.menu_netimoveis_locacao);
     tv.setImageDrawable(dw);
     getListView().addHeaderView(tv);

    crateListView();

}

private void crateListView(){

     List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();

        for(int i=0;i<6;i++){
            HashMap<String, String> hm = new HashMap<String,String>();
            hm.put("txt", itensMenu[i]);
            hm.put("imagens", Integer.toString(imagens[i]) );
            aList.add(hm);
        }

        String[] from = { "txt","imagens" };
        int[] to = { R.id.text,R.id.imagemview};
        SimpleAdapter adapter = new SimpleAdapter(getActivity().getBaseContext(), aList, R.layout.list_row, from, to);

        setListAdapter(adapter);
}

@Override
public void onListItemClick(ListView lv, View v, int position, long id) {
    Fragment newContent = null;
    switch (position) {
    case 0:
        newContent = new Menu_DicasParaAlugar();
        break;
    case 1:
        break;

    }
    if (newContent != null)
        switchFragment(newContent);
}



// the meat of switching the above fragment
private void switchFragment(Fragment fragment) {
    if (getActivity() == null)
        return;

    if (getActivity() instanceof FragmentChangeActivity) {
        FragmentChangeActivity fca = (FragmentChangeActivity) getActivity();
        fca.switchContent(fragment);

    }
}
}

1 个答案:

答案 0 :(得分:1)

在onCreate()

结束时执行getListView().setOnListItemClickListener(this);
相关问题