1

我有一个搜索视图,我从中搜索电话簿中的联系人,我想将它们存储在列表视图中..那么如何将搜索到的联系人存储在列表视图中?请问有人可以帮我吗?:(

这是我的代码:

public class SearchViewActivity extends Activity {    
    //private TextView resultText;
    static ArrayList<String> contacts;
    private ListView listview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_view);
        contacts = new ArrayList<String>();

        //resultText = (TextView)findViewById(R.id.searchViewResult);

        setupSearchView();
    }

    private void setupSearchView() {
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        final SearchView searchView = (SearchView) findViewById(R.id.searchView);
        SearchableInfo searchableInfo = searchManager.getSearchableInfo(getComponentName());
        searchView.setSearchableInfo(searchableInfo);
    }

    @Override
    protected void onNewIntent(Intent intent) {

        if (ContactsContract.Intents.SEARCH_SUGGESTION_CLICKED.equals(intent.getAction())) {
            //handles suggestion clicked query


                listview = (ListView)findViewById(R.id.listView1);
                ListAdapter adapter = null;
                listview.setAdapter(adapter);
                /*listview.setAdapter(new BaseAdapter() {

                @SuppressLint("NewApi")
                @Override
                public View getView(int arg0, View arg1, ViewGroup arg2) {
                    // TODO Auto-generated method stub
                    LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
                    View view = inflater.inflate(R.id.listView1, null);
                    ListView listview = (ListView)findViewById(R.id.listView1);
                    int position = 0;
                    listview.setTextAlignment(position);


                    return view;

                }

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

                @Override
                public Object getItem(int position) {
                    // TODO Auto-generated method stub
                    return listview;
                    //return contacts.add(ContactsContract.Contacts.DISPLAY_NAME);
                }

                @Override
                public int getCount() {
                    // TODO Auto-generated method stub
                    return contacts.size();
                }
            });
            */
            //String displayName = getDisplayNameForContact(intent);
        //resultText.setText(displayName);
        } else if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            // handles a search query
            String query = intent.getStringExtra(SearchManager.QUERY);
            //resultText.setText("should search for query: '" + query + "'...");
        }
    }

    private String getDisplayNameForContact(Intent intent) {
        Cursor phoneCursor = getContentResolver().query(intent.getData(), null, null, null, null);
        phoneCursor.moveToFirst();
        int idDisplayName = phoneCursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
        String name = phoneCursor.getString(idDisplayName);
        phoneCursor.close();
        return name;
    }
}

这段代码有问题,我可以搜索联系人但不能将其存储在列表视图中

提前致谢

4

0 回答 0