这是一个奇怪的问题,几乎看起来像一个错误。所以我使用了一个arraylist和一个扩展ArrayAdapter的customlistview。
arraylist 填充 customlistview,我在操作栏中实现了一个搜索框,用于过滤自定义适配器,顺便说一下,它工作得很好。
问题是我正在尝试使用过滤后的数据,所以我在列表视图上使用 OnItemCLickListener 来使用索引并获取与过滤结果相对应的数据。
但是这里发生的是数据被过滤,arraylist 本身也是如此。
说当我打开活动时,它会显示我的所有联系人,所以我使用搜索栏搜索一个给我这个名字的名字
说保罗,
现在保罗在搜索栏过滤后处于 0 索引现在当我点击保罗时,我期望保罗的数据,但我得到了 arraylist 的原始 0 索引数据的数据
说是阿尼尔
所以在 OniItelClickListiner 里面我得到了 anil 的数据插入了 paul 的
这是代码
public class sendSms extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor> {
ListView listView;
ArrayList<contact> arrayList;
CustomArrayAdapterForContact customArrayAdapterForContact;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_send_sms);
listView = (ListView) findViewById(R.id.showContactsListView);
arrayList = new ArrayList<contact>();
customArrayAdapterForContact = new CustomArrayAdapterForContact(this, arrayList);
listView.setAdapter(customArrayAdapterForContact);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(getApplicationContext(), typeMessage.class);
intent.putExtra("id", arrayList.get(position).contact_id);
intent.putExtra("name", arrayList.get(position).display_name);
intent.putExtra("number", arrayList.get(position).display_number);
startActivity(intent);
}
});
getSupportLoaderManager().initLoader(5, null, this);
}
class CustomArrayAdapterForContact extends ArrayAdapter<contact> {
ArrayList<contact> contact;
public CustomArrayAdapterForContact(@NonNull Context context, ArrayList<contact> contact) {
super(context, 0, contact);
this.contact = contact;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
contact innserContact = getItem(position);
if (convertView == null){
convertView = LayoutInflater.from(getContext()).inflate(R.layout.row_layout, parent, false);
}
TextView username = (TextView) convertView.findViewById(R.id.subject);
TextView userNumber = (TextView) convertView.findViewById(R.id.address);
try {
if (innserContact.display_name.length() > 30) {
username.setText(innserContact.display_name.substring(0, 30));
}else{
username.setText(innserContact.display_name);
}
userNumber.setText(innserContact.display_number);
}catch (Exception e){
e.printStackTrace();
}
return convertView;
}
}
@NonNull
@Override
public Loader<Cursor> onCreateLoader(int i, @Nullable Bundle bundle) {
String[] projection = {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.HAS_PHONE_NUMBER
};
return new CursorLoader(
this,
ContactsContract.Contacts.CONTENT_URI,
projection,
null,
null,
ContactsContract.Contacts.DISPLAY_NAME
);
}
@Override
public void onLoadFinished(@NonNull Loader<Cursor> loader, Cursor cursor) {
while(cursor.moveToNext()){
String con_id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String con_name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0){
ContentResolver cr = getContentResolver();
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{con_id}, null);
while (pCur.moveToNext()) {
String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
contact con = new contact(con_id, con_name, phoneNo);
arrayList.add(con);
break;
}
pCur.close();
}
}
customArrayAdapterForContact.notifyDataSetChanged();
}
@Override
public void onLoaderReset(@NonNull Loader<Cursor> loader) {
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.search_box, menu);
MenuItem item = menu.findItem(R.id.app_bar_search);
SearchView searchView = (SearchView)item.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
customArrayAdapterForContact.getFilter().filter(newText);
return true;
}
});
return super.onCreateOptionsMenu(menu);
}
}
过滤后
点击后