为什么我不能在 Asynctask Class 中从 gridview 设置 OnItemClickListener?

Why I can't setOnItemClickListener from gridview in Asynctask Class?

我想查看 toast,但是我的 setOnItemClickListener 不工作。

这是我的 class 异步任务

private GridView listView;

class jadwall extends AsyncTask<String, String, String>
{
    private String val3;


    public jadwall(String a) {
        this.val3 = a;
        // Do something ...
    }
    @Override
    protected String doInBackground(String... strings) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        CallSoap cs = new CallSoap();
        data = cs.JadwalHarian(val3);

        return data;
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        //Toast.makeText(JadwalDokter.this,s.toString(),Toast.LENGTH_SHORT).show();
        ArrayList<String> jadwalList = new ArrayList<String>();
        try {
            String data1[] = s.split("%");
            for (int i = 0; i < data1.length-1; i++) {
                String data2[] = data1[i].split("#");
                jadwalList.add(data2[1].substring(1));
            }
        }catch (Exception e){
            Toast.makeText(JadwalDokter.this,e.toString(),Toast.LENGTH_SHORT).show();
        }
        listView.setAdapter(new JadwalDokterAdapter(JadwalDokter.this, jadwalList));
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(JadwalDokter.this,((TextView) view.findViewById(R.id.name)).getText(),Toast.LENGTH_SHORT).show();
            }
        });

    }
}

这是我的class。我想在网格中查看吐司。但我不知道吐司是热的。我完成了网格。但我没有看到 setOnItemClickListener 正在工作

这是我的适配器中的 getview()

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

    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View gridView;

    if (convertView == null) {

        gridView = new View(context);

        // get layout from mobile.xml
        gridView = inflater.inflate(R.layout.user_row_jadwal, null);

        // set value into textview
        TextView textView = (TextView) gridView
                .findViewById(R.id.name);
        TextView textViewphone = (TextView) gridView
                .findViewById(R.id.phone);

        textView.setText(mobileValues.get(position));
    } else {
        gridView = (View) convertView;
    }

    return gridView;
}

这是我的 XML 我的看法

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="info.androidhive.recyclerviewsearch.JadwalDokter"
tools:showIn="@layout/activity_jadwal_dokter">

<CalendarView
    android:id="@+id/calendarView"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true" />

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:layout_below="@+id/calendarView"
   >

    <GridView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="49dp"
        android:scrollbars="vertical"
        android:focusable="false"
        android:focusableInTouchMode="false"/>
</RelativeLayout>

这是我的 user_row_jadwal

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:paddingBottom="25dp"
android:paddingLeft="25dp"
android:paddingRight="25dp"
android:paddingTop="25dp">

<TextView
    android:id="@+id/name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/thumbnail"
    android:fontFamily="sans-serif-medium"
    android:textColor="@color/contact_name"
    android:textSize="@dimen/contact_name" />

<TextView
    android:id="@+id/phone"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/name"
    android:layout_toRightOf="@id/thumbnail"
    android:textColor="@color/contact_number"
    android:textSize="@dimen/contact_number" />

只需从 user_row_jadwal.xml 中的 RelativeLayout 中删除 android:clickable="true"

ListView中,每当点击项目视图时,它都会对该视图执行View.hasExplicitFocusable,因此如果您的项目视图上有任何可点击的视图,那么您将不会收到onItemClick.

回调

getView 方法中的 return gridView 之前添加一个点击侦听器。

gridView.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View view){
       Toast.makeText(JadwalDokter.this,((TextView) view.findViewById(R.id.name)).getText(),Toast.LENGTH_SHORT).show();
    }
});