您好我正在尝试创建一个活动,其中SearchView用于搜索Android手机中的联系人列表,并点击建议或按"输入"键盘上的按钮,同一个联系人显示在列表视图中,只显示联系人姓名。但是,我遇到了两个问题:
关于建议单击,联系人将显示在列表视图中,但当我进行另一次搜索时,同一行将替换为新联系人。 CursorAdapter没有形成任何列表。
按下"输入"按钮,光标变为" null"值。
这是我的代码:
public class FamilyAndFriends extends AppCompatActivity {
Typeface mytypeface1;
SearchView searchView;
ListView contactlist;
CursorAdapter nameAdapter;
ContentResolver cr;
public static final String projection[] = {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.Contacts.LOOKUP_KEY};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_family_and_friends);
TextView textView = (TextView) findViewById(R.id.textView9);
mytypeface1 = Typeface.createFromAsset(getAssets(), "Roboto-Light.ttf");
textView.setTypeface(mytypeface1);
Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar);
toolbar.setTitle("Friends & Family");
setSupportActionBar(toolbar);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
searchView = (SearchView) findViewById(R.id.searchView);
setupSearchView();
contactlist = (ListView) findViewById(R.id.listView);
cr = getContentResolver();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_user_info, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
break;
case R.id.next:
startActivity(new Intent(this, Settings.class));
break;
}
return true;
}
private void setupSearchView() {
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchableInfo searchableInfo = searchManager.getSearchableInfo(getComponentName());
searchView.setSearchableInfo(searchableInfo);
searchView.setQueryRefinementEnabled(true);
searchView.setIconifiedByDefault(false);
}
@Override
protected void onNewIntent(Intent intent) {
setIntent(intent);
handleIntent();
}
private void handleIntent() {
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
dosearch(query);
} else if (ContactsContract.Intents.SEARCH_SUGGESTION_CLICKED.equals(intent.getAction())) {
Uri nameUri = intent.getData();
String suggested_contact = String.valueOf(nameUri);
dosearch(suggested_contact);
}
}
private void dosearch(final String query) {
Cursor nameCursor = cr.query(Uri.parse(query),projection,null,null,null);
nameCursor.moveToFirst();
int colindex = nameCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
final String result = nameCursor.getString(colindex);
nameAdapter = new CursorAdapter(FamilyAndFriends.this, nameCursor, 0) {
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.contact_listview, parent, false);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView nameContact = (TextView) view.findViewById(R.id.contact_name);
nameContact.setText(result);
nameContact.setTypeface(mytypeface1);
}
};
contactlist.setAdapter(nameAdapter);
nameCursor.close();
}
}
任何人都可以帮我找到所需的清单吗???请帮助。