Почему я не могу установить OnItemClickListener из gridview в Asynctask Class? - PullRequest
0 голосов
/ 21 ноября 2018

Я хочу посмотреть тост, но мой 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 голосов
/ 21 ноября 2018

Добавьте прослушиватель кликов в вашем методе 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();
    }
});
0 голосов
/ 21 ноября 2018

Просто удалите android:clickable="true" из RelativeLayout в вашем user_row_jadwal.xml.

В ListView при каждом нажатии на элемент просмотра будет выполняться View.hasExplicitFocusable в представлении, поэтому, если в представлении элемента есть какое-либо интерактивное представление, вы не получите обратный вызов в onItemClick.

...