setOnItemClickListener не вызывается с пользовательским адаптером - PullRequest
0 голосов
/ 08 января 2020

Поэтому я пытаюсь установить OnItemClickListener для моего ListView. Но это не называется вообще. Я уже искал некоторые ответы, но ни один из них не помог мне. (Все должно быть не фокусируемым / кликабельным)

Сам ListView выглядит так:

enter image description here

Вот мой код:

Мой ListView во фрагменте:

package com.example.efahrtenbuchapp.ui.table;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProviders;

import com.example.efahrtenbuchapp.R;
import com.example.efahrtenbuchapp.eFahrtenbuch.Fahrt;
import com.example.efahrtenbuchapp.eFahrtenbuch.FahrtListAdapter;
import com.example.efahrtenbuchapp.eFahrtenbuch.FahrtListenAdapter;
import com.example.efahrtenbuchapp.http.json.JSONConverter;
import com.example.efahrtenbuchapp.http.HttpRequester;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class TableFragment extends Fragment {

    private TableViewModel tableViewModel;
    private List<FahrtListAdapter> list;
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        tableViewModel = ViewModelProviders.of(this).get(TableViewModel.class);

        View root = inflater.inflate(R.layout.fragment_table, container, false);

        getActivity().setContentView(R.layout.fragment_table);
        ListView lv = root.findViewById(R.id.listviewidfrag);



        HttpRequester.simpleJsonArrayRequest(getActivity(), "http://10.0.2.2:8080/loadFahrtenListe?kennzeichen=B OB 385", jsonResponse -> {
            Log.d("onCreate: ", jsonResponse.toString());
            List<FahrtListAdapter> list = JSONConverter.toJSONList(jsonResponse).stream()
                    .map(json -> (Fahrt) JSONConverter.createFahrtFromJSON(json))
                    .peek(fahrt -> Log.d("TABLEFRAGMENT", fahrt.toString()))
                    .map(fahrt -> new FahrtListAdapter(fahrt))
                    .collect(Collectors.toList());
            refreshList(lv, list);
        }, null);

        FahrtListenAdapter adapter = new FahrtListenAdapter(getActivity(), R.layout.fahrt_list_adapter, new ArrayList());
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getActivity(), "DDD", Toast.LENGTH_LONG);
                System.out.println("dddddd");
                Log.d("KLICKED ON: ", "Item: " + list.get(position).toString());
            }
        });
        return root;
    }

    public void message(String msg) {
        Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
    }

    public void refreshList(View root, List<FahrtListAdapter> list){
        this.list = list;
        ListView lv = getActivity().findViewById(R.id.listviewidfrag);          //R.layout.fahrt_list_adapter
        ArrayAdapter adapter = new FahrtListenAdapter(getActivity(), R.layout.fahrt_list_adapter, list);
        lv.setAdapter(adapter);
    }
}

Адаптер:

package com.example.efahrtenbuchapp.eFahrtenbuch;  
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.example.efahrtenbuchapp.R;

import java.util.List;

public class FahrtListenAdapter extends ArrayAdapter<FahrtListAdapter> {
    private final int resource;
    private Context context;

    public FahrtListenAdapter(@NonNull Context context, int resource, List<FahrtListAdapter> 
    objects) {
        super(context, resource, objects);
        this.context = context;
        this.resource = resource;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        String km = getItem(position).getKm();
        String ziel = getItem(position).getZiel();
        String datum = getItem(position).getDatum();
        FahrtListAdapter fla = new FahrtListAdapter(datum, ziel, km);

        LayoutInflater inflater = LayoutInflater.from(context);
        convertView = inflater.inflate(R.layout.fahrt_list_adapter, parent, false);
        Log.d("", "getView: focusable = " + convertView.hasFocusable());
        Log.d("", "getView: clickable = " + convertView.isClickable());
        Log.d("", "getView: clickable = " + convertView.isFocusableInTouchMode());
        TextView tv1 = convertView.findViewById(R.id.tv1);
        TextView tv2 = convertView.findViewById(R.id.tv2);
        TextView tv3 = convertView.findViewById(R.id.tv3);
        tv1.setText(ziel);
        tv2.setText(datum);
        tv3.setText(km);
        return convertView;
    }
}

Макет адаптера:

<?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"
    android:layout_marginTop="2dp"
    android:background="#80FFFFFF"
    android:orientation="horizontal"
    android:weightSum="100"
    android:descendantFocusability = "blocksDescendants"
    android:focusable="false"
    android:focusableInTouchMode="false">

    <TextView
        android:id="@+id/tv1"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_weight="66.6"
        android:gravity="center"
        android:text="textview1"
        android:textAlignment="center"
        android:focusable="false"
        android:descendantFocusability = "blocksDescendants"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="33.3"
        android:orientation="vertical"
        android:focusable="false"
        android:descendantFocusability = "blocksDescendants">

        <TextView
            android:id="@+id/tv2"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:gravity="center"
            android:text="textview2"
            android:focusable="false"
            android:descendantFocusability = "blocksDescendants" />

        <TextView
            android:id="@+id/tv3"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:gravity="center"
            android:text="textview3"
            android:focusable="false"
            android:descendantFocusability = "blocksDescendants" />

    </LinearLayout>

</LinearLayout>

the layout of the fragment:

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/background"
        android:descendantFocusability="blocksDescendants"
        android:focusable="false">
            <ListView
                android:id="@+id/listviewidfrag"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:descendantFocusability = "blocksDescendants"
                android:focusable="false"/>
    </androidx.constraintlayout.widget.ConstraintLayout>

1 Ответ

0 голосов
/ 08 января 2020

Как заявил omer.ersoy, метод onClick работал для меня.

Поэтому я добавил в свой код в адаптере:

convertView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Log.i("ADAPTER", "onClick: ELEMENT= " + datum);
    }
});
...