Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android搜索列表视图_Java_Android_Xml_Android Layout_Listview - Fatal编程技术网

Java Android搜索列表视图

Java Android搜索列表视图,java,android,xml,android-layout,listview,Java,Android,Xml,Android Layout,Listview,我正在使用ListView方法在自定义ListView中搜索。我制作了一个图像和文本视图作为搜索元素。但是,我用作搜索栏的edittext无法正确过滤我的项目,但当仅使用文本进行测试时,它可以正常工作。有没有办法纠正我的代码?它没有显示任何错误。显示的图像显示,当我试图过滤T时,它没有显示淡马锡;相反,它只显示前两个元素 <ListView android:id="@+id/list" android:layout_width="fill_parent" andro

我正在使用
ListView
方法在自定义
ListView
中搜索。我制作了一个
图像和文本视图
作为搜索元素。但是,我用作搜索栏的
edittext
无法正确过滤我的项目,但当仅使用文本进行测试时,它可以正常工作。有没有办法纠正我的代码?它没有显示任何错误。显示的图像显示,当我试图过滤T时,它没有显示淡马锡;相反,它只显示前两个元素

<ListView
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >

</ListView>
MainActivity.java

public class MainActivity extends Activity {

 String[] schools = { "Elias Park Primary",
        "Anderson Secondary",
        "Temasek JC",
        "Republic Poly",
        "Nanyang Poly" ,
        "Meridian JC",
        "Temasek Poly",
        "Ngee Ann Poly",
        "Anglican High"
        };

    Integer[] imageId = {
            R.drawable.eliaspark_pri, 
                  R.drawable.anderson_sec,
                  R.drawable.temasek_jc,
                  R.drawable.republic_poly,
                  R.drawable.nyp,
                  R.drawable.mjc,
                  R.drawable.tp,
                  R.drawable.ngee_ann,
                  R.drawable.anglican_high


        };
     ListView list;
     custom adapter1;
     EditText inputSearch;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    list = (ListView)findViewById(R.id.list);   
    inputSearch = (EditText) findViewById(R.id.inputSearch);

    adapter1 = new custom(MainActivity.this, schools, imageId);


    list.setAdapter(adapter1);


    inputSearch.addTextChangedListener(new TextWatcher(){

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before,
            int count) {
        // TODO Auto-generated method stub
        Log.i("Char Sqequence", ""+s);
        MainActivity.this.adapter1.getFilter().filter(s);   
    }

    @Override
    public void afterTextChanged(Editable s) {
        // TODO Auto-generated method stub

    }



}); 

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}}
<ListView
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >

</ListView>
自定义列表视图:

public class custom extends ArrayAdapter<String>{

private final Activity context ;
private final String[] schools ;
private final Integer[] imageId;

public custom(Activity context, String[] schools, Integer[] imageId){
    super(context, R.layout.list1,schools);
    this.context = context;
    this.schools = schools;
    this.imageId = imageId;
}

public View getView(int position, View view, ViewGroup parent){

    LayoutInflater inflater = context.getLayoutInflater();
    View rowView = inflater.inflate(R.layout.list1,null,true);

    TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.img);

    txtTitle.setText(schools[position]);
    imageView.setImageResource(imageId[position]);

    return rowView;


  }}
<ListView
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >

</ListView>
 <?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:orientation="vertical" >

<TextView
    android:id="@+id/txt"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    />
<ImageView
    android:id="@+id/img"
    android:layout_width="50dp"
    android:layout_height="match_parent"
    />
公共类自定义扩展ArrayAdapter{
私人最终活动背景;
私立学校;
私有最终整数[]imageId;
公共自定义(活动上下文、字符串[]学校、整数[]图像ID){
超级(上下文,右布局,列表1,学校);
this.context=上下文;
这个。学校=学校;
this.imageId=imageId;
}
公共视图getView(内部位置、视图视图、视图组父视图){
LayoutInflater充气器=上下文。getLayoutInflater();
视图行视图=充气机充气(R.layout.list1,null,true);
TextView txtTitle=(TextView)rowView.findViewById(R.id.txt);
ImageView ImageView=(ImageView)rowView.findViewById(R.id.img);
setText(学校[职位]);
setImageResource(imageId[位置]);
返回行视图;
}}
XML列表视图:

<ListView
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >

</ListView>

<ListView
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >

</ListView>
自定义列表视图:

public class custom extends ArrayAdapter<String>{

private final Activity context ;
private final String[] schools ;
private final Integer[] imageId;

public custom(Activity context, String[] schools, Integer[] imageId){
    super(context, R.layout.list1,schools);
    this.context = context;
    this.schools = schools;
    this.imageId = imageId;
}

public View getView(int position, View view, ViewGroup parent){

    LayoutInflater inflater = context.getLayoutInflater();
    View rowView = inflater.inflate(R.layout.list1,null,true);

    TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.img);

    txtTitle.setText(schools[position]);
    imageView.setImageResource(imageId[position]);

    return rowView;


  }}
<ListView
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >

</ListView>
 <?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:orientation="vertical" >

<TextView
    android:id="@+id/txt"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    />
<ImageView
    android:id="@+id/img"
    android:layout_width="50dp"
    android:layout_height="match_parent"
    />


很抱歉,图片无法发布,因为我没有10个声誉,但我要解释的是,当我搜索一封信时,它不会过滤。在您的
自定义
类中,您必须覆盖
getFilter()
方法,并创建一个过滤器,该过滤器将根据您的条件筛选列表。下面是一个示例,说明如何执行此操作:嘿,谢谢您的帮助。是的,我看到了那个帖子,但我还是不明白。这是我能去的地方。是否可能是代码的删除或排列?问题是默认过滤器不知道如何解释数据,您必须创建自己的过滤器。
<ListView
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >

</ListView>