为什么我不能从Asynctask类的gridview中设置setOnItemClickListener?

时间:2018-11-21 03:12:24

标签: android gridview android-asynctask android-toast

我想查看烤面包,但是我的setOnItemClickListener无法正常工作。

这是我关于asynctask的课程

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();
            }
        });

    }
}

这是我的课。我想在网格中查看吐司。但我不知道怎么流行吐司。我已经完成了网格。但我看不到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" />

2 个答案:

答案 0 :(得分:0)

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

ListView中,每当单击某个项目视图时,它将在该视图上执行View.hasExplicitFocusable,因此,如果您的项目视图中有任何可单击的视图,则您将不会在其中收到回调onItemClick

答案 1 :(得分:0)

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();
    }
});