我的列表视图和我的按钮在我的片段中不可点击

时间:2019-05-18 15:39:21

标签: android

我有一个fragment,其中包含一个ListView和一个buttonfragment将与列表中的项目一起打开,但这些项目均不可单击,也不会button。 我尝试添加
    android:clickable="true"android:focusable="true",但仍然无法正常运行

这是我的xml文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    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"

     >

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
       />


    <ListView
        android:id="@+id/list_fiche"
        android:layout_width="399dp"
        android:layout_height="215dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.219"


        />

    <Button
        android:id="@+id/btnaffiche"

        android:layout_width="227dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="132dp"
        android:background="@drawable/button"
        android:text="Afficher toutes les fiches"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/list_fiche" />


</android.support.constraint.ConstraintLayout>

这是我的课程

public class ListFicheActivity extends Fragment {
    List<FichePatient> fiches , firstfiche;
    android.widget.ListView ListView;
    FichePatient fiche ;
    Button btnaffiche, btnback;
    String ID;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
       return inflater.inflate(R.layout.liste_fiches, container, false);
    }
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Bundle b = this.getArguments();
        if (b != null)
            ID = b.getString("ID");
        ListView = getView().findViewById(R.id.list_fiche);
        btnaffiche= getActivity().findViewById(R.id.btnaffiche);
        getAllFiche();
        ListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(getActivity().getApplicationContext(),BottomNavigationActivity.class);
                intent.putExtra("idFiche",fiches.get(position).getId());
                intent.putExtra("age",fiches.get(position).getAge());
                intent.putExtra("idpat",Integer.toString(fiches.get(position).getIdPatient()));               startActivity(intent);
            }
        });
        btnaffiche.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1= new Intent(getActivity().getApplicationContext(),ListFichePatientActivity.class);
                intent1.putExtra("ID",ID);
                startActivity(intent1);
 } });}
    void getAllFiche() {
        Bundle b = this.getArguments();
        if (b != null)
            ID = b.getString("ID");
        IFiche iFiche= APIClient.getClient().create(IFiche.class);
        iFiche.getAllFiche(Integer.parseInt(ID)).enqueue(new Callback<List<FichePatient>>() {
            @Override
            public void onResponse(Call<List<FichePatient>> call, Response<List<FichePatient>> response) {
                Toast.makeText(getActivity().getApplicationContext(),"Succefully connected to Database",Toast.LENGTH_LONG).show();
                Log.v("!!!!!!!!!!!!!!!!!!!", Integer.toString(response.body().size()));
                fiches=response.body();

                for(int i=fiches.size()-2; i>=0 ;i--)
                {
                    fiches.remove(i);
                }
                Log.v("taille!!!!!", Integer.toString(fiches.size()));
                AdapterFiche userAddapterFiche = new AdapterFiche(getActivity().getApplicationContext(), fiches);
                ListView.setAdapter(userAddapterFiche);
            }
            @Override
            public void onFailure(Call<List<FichePatient>> call, Throwable t) {Toast.makeText(getActivity().getApplicationContext(),"error",Toast.LENGTH_LONG).show();
                Log.v("tag",t.getMessage());
            } }); }}

2 个答案:

答案 0 :(得分:0)

首先,您应该将ListView对象命名为 listView ,而不是 ListView 。尝试使用view.findViewById(R.id.list_fiche)而非getView().findViewById(R.id.list_fiche)获取对您的对象的引用。

此外,您应该使用RecyclerView而不是ListView。

答案 1 :(得分:0)

定义时,所有内容都必须与在xml上声明的ID相关联。
例如,您在Button处定义了xml,而不能仅在java中编写

Button.setOnClickListener

但是您需要定义Button与按钮的ID的相关性。
对于Fragments

Button myButton = view.findViewById(R.id.yourXmlID);

对于Activites

Button myButton = findViewById(R.id.yourXmlID)

您需要定义ListViewButton这样的东西。

ListView listView = view.findViewById(R.id.list_fiche);
Button btnaffiche= view.findViewById(R.id.btnaffiche);

对于点击监听器来说,是这样的。

public class ListFicheActivity extends Fragment {
    View paramView;
    ListView listView;
    Button btnFiche;
    String ID;
    FichePatient fiche;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        paramView = inflater.inflate(R.layout.liste_fiches, container, false);
         listView = paramView.findViewById(R.id.list_fiche);
         btnFiche = paramView.findViewById(R.id.btnaffiche);
        Bundle b = this.getArguments();
        if (b != null)
            ID = b.getString("ID");

        getAllFiche();

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent= new Intent(getActivity(), BottomNavigationActivity.class);
                intent.putExtra("idFiche",fiches.get(position).getId());
                intent.putExtra("age",fiches.get(position).getAge());
                intent.putExtra("idpat",Integer.toString(fiches.get(position).getIdPatient()));
                startActivity(intent);
            }
        });

        btnFiche.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1= new Intent(getActivity().getApplicationContext(),ListFichePatientActivity.class);
                intent1.putExtra("ID",ID);
                startActivity(intent1);
            }
        });
        return paramView;
    }

    public void getAllFiche() {
        Bundle b = this.getArguments();
        if (b != null)
            ID = b.getString("ID");
        IFiche iFiche= APIClient.getClient().create(IFiche.class);
        iFiche.getAllFiche(Integer.parseInt(ID)).enqueue(new Callback<List<FichePatient>>() {
            @Override
            public void onResponse(Call<List<FichePatient>> call, Response<List<FichePatient>> response) {

                Integer.toString(response.body().size()));
                fiches=response.body();

                for(int i=fiches.size()-2; i>=0 ;i--)
                {
                    fiches.remove(i);
                }

                AdapterFiche userAddapterFiche = new AdapterFiche(getActivity().getApplicationContext(), fiches);
                ListView.setAdapter(userAddapterFiche);
            }
            @Override
            public void onFailure(Call<List<FichePatient>> call, Throwable t) {Toast.makeText(getActivity().getApplicationContext(),"error",Toast.LENGTH_LONG).show();
                Log.v("tag",t.getMessage());
            } });
    }
}