onListItemClick不起作用?

时间:2016-12-30 12:25:22

标签: android listview android-arrayadapter onitemclicklistener

我正在尝试使用自定义适配器设置OnItemClickListener。当我按下时,onItemClick没有触发。

我发现我需要添加一些属性,但仍然无效。

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

的活动:

public class StudentActivity {

    private Activity mActivity;
    private Student mStudent;

    private TextView mName;
    private Button mMonday;
    private ListView mListView;

    //That will be deleted
    private ArrayList<HashMap<String, String>> list;

    public StudentActivity(Activity activity, Student student) {
        mActivity = activity;
        mStudent = student;

        mName = (TextView) mActivity.findViewById(R.id.name_student);
        mName.setText(mStudent.getFirstName() + " " + mStudent.getLastName());

        mListView = (ListView) mActivity.findViewById(R.id.list_view);

        list = new ArrayList<>();

        int numberOfIntervals = 7;

        List<String> hours = new ArrayList<>();
        hours.add("08:00-10:00");
        hours.add("10:00-12:00");
        hours.add("12:00-14:00");
        hours.add("14:00-16:00");
        hours.add("16:00-18:00");
        hours.add("18:00-20:00");
        hours.add("20:00-22:00");

        for (int i = 0; i < numberOfIntervals; i++) {
            HashMap<String, String> temp = new HashMap<>();
            temp.put("First", hours.get(i));
            temp.put("Second", "");
            list.add(temp);
        }

        ListViewAdapter adapter = new ListViewAdapter(mActivity, list);
        mListView.setAdapter(adapter);

        addListenerForMondayButton(adapter);
        addListenerForListViewItem(mListView);
    }

    private void addListenerForMondayButton(final ListViewAdapter adapter) {
        mMonday = (Button) mActivity.findViewById(R.id.name_monday);

        mMonday.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ReservationTask reservationTask = new ReservationTask();
                reservationTask.populateList(adapter);
            }
        });
    }

    private void addListenerForListViewItem(ListView view) {
        view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                addNotification();
            }
        });
    }

    private void addNotification() {
        NotificationCompat.Builder builder =
                new NotificationCompat.Builder(mActivity.getApplicationContext())
                        .setSmallIcon(R.drawable.arrow_back)
                        .setContentTitle("Notification")
                        .setContentText("This is a test notification");

        Intent notificationIntent = new Intent(mActivity.getApplicationContext(), MainActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(mActivity.getApplicationContext(), 0,
                notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(contentIntent);

        NotificationManager manager = (NotificationManager) mActivity.getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(0, builder.build());
    }

适配器:

public class ListViewAdapter extends BaseAdapter {

    private ArrayList<HashMap<String, String>> mList;
    private List<Reservation> mReservationList;
    Activity mActivity;
    TextView mHour;
    TextView mName;

    public ListViewAdapter(Activity activity, ArrayList<HashMap<String, String>> list) {
        super();
        mActivity = activity;
        mList = list;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = mActivity.getLayoutInflater();

        convertView=inflater.inflate(R.layout.list_view_item, null);

        mHour = (TextView) convertView.findViewById(R.id.list_item_hour);
        mName = (TextView) convertView.findViewById(R.id.list_item_name);

        HashMap<String, String> map = mList.get(position);

        mHour.setText(map.get("First"));
        boolean set = false;
        if (mReservationList != null && mReservationList.size() > 0) {
            for (Reservation reservation: mReservationList) {
                String createInterval = reservation.getStartHour() + "-" + reservation.getEndHour();
                if (createInterval.equals(parseTime(mHour.getText().toString()))) {
                    mName.setText("Dima");
                    set = true;
                }
            }
            if (set == false) {
                mName.setText(map.get("Second"));
            }
        } else {
            mName.setText(map.get("Second"));
        }

        return convertView;
    }

    public void populateNameReservation(List<Reservation> reservations) {
        mReservationList = reservations;
        notifyDataSetChanged();
    }

    private String parseTime(String intervalTime) {
        String startHour = intervalTime.substring(0, intervalTime.indexOf("-"));
        String endHour = intervalTime.substring(intervalTime.indexOf("-") + 1, intervalTime.length());
        Time startHourTime = Time.valueOf(startHour + ":00");
        Time endHourTime = Time.valueOf(endHour + ":00");

        return startHourTime + "-" + endHourTime;
    }

列表视图的XML布局:

<?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">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="vertical">
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:gravity="center"
                    android:text="Text View"
                    android:id="@+id/list_item_hour"
                    android:focusableInTouchMode="false"
                    android:clickable="false"
                    android:focusable="false"/>
            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1">
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:gravity="center"
                    android:text="Text View"
                    android:id="@+id/list_item_name"
                    android:focusableInTouchMode="false"
                    android:clickable="false"
                    android:focusable="false"/>
            </LinearLayout>
        </LinearLayout>
    </ScrollView>
</LinearLayout>

谢谢!

1 个答案:

答案 0 :(得分:0)

尝试这个示例代码我使用了一个列表视图,其中包含一个自定义类,用于在列表视图中对数据进行膨胀,然后使用一个单击列表项重定向到另一个使用intent的活动。

检查一下它是否正常工作。

{{1}}

希望这可以帮助你。