整合Listview和搜索

时间:2019-01-09 16:21:44

标签: android

我很难将搜索集成到自定义列表视图中。我有一个使用layout.xml和适配器的列表视图,然后在现有列表视图中添加搜索时搜索正在工作,但布局未显示在列表中。请单击此处以获取文件 Android Zip Click here

主要活动

public class MainActivity extends Activity{
EditText search;
ListView listview;
ArrayList<Person> list=new ArrayList<Person>();
ArrayList<Person> source=new ArrayList<Person>();
ArrayAdapter<Person> adapter;

AlertDialog.Builder builder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//
this.search=(EditText) this.findViewById(R.id.editText1);
        this.listview=(ListView) this.findViewById(R.id.listView1);
        //populate source

        //list.add(new Person(R.drawable.melvin,"Melvin ","PhD Educational Management"));
        source.add(new Person(R.drawable.dennis,"Dennis ","Master in Information Technology"));
        source.add(new Person(R.drawable.jonathan,"Jonathan ","PhD in Technological Management"));
        source.add(new Person(R.drawable.bell,"Bell ","MS Information Technology"));
        source.add(new Person(R.drawable.jennifer,"Jennifer ","PhD Technological Management"));
        source.add(new Person(R.drawable.gian,"Gian","MS Information Technology"));
        //delegatethe list to the adapter
        adapter=new ArrayAdapter<Person>(this,android.R.layout.simple_list_item_1,list);
        //delegate the  adapter to the listview
        this.listview.setAdapter(adapter);
        //delegate a key watcher
        this.search.addTextChangedListener(new TextWatcher(){

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

            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1,
                    int arg2, int arg3) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onTextChanged(CharSequence arg0, int arg1,
                    int arg2, int arg3) {
                // TODO Auto-generated method stub
                //clear the listview
                list.clear();
                //get match
                String regex=arg0.toString();
                Pattern p=Pattern.compile(regex);
                for(int i=0;i<source.size();i++){
                    Matcher m=p.matcher((CharSequence) list.get(i));
                    while(m.find()){
                        list.add(source.get(i));
                        adapter.notifyDataSetChanged();
                    }
                }
            }

        });
    }

适配器    包com.example.searchlist;

公共类ItemAdapter扩展了BaseAdapter {

Context context;
ArrayList<Person> list;
LayoutInflater inflater;

public ItemAdapter(Context context, ArrayList<Person> list) {
    super();
    this.context = context;
    this.list = list;
    this.inflater=LayoutInflater.from(context);
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return list.size();
}

@Override
public Object getItem(int arg0) {
    // TODO Auto-generated method stub
    return list.get(arg0);
}

@Override
public long getItemId(int arg0) {
    // TODO Auto-generated method stub
    return arg0;
}

@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
    PersonHandler handler =null;
    if(arg1==null){
        arg1=inflater.inflate(R.layout.item_layout, null);
        handler=new PersonHandler();
        handler.iv=(ImageView) arg1.findViewById(R.id.imageView1);
        handler.name=(TextView) arg1.findViewById(R.id.textView1);
        handler.course=(TextView) arg1.findViewById(R.id.textView2);
        arg1.setTag(handler);
    }
    else handler=(PersonHandler) arg1.getTag();

    handler.iv.setImageResource(list.get(arg0).getImg());
    handler.name.setText(list.get(arg0).getName());
    handler.course.setText(list.get(arg0).getCourse());

    return arg1;
}
static class PersonHandler{
    ImageView iv;
    TextView name, course;
}
}

布局

<?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="horizontal" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:src="@drawable/ic_launcher" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:gravity="center_vertical"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#0000ff" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="#ff0000" />

</LinearLayout>

Activity_main.xml

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#4682b4"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<EditText
    android:id="@+id/editText1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#ffffff"
    android:ems="10"
    android:hint="SEARCH"
    android:padding="10dp" >

    <requestFocus />
</EditText>

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:background="#fafad2" >

</ListView>

</LinearLayout>

0 个答案:

没有答案