OnItemClick未在List Fragment中调用

时间:2013-07-16 17:02:19

标签: java android listview android-fragments onitemclicklistener

当我单击List的一个项目时,方法OnItemClick不会被调用。我试图打电话给另一个活动,以显示有关“AnaliseEstrutural”的更多信息。列表中的内容是Brunton或Clar为ehBrunton定义的值。然后,当用户点击某个项目时,它将显示有关该“AnaliseEstrutural”的其他信息。但它没有通过这种方法。

这是代码:

public class AnalisesPonto extends ListFragment implements OnItemClickListener{

private ArrayList<AnaliseEstrutural> analises;
AnalisesDAO andao;
String idPonto;
private View view;
private ListView analisesList;
private AnalisesAdapter analisesAdapter;
private boolean ehBrunton;
private Intent i;

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

    idPonto = getArguments().getString("id");

    andao = new AnalisesDAO(getActivity().getApplicationContext());
    analises = andao.relatorioAnalises(idPonto);
    ehBrunton = true;
    i = new Intent(getActivity().getApplicationContext(), DadosAnalises.class);
}

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


    view = inflater.inflate(R.layout.analises_ponto_layout, null);

    analisesList = (ListView) view.findViewById(android.R.id.list);

    analisesAdapter = new AnalisesAdapter(getActivity().getApplicationContext(), true);

    for (int i = 0; i < analises.size(); i++) {
        analisesAdapter.add(analises.get(i));
    }

    analisesList.setAdapter(analisesAdapter);

    ViewGroup parent = (ViewGroup) analisesList.getParent();
    parent.removeView(analisesList);
    analisesList.setOnItemClickListener(new OnItemClickListener()
    {
        @Override public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
        {           
            Bundle extra = new Bundle();
            extra.putBoolean("radio", ehBrunton);
            extra.putString("tipo_est", analises.get(position).getTipo());
            extra.putString("codigo", analises.get(position).getCodigo());
            if(ehBrunton) {
                extra.putString("brun_clar", analises.get(position).getBrunton());
            } else {
                extra.putString("brun_clar", analises.get(position).getClar());
            }
            extra.putString("azimute", analises.get(position).getAzimute());
            extra.putString("direcao", analises.get(position).getDirecao());
            extra.putString("quadrante", analises.get(position).getQuadrante());
            extra.putString("sentido", analises.get(position).getSentido());
            extra.putString("descricao",analises.get(position).getDescricao());
            extra.putString("mergulho", analises.get(position).getMergulho());
            extra.putString("familia", analises.get(position).getFamilia());
            i.putExtra("analise", extra);
            startActivity(i);
        }
    });  




    return analisesList;
}
 }

列表中的布局:

    <?xml version="1.0" encoding="utf-8"?>
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:orientation="vertical" >

          <ListView
             android:id="@android:id/list"
             android:layout_width="fill_parent"
             android:layout_height="match_parent" >
         </ListView>
    </LinearLayout>

这是列表元素的布局

  <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

       <TextView
          android:id="@+id/med_angular"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" />
    </LinearLayout>

2 个答案:

答案 0 :(得分:4)

[已解决] 将方法analisesList.setOnItemClickListener(new OnItemClickListener() 置于 onActivityCreated内而不是onCrateView

 @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        analisesList.setOnItemClickListener(new OnItemClickListener()
        {
            @Override public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
            {           
                Bundle extra = new Bundle();
                extra.putBoolean("radio", ehBrunton);
                extra.putString("tipo_est", analises.get(position).getTipo());
                extra.putString("codigo", analises.get(position).getCodigo());
                if(ehBrunton) {
                    extra.putString("brun_clar", analises.get(position).getBrunton());
                } else {
                    extra.putString("brun_clar", analises.get(position).getClar());
                }
                extra.putString("azimute", analises.get(position).getAzimute());

                extra.putString("direcao", analises.get(position).getDirecao());
                extra.putString("quadrante", analises.get(position).getQuadrante());
                extra.putString("sentido", analises.get(position).getSentido());
                extra.putString("descricao",analises.get(position).getDescricao());
                extra.putString("mergulho", analises.get(position).getMergulho());
                extra.putString("familia", analises.get(position).getFamilia());
                i.putExtra("analise", extra);
                startActivity(i);
            }
        });
    }

答案 1 :(得分:0)

这是什么removeView for?

parent.removeView(analisesList);

你也不应该在onCreateView而不是analisesList中返回整个视图吗?

我想以下填写修复问题

  1. 删除“parent.removeView(analisesList);”
  2. return analisesList;
  3. 删除implements onOtemClickListener(你正在创建一个内部匿名类,如Raghunandan所提到的)