SearchView无法在listview上的android应用程序中运行

时间:2017-02-10 18:20:33

标签: android listview searchview

此处我正在实施SearchView以上联系人listview搜索视图无法正常工作并出错。拜托,有人帮帮我。我是android的新手 我正在创造吸气剂和放大器listview的setter类,并使用名称“adapter”创建Custom_adapter。这是我的MainActivity代码,请帮助我,我被困在这里。

     public class MainActivity extends AppCompatActivity {

    ListView listView;
    Button sync;

   String name, phone;
//Adapter adapter;
Intent intent;
ProgressDialog progressDialog;
TextView tv_count;
ArrayList<Contact_list> listitem;
SearchView searchView;
ArrayAdapter<Contact_list> adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tv_count= (TextView) findViewById(R.id.tv_size);
   // tv_count.setText("hello");


    listitem = new ArrayList<Contact_list>();

      listView = (ListView) findViewById(R.id.listViewID);

    registerForContextMenu(listView);


    //for searchview
    searchView= (SearchView) findViewById(R.id.searchID);


    sync= (Button) findViewById(R.id.syncID);
    sync.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            // GET CONTACTS DATA


           GetContactsIntoArrayList();


        }
    });
}
public void GetContactsIntoArrayList(){
    Cursor cursor;
    cursor =  getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null, null, null);

    while (cursor.moveToNext()) {

        name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME ));

        phone = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        listitem.add(new Contact_list(name,phone));


        listView.setAdapter(new Custom_adapter(MainActivity.this, listitem));

                             /**
                             * for searchview
                             */
                            adapter = new ArrayAdapter<Contact_list>(this,
                                    R.layout.custom_adapter,  listitem);


                            searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                                @Override
                                public boolean onQueryTextSubmit(String query) {

                                    return false;
                                }

                                @Override
                                public boolean onQueryTextChange(String newText) {

                                //it gives error on this line
                                    adapter.getFilter().filter(newText);
                                    // adapter.getF
                                    return false;
                                }
                            });

    }

    cursor.close();

        }
}

1 个答案:

答案 0 :(得分:0)

您正在调用listView.setAdapter(new Custom_adapter...,但之后您正在对另一个未使用的适配器ArrayAdapter实施过滤。 您应该使用SearchView搜索过滤主Custom_adapter

此外,您不应该listView.setAdapter两次调用,删除onClick内的第二个。