gpt4 book ai didi

java - setOnItemClickListener 不会被自定义适配器调用

转载 作者:行者123 更新时间:2023-12-01 18:59:43 27 4
gpt4 key购买 nike

所以我试图为我的ListView 设置一个OnItemClickListener。但它根本没有被调用。我已经寻找了一些答案,但没有一个对我有用。 (一切都应该不可聚焦/可点击)

ListView 本身如下所示:

enter image description here

这是我的代码:

fragment 中的我的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>

最佳答案

正如 omer.ersoy 所说,onClick-Method 对我有用。

所以我在适配器中添加了我的代码:

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

关于java - setOnItemClickListener 不会被自定义适配器调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59647636/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com