幻灯片菜单片段中的onitemclicklistener

时间:2015-01-22 09:20:33

标签: java android android-fragments android-listview

我在项目中使用了slidemenu。

在我的片段中,我有一个listview,它从db中读取一些数据。

我的片段:

package ir.monocode.azmooneEstekhdami;

import ir.monocode.azmoonEstekhdami.R;

import java.util.ArrayList;

import android.app.Fragment;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

public class QuestionsFragment extends Fragment {


    public QuestionsFragment(){}

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

        View view = inflater.inflate(R.layout.fragment_questions, container, false);


        /******** Take some data in Arraylist ( CustomListViewValuesArr ) ***********/
        // setListData();
        // CustomListViewValuesArr.clear();
        MySQLiteHelper db = new MySQLiteHelper(getActivity());
        ArrayList<CatsModel> CustomListViewValuesArr = db.getAllCelebs();
        Resources res = getResources();
        // now you must initialize your list view

        ListView listview = (ListView) view.findViewById(R.id.listView1);

        //listview.setFastScrollEnabled(true);
        /**************** Create Custom Adapter *********/
        CustomAdapter adapter = new CustomAdapter(getActivity(),CustomListViewValuesArr, res);
        listview.setAdapter(adapter);


        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
            // do things with the clicked item
                Toast.makeText(getActivity(), ""+ position, Toast.LENGTH_LONG).show();
        }
        });

        return view;
    }


}

我的片段布局是:

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

    <ImageView
        android:id="@+id/cimageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/top"
        android:layout_marginRight="10dp"
        android:layout_marginLeft="10dp" />

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dp"
        android:layout_marginLeft="10dp" >
    </ListView>

</LinearLayout>

但是当我想使用onitemclicklistener时,它不起作用。我的问题在哪里?

如您所见,我使用customapater

public void onItemClick(int mPosition) {
        // TODO Auto-generated method stub

    }

在我的主要活动中。

1 个答案:

答案 0 :(得分:0)

您需要为自定义列表项行布局窗口小部件添加这些属性,这些窗口小部件已在您getView()方法中充实。

android:focusable="false"
android:focusableInTouchMode="false"