GridView onClick()事件,使用额外字段打开活动

时间:2018-05-17 15:55:16

标签: android listview gridview android-adapter

我基本上从rest api中检索一些记录并在网格视图中显示它们。一切正常,直到我从ListView更改为GridView。现在,onClickItemListener无法正常工作,我似乎也无法从自定义布局中获取ID字段。

MainActivity.java

        coursesGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent signUp = new Intent(MainActivity.this, Signup.class);
                signUp.putExtra("course_id", "<<NEED TEXTVIEW courseId TEXT HERE>>");
                startActivity(signUp);
                Log.e("CLICKED", "YES");
            }

        });

courses_row_layout.xml

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

    <TableLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <ImageView
                android:id="@+id/imageView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:srcCompat="@mipmap/ic_university" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TextView
                    android:id="@+id/courseName"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="Name"
                    android:textAppearance="@style/TextAppearance.AppCompat.Large" />
        </TableRow>

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="gone">

            <TextView
                android:id="@+id/courseStartDate"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="Start date"
                android:textAppearance="@style/TextAppearance.AppCompat.Large" />
        </TableRow>

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="gone">

            <TextView
                android:id="@+id/courseEndDate"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="End date"
                android:textAppearance="@style/TextAppearance.AppCompat.Large" />
        </TableRow>

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/courseId"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="ID"
                android:textAppearance="@style/TextAppearance.AppCompat.Large" />
        </TableRow>
    </TableLayout>

</RelativeLayout>

CoursesAdapter.java

package uk.co.grandcentralgroup.gcg;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import static uk.co.grandcentralgroup.gcg.R.id.imageView;

public class CoursesAdapter extends ArrayAdapter {
    List courses = new ArrayList();

    public CoursesAdapter(@NonNull Context context, int resource) {
        super(context, resource);
    }

    @Override
    public void add(Object object) {
        courses.add(object);
    }

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

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

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View row;
        row = convertView;
        CourseHolder courseHolder;

        if(row == null) {
            LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = layoutInflater.inflate(R.layout.courses_row_layout, parent, false);
            courseHolder = new CourseHolder();
            courseHolder.courseId = row.findViewById(R.id.courseId);
            courseHolder.courseName = row.findViewById(R.id.courseName);
            courseHolder.courseStartDate = row.findViewById(R.id.courseStartDate);
            courseHolder.courseEndDate = row.findViewById(R.id.courseEndDate);
            row.setTag(courseHolder);
        } else {
            courseHolder = (CourseHolder) row.getTag();
        }

        Courses courses = (Courses) this.getItem(position);
        courseHolder.courseId.setText(String.valueOf(courses.getId()));
        courseHolder.courseName.setText(courses.getName());
        courseHolder.courseName.setLines(3);
        courseHolder.courseStartDate.setText(courses.getStartDate());
        courseHolder.courseEndDate.setText(courses.getEndDate());

        return row;
    }

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

    static class CourseHolder {
        TextView courseId, courseName, courseStartDate, courseEndDate;
    }
}

0 个答案:

没有答案