我是Android新手,我跟着一些關於在ListView上添加搜索功能的教程,但在我的情況下它不工作。有人能告訴我我的代碼有什麼問題嗎?在Android中的CustomArrayAdapter上過濾器
這裏是我的代碼:
Activity.java
public class SendMoney extends Activity {
private ListView lvContacts;
private EditText etSearch;
private ContactListAdapter mAdapter;
private String[] stockArr, stockArr1, stockArr2;
private Intent i;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_send_money);
initObjects();
ArrayList<String> phoneContactList = new ArrayList<String>();
ArrayList<String> ContactImage = new ArrayList<String>();
ArrayList<String> PhoneNumber = new ArrayList<String>();
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String img = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.PHOTO_THUMBNAIL_URI));
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
// This inner cursor is for contacts that have multiple numbers.
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null);
while (pCur.moveToNext()) {
String pnumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
ContactImage.add(img);
phoneContactList.add(name);
PhoneNumber.add(pnumber);
Log.i("Contact List", pnumber);
}
pCur.close();
}
}
Collections.sort(phoneContactList);
int cnt = phoneContactList.size(); // Count of contacts
// Convert ArrayList<String> to String[] START
// Contacts Name
stockArr = new String[phoneContactList.size()];
stockArr = phoneContactList.toArray(stockArr);
// Contacts Photo
stockArr1 = new String[ContactImage.size()];
stockArr1 = ContactImage.toArray(stockArr1);
// Contacts Number
stockArr2 = new String[PhoneNumber.size()];
stockArr2 = PhoneNumber.toArray(stockArr2);
// Convert ArrayList<String> to String[] END
mAdapter = new ContactListAdapter(getApplicationContext(), R.layout.banks_listview_layout, stockArr, stockArr1);
lvContacts.setAdapter(mAdapter);
}
cur.close();
lvContacts.setTextFilterEnabled(true);
lvContacts.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
i = new Intent(SendMoney.this, SendMoneyForm.class);
i.putExtra("contact_name", stockArr[position]);
i.putExtra("contact_image", stockArr1[position]);
i.putExtra("contact_number", stockArr2[position]);
startActivity(i);
}
});
//***************** SEARCH START ***********************
etSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
mAdapter.getFilter().filter(cs.toString());
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
//***************** SEARCH END ***********************
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.send_money, menu);
return true;
}
private void initObjects(){
lvContacts = (ListView)findViewById(R.id.lvContacts);
etSearch = (EditText)findViewById(R.id.etSearch);
}
我的自定義數組適配器
public class ContactListAdapter extends ArrayAdapter<String> {
private LayoutInflater mInflater;
private String[] mStrings;
private String[] mIcons;
private int mViewResourceId;
public ContactListAdapter(Context ctx, int resource,
String[] option, String[] icons) {
super(ctx, resource, option);
mInflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mStrings = option;
mIcons = icons;
mViewResourceId = resource;
}
@Override
public int getCount() {
return mStrings.length;
}
@Override
public String getItem(int position) {
return mStrings[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = mInflater.inflate(mViewResourceId, null);
ImageView iv = (ImageView)convertView.findViewById(R.id.ivBankIcon);
if(mIcons[position]==null) {
iv.setImageResource(R.drawable.default_picture);
} else {
iv.setImageURI(Uri.parse(mIcons[position]));
Log.d("LALA", mIcons[position]);
}
TextView tv = (TextView)convertView.findViewById(R.id.tvBankName);
tv.setText(mStrings[position]);
return convertView;
}
}
提前感謝!
請嘗試詳細說明您的代碼片段應該做什麼以及哪些功能無法正常工作。使人們更容易提供答案。 – ddewaele
檢查下面的代碼搜索START評論..該代碼是我的搜索(從ListView過濾) – Lian