我正在尝试创建一个活动,其中我添加了搜索工具来搜索电话簿中的联系人,并在ListView中列出所选联系人。 我已经能够获得ListView的一行工作。但是,当我尝试添加另一个联系人时,它只是替换了第一行。我是Android应用程序开发的新手。无法弄清楚如何获得所需的输出。
这是我的代码:
import android.app.SearchManager;
import android.app.SearchableInfo;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Typeface;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v4.app.NavUtils;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.TextView;
public class FamilyAndFriends extends AppCompatActivity {
Typeface mytypeface1;
SearchView searchView;
ListView contactlist;
View contact_listview;
LayoutInflater layoutInflater;
Cursor nameCursor;
CursorAdapter nameAdapter;
@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();
layoutInflater = getLayoutInflater();
contactlist = (ListView) findViewById(R.id.listView);
}
@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);
}
private String getDisplayContactName(Intent intent) {
nameCursor = getContentResolver().query(intent.getData(), null, null, null, null);
nameCursor.moveToFirst();
int idDisplayName = nameCursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
return nameCursor.getString(idDisplayName);
}
@Override
protected void onNewIntent(Intent intent) {
if (ContactsContract.Intents.SEARCH_SUGGESTION_CLICKED.equals(intent.getAction())) {
final String CONTACT_NAME = getDisplayContactName(intent);
nameAdapter = new CursorAdapter(this, nameCursor, false) {
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
contact_listview = layoutInflater.inflate(R.layout.contact_listview, parent, false);
return contact_listview;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
final TextView name_Contact = (TextView) view.findViewById(R.id.contact_name);
name_Contact.setText(CONTACT_NAME);
name_Contact.setTypeface(mytypeface1);
}
};
}
contactlist.setAdapter(nameAdapter);
}
@Override
protected void onStop() {
super.onStop();
if (nameCursor != null) {
nameCursor.close();
}
}
}
任何帮助???
答案 0 :(得分:0)
很可能听起来你在getItemCount()
@Override
public int getItemCount() {
return list.size();
}
您需要返回在适配器中传递的列表大小