我正在尝试从自定义适配器获取搜索结果,但未显示任何结果。列表视图正常工作,但是当我在搜索视图中搜索或键入任何单词时,没有任何反应。有人可以帮我做这些事。
Mainactivity.java
package com.studiored.muslimbabynames;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
ImageView boy, girl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
girl = findViewById(R.id.girlimage);
boy = findViewById(R.id.boyimage);
girl.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, GirlName.class);
startActivity(i);
}
});
boy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, BoyName.class);
startActivity(i);
}
});
}
}
BoyName.java
package com.studiored.muslimbabynames;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SearchView;
import java.util.ArrayList;
public class BoyName extends AppCompatActivity {
ListView List_itemboy;
NameAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_boy_name);
ArrayList<NameListClass> nameList = new ArrayList<NameListClass>();
nameList.add(new NameListClass("Rayyan", "Door of a Paradise"));
nameList.add(new NameListClass("Rafi", "Best Friend"));
nameList.add(new NameListClass("Sadiq", "Sadiq"));
nameList.add(new NameListClass("Rayyan", "Door of a Paradise"));
nameList.add(new NameListClass("Rayyan", "Door of a Paradise"));
nameList.add(new NameListClass("Rayyan", "Door of a Paradise"));
nameList.add(new NameListClass("Rayyan", "Door of a Paradise"));
nameList.add(new NameListClass("Rayyan", "Door of a Paradise"));
nameList.add(new NameListClass("Rayyan", "Door of a Paradise"));
nameList.add(new NameListClass("Rayyan", "Door of a Paradise"));
adapter = new NameAdapter(this, nameList);
List_itemboy = findViewById(R.id.list_itemboy);
List_itemboy.setAdapter(adapter);
List_itemboy.setTextFilterEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater =getMenuInflater();
inflater.inflate(R.menu.search_view,menu);
MenuItem item = menu.findItem(R.id.search_name);
SearchView searchView = (SearchView)item.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
adapter.getFilter().filter(newText);
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
}
这是我的自定义适配器文件 NameAdapter.java
package com.studiored.muslimbabynames;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import org.w3c.dom.Text;
import java.util.ArrayList;
public class NameAdapter extends ArrayAdapter<NameListClass> {
public NameAdapter(@NonNull Context context, ArrayList<NameListClass> nameList) {
super(context, 0, nameList);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
NameListClass name = getItem(position);
if (convertView == null){
convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item_view,parent,false);
}
TextView Listname = convertView.findViewById(R.id.textView);
Listname.setText(name.Name);
TextView MeanigName = convertView.findViewById(R.id.textView2);
MeanigName.setText(name.Meaning);
return convertView;
}
}
这是自定义类 NameListClass.java
package com.studiored.muslimbabynames;
import java.security.PublicKey;
public class NameListClass {
public String Name;
public String Meaning;
public NameListClass(String babyname, String babynamemeaning){
Name = babyname;
Meaning = babynamemeaning;
}
}
xml file of Boy name activity
BoyName.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:layout_height="match_parent"
tools:context=".BoyName">
<LinearLayout
android:layout_width="match_parent"
android:layout_marginTop="25dp"
android:layout_marginBottom="10dp"
android:gravity="center"
android:layout_height="50dp">
<TextView
android:id="@+id/textView"
android:layout_marginLeft="10dp"
android:textSize="25sp"
android:layout_width="wrap_content"
android:textStyle="bold"
android:fontFamily="serif-monospace"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Name" />
<TextView
android:fontFamily="serif-monospace"
android:id="@+id/textView2"
android:textStyle="bold"
android:textSize="25sp"
android:layout_marginRight="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:text="Meaning" />
</LinearLayout>
<ListView
android:layout_width="match_parent"
android:id="@+id/list_itemboy"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_height="wrap_content"/>
</LinearLayout>
list_view_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_marginTop="10dp"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/textView"
android:layout_marginLeft="10dp"
android:textSize="25sp"
android:layout_width="0dp"
android:fontFamily="serif-monospace"
android:layout_marginBottom="20dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:paddingRight="5dp"
android:text="Name"
/>
<TextView
android:fontFamily="serif-monospace"
android:id="@+id/textView2"
android:layout_marginBottom="20dp"
android:textSize="20sp"
android:layout_marginRight="10dp"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="2"
android:paddingLeft="5dp"
android:text="Meaning" />
</LinearLayout>
答案 0 :(得分:1)
在文本更改时输入此代码
@Override
public boolean onQueryTextChange(String query) {
String newText = query.toLowerCase();
ArrayList<NameListClass> newList = new ArrayList<>();
for (NameListClass r : nameList){
String name = r.getOwner();
if (name.contains(newText)){
newList.add(r);
}
}
retailerAdapter.setFilter(newList);
return true;
}
});
在适配器中执行此操作..
public void setFilter(ArrayList<NameListClass> newList){
nameList= new ArrayList<>();
nameList.addAll(newList);
notifyDataSetChanged();
}