【问题标题】:How to fix EditText part from ListView inside fragment?如何修复片段内 ListView 中的 EditText 部分?
【发布时间】:2021-12-03 10:30:47
【问题描述】:

当我从 ListView 中单击它时,想要使用 EditText 进行搜索。我用一些 youtube 视频或博客做到了。把这些混在一起,当然,有错误。应该解决什么问题?我把我的每一个代码都放在了理解它的地方。非常需要你们的帮助。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
    tools:context=".Playbook">

    <ListView
        android:id="@+id/listView2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:padding="16dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@drawable/ic_baseline_search_24"
        android:id="@+id/searchImage" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_toRightOf="@id/searchImage"
        android:id="@+id/editTextFilter"/>

    <TextView
        android:id="@+id/termName"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/editTextFilter"
        style="@style/TextAppearance.AppCompat.Title"
        android:text="Term"/>

</RelativeLayout>

这是布局部分。

package com.example.gridiron;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;

import java.util.ArrayList;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link Playbook#newInstance} factory method to
 * create an instance of this fragment.
 */
public class Playbook extends Fragment {

    ArrayList<PlaybookList> arrayList2;
    ListView listView2;
    private static PlaybookListAdapter playbookListAdapter;

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public Playbook() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment Playbook.
     */
    // TODO: Rename and change types and number of parameters
    public static Playbook newInstance(String param1, String param2) {
        Playbook fragment = new Playbook();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_playbook, container, false);
        ListView listView2 = (ListView) view.findViewById(R.id.listView2);
        EditText editTextFilter = (EditText) view.findViewById(R.id.editTextFilter);

        ArrayList<PlaybookList> arrayList2 = new ArrayList<>();
        arrayList2.add(new PlaybookList("Quarterback", "https://namu.wiki/w/%EC%BF%BC%ED%84%B0%EB%B0%B1"));
        arrayList2.add(new PlaybookList("Runningback", "https://namu.wiki/w/%EB%9F%AC%EB%8B%9D%EB%B0%B1"));

        PlaybookListAdapter playbookListAdapter = new PlaybookListAdapter(getActivity(), R.layout.list_row2, arrayList2);

        listView2.setAdapter(playbookListAdapter);

        editTextFilter.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable edit) {
                String filterText = edit.toString();
                if (filterText.length() > 0) {
                    listView2.setFilterText(filterText);
                } else {
                    listView2.clearTextFilter();
                }
            }
        });

        listView2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(String.valueOf(arrayList2.get(position).getTermURL())));
                getActivity().startActivity(intent);
            }
        });

        return view;
    }
}
package com.example.gridiron;

public class PlaybookList {
    String TermName;
    String TermURL;

    public PlaybookList(String termName, String termURL) {
        TermName = termName;
        TermURL = termURL;
    }

    public String getTermName() {
        return TermName;
    }

    public void setTermName(String termName) {
        TermName = termName;
    }

    public String getTermURL() {
        return TermURL;
    }

    public void setTermURL(String termURL) {
        TermURL = termURL;
    }
}
package com.example.gridiron;

import android.content.Context;
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 androidx.annotation.Nullable;

import java.util.ArrayList;

public class PlaybookListAdapter extends ArrayAdapter<PlaybookList> {

    private Context mContext;
    private int mResource;

    public PlaybookListAdapter(@NonNull Context context, int resource, @NonNull ArrayList<PlaybookList> objects) {
        super(context, resource, objects);
        this.mContext = context;
        this.mResource = resource;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

        LayoutInflater layoutInflater = LayoutInflater.from(mContext);

        convertView = layoutInflater.inflate(mResource, parent, false);

        TextView termName = convertView.findViewById(R.id.termName);
        termName.setText(getItem(position).getTermName());

        return convertView;
    }
}

最后 3 个代码用于课程。真的很难做到。请帮帮我!

【问题讨论】:

标签: java android listview android-fragments android-edittext


【解决方案1】:

您的布局代码中没有ListView。您没有包含错误,但我很确定它是 NullPointerException 在这一行:

listView2.setAdapter(playbookListAdapter);

【讨论】:

  • 哦,错误是“尝试在空对象引用上调用虚拟方法 void android.widget.EditText.addTextChangedListener(android.text.TextWatcher)”editTextFilter.addTextChangedListener(new TextWatcher() {
  • 您确定R.layout.fragment_playbook 是您问题中包含的那个人吗?如果 EditText 为空,则表示膨胀视图不包含任何 ID 为 editTextFilter 的视图,但它明确定义在您包含的视图中,因此那里一定有问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-13
  • 1970-01-01
  • 1970-01-01
  • 2022-07-04
  • 2020-02-25
相关资源
最近更新 更多