我想在我的Android应用程序中搜索列表视图,所以我使用了android搜索视图小部件。我成功实现了搜索,但问题是在搜索后到达。 当我搜索它给我过滤结果,但当我点击过滤的项目,它给我错误的位置。 请帮帮我。
MainActivity
public class MainActivity extends AppCompatActivity implements
SlidingPaneLayout.PanelSlideListener,
android.widget.SearchView.OnQueryTextListener
{
public static ListView mcontact;
RelativeLayout right, left;
public static ArrayList<Contacts> mList;
MyContactsAdapter mMyContactsAdapter;
SlidingPaneLayout sp;
Context context=this;
android.widget.SearchView sv;
public static String number;
public static String email;
@Override
protected void onCreate(Bundle savedInstanceState)
{
context = this;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
right = (RelativeLayout) findViewById(R.id.rightSide);
left = (RelativeLayout) findViewById(R.id.leftSide);
mcontact = (ListView) findViewById(R.id.contact);
sv= (android.widget.SearchView) findViewById(R.id.searchView1);
sp = (SlidingPaneLayout) findViewById(R.id.Sliding);
assert sp != null;
sp.setSliderFadeColor(Color.TRANSPARENT);
sp.openPane();
mList = new ArrayList<Contacts>();
readContacts();
mcontact.setTextFilterEnabled(true);
mcontact.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
number=mList.get(position).getNumber();
email=mList.get(position).getEmail();
view.setSelected(true);
}
});
setupSearchView();
}
private void setupSearchView()
{
sv.setIconifiedByDefault(false);
sv.setOnQueryTextListener(this);
sv.setSubmitButtonEnabled(true);
sv.setQueryHint("Search Here");
}
public void readContacts() {
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, null);
String phone = null;
String emailContact = null;
String emailType = null;
String image_uri = "";
Bitmap bitmap = 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));
image_uri = cur
.getString(cur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
if (Integer
.parseInt(cur.getString(cur
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
System.out.println("name : " + name + ", ID : " + id);
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?", new String[]{id}, null);
while (pCur.moveToNext())
{
phone = pCur
.getString(pCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
System.out.println("phone" + phone);
}
pCur.close();
Cursor emailCur = cr.query
(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID
+ " = ?", new String[]{id}, null);
while (emailCur.moveToNext())
{
emailContact = emailCur
.getString(emailCur
.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
if(TextUtils.isEmpty(emailContact)||emailContact.equalsIgnoreCase(null)||emailContact.equalsIgnoreCase("")){
emailContact="";
Log.e("isEmpty","isEmpty " + emailContact);
}else{
Log.e("gfdszfg","Email " + emailContact);
}
/* emailType = emailCur
.getString(emailCur
.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));*/
Log.e("gfdszfg","Email " + emailContact);
}
emailCur.close();
}
if (image_uri != null) {
System.out.println(Uri.parse(image_uri));
try {
bitmap = MediaStore.Images.Media
.getBitmap(this.getContentResolver(),
Uri.parse(image_uri));
System.out.println(bitmap);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
mList.add(new Contacts(name, phone, image_uri,emailContact));
emailContact="";
}
cur.close();
mMyContactsAdapter = new MyContactsAdapter(MainActivity.this, mList);
mcontact.setAdapter(mMyContactsAdapter);
}
}
@Override
public void onPanelSlide(View panel, float slideOffset)
{
}
@Override
public void onPanelOpened(View panel) {
left.setVisibility(View.VISIBLE);
}
@Override
public void onPanelClosed(View panel)
{
left.setVisibility(View.VISIBLE);
}
@Override
public boolean onQueryTextSubmit(String query)
{
return false;
}
@Override
public boolean onQueryTextChange(String newText)
{
if (TextUtils.isEmpty(newText)) {
mcontact.clearTextFilter();
mMyContactsAdapter.getFilter().filter("");
} else {
mcontact.setFilterText(newText);
}
return true;
}
}
我的适配器类
public class MyContactsAdapter extends BaseAdapter implements Filterable
{
private Context mContext;
ArrayList<Contacts> coverList;
public ArrayList<Contacts> orig;
private ColorGenerator mColorGenerator = ColorGenerator.MATERIAL;
private TextDrawable.IBuilder mDrawableBuilder;
public MyContactsAdapter(Context c, ArrayList<Contacts> coverList)
{
super();
mContext = c;
this.coverList = coverList;
}
@Override
public int getCount()
{
return coverList.size();
}
@Override
public Contacts getItem(int position) {
return coverList.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView,
final ViewGroup parent)
{
View grid = null;
convertView = null;
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
{
grid = new View(mContext);
grid = inflater.inflate(R.layout.list_item, parent, false);
TextView nameTxt = (TextView) grid.findViewById(R.id.name_entry);
final TextView numTxt = (TextView)
grid.findViewById(R.id.number_entry);
final TextView emailTxt = (TextView) grid.findViewById(R.id.email);
final ImageView adImage = (ImageView)
grid.findViewById(R.id.profile_pic);
grid.setTag(adImage);
nameTxt.setText(coverList.get(position).getName());
numTxt.setText(coverList.get(position).getNumber());
emailTxt.setText(coverList.get(position).getEmail());
Log.e("NUMBER","dxdd"+coverList.get(position).getEmail());
if (TextUtils.isEmpty(coverList.get(position).getImage()) || coverList.get(position).getImage().equalsIgnoreCase(null))
{
mDrawableBuilder = TextDrawable.builder()
.beginConfig()
.withBorder(4)
.endConfig()
.round();
TextDrawable drawable =
mDrawableBuilder.build(String.valueOf(coverList.get(position)
.getName().
charAt(0)), mColorGenerator.getColor(coverList.get(position).getName()));
adImage.setImageDrawable(drawable);
} else
{
// System.out.println(Uri.parse(coverList.get(position).getImage()));
Picasso.with(mContext)
.load(coverList.get(position).getImage()).
transform(new CircleTransform())
.resize(60, 60)
.into(adImage);
}
adImage.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v)
{
String number=coverList.get(position).getNumber();
String email=coverList.get(position).getEmail();
ClipData.Item item = new ClipData.Item((CharSequence)number);
ClipData.Item mail = new ClipData.Item((CharSequence)email);
String[] mimeTypes = {ClipDescription.MIMETYPE_TEXT_PLAIN};
ClipData dragData = new ClipData("",mimeTypes,item);
dragData.addItem(mail);
Log.e("NUMBER",number+ "dragdata"+dragData.getItemAt(1));
View.DragShadowBuilder shadowBuilder = new
View.DragShadowBuilder(adImage);
adImage.startDrag(dragData, shadowBuilder, adImage, 1);
adImage.setVisibility(View.INVISIBLE);
return false;
}
});
adImage.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
((ListView) parent).performItemClick(v, position, 0); // Let
the event be handled in onItemClick()
}
});
}
return grid;
}
@Override
public Filter getFilter()
{
return new Filter()
{
@Override
protected FilterResults performFiltering(CharSequence constraint)
{
final FilterResults oReturn= new FilterResults();
final ArrayList<Contacts> result= new ArrayList<Contacts>();
if(orig== null)
orig = coverList ;
if (constraint!=null)
{
if (orig!=null && orig.size()>0)
{
for (final Contacts c:orig)
{
if
(c.getName().toLowerCase().contains(constraint.toString()))
result.add(c);
}
}
oReturn.values = result;
}
return oReturn;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults
results)
{
coverList = (ArrayList<Contacts>) results.values;
notifyDataSetChanged();
}
};
}
public class CircleTransform implements Transformation {
@Override
public Bitmap transform(Bitmap source) {
int size = Math.min(source.getWidth(), source.getHeight());
int x = (source.getWidth() - size) / 2;
int y = (source.getHeight() - size) / 2;
Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size,
size);
if (squaredBitmap != source) {
source.recycle();
}
Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig());
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
BitmapShader shader = new BitmapShader(squaredBitmap,
BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
paint.setShader(shader);
paint.setAntiAlias(true);
float r = size / 2f;
canvas.drawCircle(r, r, r, paint);
squaredBitmap.recycle();
return bitmap;
}
@Override
public String key() {
return "circle";
}
}
}
答案 0 :(得分:0)
您需要在mcontact按钮中单击
更新适配器mMyContactsAdapter.getItem(position);
Contacts mList = getSearchResults(sv.getQuery().toString()).get(position);
number = mList.get(position).getNumber();
email = mList.get(position).getEmail();
答案 1 :(得分:0)
问题是搜索过滤了内部适配器coverList
。因此,在适配器中项目的位置是OK。
但是,然后,您调用适配器的外部(在课堂上)mList
。在那一点上,你正在寻找没有过滤器的原始数组。所以这个立场是错误的。
您需要从适配器中的已过滤阵列中恢复联系人。所以在您的 MainActivity :
中mcontact.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
number = mMyContactsAdapter.getItem(position).getNumber();
email = mMyContactsAdapter.getItem(position).getEmail();
view.setSelected(true);
}
});