我有一个自定义列表视图,其中包含一个对象类型Arraylist,在同一个活动中我有一个SearchView,如何根据关键字搜索和显示对象,例如getName(),getCity(),... 这是我的代码,我的onResume
@Override
protected void onResume() {
super.onResume();
ArrayList<User> users;
searchBar = findViewById(R.id.maBtnSearch);
db = new DatabaseUser(this);
users = db.getData();
final CustomAdapter adapter = new CustomAdapter(this, users);
listView.setAdapter(adapter);
searchBar.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
adapter.getFilter().filter(newText);
return false;
}
});
}
这是我的适配器类:
package com.example.mickael.gestioclientel;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
/**
* Created by MICKAEL on 02/12/2017.
*/
class CustomAdapter extends ArrayAdapter{
private Context context;
private LayoutInflater inflater;
private ArrayList<User> users; //Lui transmettre le tableau adapter
private DatabaseUser db;
public CustomAdapter(Context context, ArrayList<User> users){
super(context, R.layout.design_list_user, users); //Lui transmettre le layout design de ta listeview
db = new DatabaseUser(context);
this.context = context;
inflater = LayoutInflater.from(context);
this.users = users;
}
private class ViewHolder{
TextView name, phone, date;
ImageView btnDial, btnMap, menuPoint;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
final ViewHolder viewHolder;
if(convertView == null){
//Reglage general
viewHolder = new ViewHolder();
convertView = inflater.inflate(R.layout.design_list_user, parent, false); //Layout du design
//Recuperation des ID
viewHolder.name = convertView.findViewById(R.id.luPrenom);
viewHolder.phone = convertView.findViewById(R.id.maTxtNumber);
viewHolder.btnDial = convertView.findViewById(R.id.maBtnDial);
viewHolder.btnMap = convertView.findViewById(R.id.maBtnMap);
viewHolder.menuPoint = convertView.findViewById(R.id.luTrois);
viewHolder.date = convertView.findViewById(R.id.maAdd);
//Definition du tag
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) convertView.getTag();
}
//Definition des changements
final User user = users.get(position);
//On regle la taille du nom selon les caractere
if(user.getName().length() == 15){
viewHolder.name.setTextSize(24);
}else if(user.getName().length() == 16){
viewHolder.name.setTextSize(23);
}else if(user.getName().length() == 17){
viewHolder.name.setTextSize(22);
}else if(user.getName().length() > 18){
viewHolder.name.setTextSize(21);
}
viewHolder.name.setText(user.getName()); //Definition du nom
//Definition de lajout ou modification
String addOrUpdate = "Modifie le";
viewHolder.date.setText(addOrUpdate + " " + user.getDate()); //Definition de la date
viewHolder.date.setTextSize(15);
//TODO Je dois regler ce probleme de zero ci dessous
String phone = String.valueOf(user.getPhone());
if(phone.equals("93260")){
phone = "No number phone";
viewHolder.phone.setTextSize(16);
}else{
phone = "0" + phone;
viewHolder.phone.setTextSize(20);
}
viewHolder.phone.setText(phone);
//OnClick sur les boutons
viewHolder.btnDial.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
//TODO ICI Aussi Je dois regler ce probleme de zero ci dessous
Uri.parse("tel:0" + user.getPhone()));
context.startActivity(intent); //Laligne la plus importante!!
}
});
viewHolder.btnMap.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
adressMap(); //Fonction appel de louverture des map
}
public void adressMap() {
String adress = user.getAdresse();
String txtFind = " ";
String txtReplace= "+";
String tmp;
int lgFind = txtFind.length();
for (int k = 0; k < adress.length(); k++) {
try {
tmp = adress.substring(k, k + lgFind);
}
catch(Exception e) {
break;
}
if ( tmp.equalsIgnoreCase(txtFind) ) {
adress = adress.substring(0, k) + txtReplace + adress.substring(k + lgFind,adress.length());
}
}
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("google.navigation:q=" +adress+ "+" + user.getCity()));
context.startActivity(intent);
} //Remplacement des espace par des "+"
}); //Pour lancer la navigation
//le menu a 3 points
viewHolder.menuPoint.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
menuTroisPoint(viewHolder, user);
}
});
return convertView;
}
public void menuTroisPoint(ViewHolder viewHolder, final User user) {
PopupMenu popupMenu = new PopupMenu(context, viewHolder.menuPoint);
popupMenu.getMenuInflater().inflate(R.menu.popup_menu, popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
Intent intent;
switch (item.getItemId()){
case R.id.menu_delete:
//Si il selectionne Delete on lui apparait un popup de securite
dialogConfirmDelete(user);
break;
case R.id.menu_edit:
intentPageEdit(user); //Vers la page dedition
break;
case R.id.menu_send:
popupSendSMS();
break;
}
return true;
}
});
popupMenu.show();
}
public void popupSendSMS() {
android.support.v7.app.AlertDialog.Builder cBuilder = new android.support.v7.app.AlertDialog.Builder(context);
View mView = LayoutInflater.from(context).inflate(R.layout.custom_dialog_sms, null);
RadioGroup rg = mView.findViewById(R.id.cdsRadioGroup);
EditText inputPers = mView.findViewById(R.id.cdsInputPers);
int radioId = rg.getCheckedRadioButtonId();
if (radioId == R.id.cdsRadioPers) { //Cest le ID du RADIO LUI MEME
inputPers.setEnabled(true); //TODO CORRIGER CE PROBLEME
}
cBuilder.setView(mView);
android.support.v7.app.AlertDialog dialog = cBuilder.create();
dialog.show();
}
public void dialogConfirmDelete(final User user) {
AlertDialog.Builder popupSure = new AlertDialog.Builder(context); //On creer la fenetre
popupSure.setTitle("Delete"); //Le titre de l'alerte
popupSure.setMessage("Are you sure?");
popupSure.setNegativeButton("CANCEL", null); //Option cancel
//Option OK
popupSure.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//On supprime lutilisateur selon le ID
delete(user);
}
}); //Option ok
popupSure.show(); //On fait apparaitre le popup
}
public void delete(User user) {
db.delete((int)user.getId());
Toast.makeText(context, "" + user.getName() + " deleted", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(context, MainActivity.class);
context.startActivity(intent);
((Activity)context).finish();
} //Supprimer un user
public void intentPageEdit(User user) {
Intent intent;
intent = new Intent(context, AddUserActivity.class);
Bundle args = new Bundle(); //On creer le bundle car cest un objet
args.putSerializable("user", (user)); //On pose les donnees de l'intent pour le convertir en bundle en tant que objet
intent.putExtra("BUNDLE", args); //On place les donnes enregistrer dans l'intent quon fera passe
context.startActivity(intent);
} //Aller vers la page dedition
}
向上 谢谢,请帮帮我 谢谢,请帮帮我 谢谢,请帮帮我 谢谢,请帮助我
答案 0 :(得分:0)
在适配器类中再生一个arraylist
并制作适配器implements Filterable
,尝试更改适配器,如下所示:
class CustomAdapter extends ArrayAdapter implements Filterable{
private Context context;
private LayoutInflater inflater;
private ArrayList<User> users;
private ArrayList<User> usersFilter;
private DatabaseUser db;
public CustomAdapter(Context context, ArrayList<User> users){
super(context, R.layout.design_list_user, users); //Lui transmettre le layout design de ta listeview
db = new DatabaseUser(context);
this.context = context;
inflater = LayoutInflater.from(context);
this.users = users;
this.usersFilter= users;
}
private class ViewHolder{
TextView name, phone, date;
ImageView btnDial, btnMap, menuPoint;
}
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
String charString = charSequence.toString();
if (charString.isEmpty()) {
users= usersFilter;
} else {
ArrayList<User> filteredList = new ArrayList<>();
for (User user: usersFilter) {
if ( user.getYourField().toLowerCase().contains(charString)) {
filteredList.add(user);
}
}
users= filteredList;
}
FilterResults filterResults = new FilterResults();
filterResults.values = users;
return filterResults;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
users= (ArrayList<User>) filterResults.values;
notifyDataSetChanged();
}
};
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
final ViewHolder viewHolder;
if(convertView == null){
//Reglage general
viewHolder = new ViewHolder();
convertView = inflater.inflate(R.layout.design_list_user, parent, false); //Layout du design
//Recuperation des ID
viewHolder.name = convertView.findViewById(R.id.luPrenom);
viewHolder.phone = convertView.findViewById(R.id.maTxtNumber);
viewHolder.btnDial = convertView.findViewById(R.id.maBtnDial);
viewHolder.btnMap = convertView.findViewById(R.id.maBtnMap);
viewHolder.menuPoint = convertView.findViewById(R.id.luTrois);
viewHolder.date = convertView.findViewById(R.id.maAdd);
//Definition du tag
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) convertView.getTag();
}
//Definition des changements
final User user = users.get(position);
//On regle la taille du nom selon les caractere
if(user.getName().length() == 15){
viewHolder.name.setTextSize(24);
}else if(user.getName().length() == 16){
viewHolder.name.setTextSize(23);
}else if(user.getName().length() == 17){
viewHolder.name.setTextSize(22);
}else if(user.getName().length() > 18){
viewHolder.name.setTextSize(21);
}
viewHolder.name.setText(user.getName()); //Definition du nom
//Definition de lajout ou modification
String addOrUpdate = "Modifie le";
viewHolder.date.setText(addOrUpdate + " " + user.getDate()); //Definition de la date
viewHolder.date.setTextSize(15);
//TODO Je dois regler ce probleme de zero ci dessous
String phone = String.valueOf(user.getPhone());
if(phone.equals("93260")){
phone = "No number phone";
viewHolder.phone.setTextSize(16);
}else{
phone = "0" + phone;
viewHolder.phone.setTextSize(20);
}
viewHolder.phone.setText(phone);
//OnClick sur les boutons
viewHolder.btnDial.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
//TODO ICI Aussi Je dois regler ce probleme de zero ci dessous
Uri.parse("tel:0" + user.getPhone()));
context.startActivity(intent); //Laligne la plus importante!!
}
});
viewHolder.btnMap.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
adressMap(); //Fonction appel de louverture des map
}
public void adressMap() {
String adress = user.getAdresse();
String txtFind = " ";
String txtReplace= "+";
String tmp;
int lgFind = txtFind.length();
for (int k = 0; k < adress.length(); k++) {
try {
tmp = adress.substring(k, k + lgFind);
}
catch(Exception e) {
break;
}
if ( tmp.equalsIgnoreCase(txtFind) ) {
adress = adress.substring(0, k) + txtReplace + adress.substring(k + lgFind,adress.length());
}
}
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("google.navigation:q=" +adress+ "+" + user.getCity()));
context.startActivity(intent);
} //Remplacement des espace par des "+"
}); //Pour lancer la navigation
//le menu a 3 points
viewHolder.menuPoint.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
menuTroisPoint(viewHolder, user);
}
});
return convertView;
}
public void menuTroisPoint(ViewHolder viewHolder, final User user) {
PopupMenu popupMenu = new PopupMenu(context, viewHolder.menuPoint);
popupMenu.getMenuInflater().inflate(R.menu.popup_menu, popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
Intent intent;
switch (item.getItemId()){
case R.id.menu_delete:
//Si il selectionne Delete on lui apparait un popup de securite
dialogConfirmDelete(user);
break;
case R.id.menu_edit:
intentPageEdit(user); //Vers la page dedition
break;
case R.id.menu_send:
popupSendSMS();
break;
}
return true;
}
});
popupMenu.show();
}
public void popupSendSMS() {
android.support.v7.app.AlertDialog.Builder cBuilder = new android.support.v7.app.AlertDialog.Builder(context);
View mView = LayoutInflater.from(context).inflate(R.layout.custom_dialog_sms, null);
RadioGroup rg = mView.findViewById(R.id.cdsRadioGroup);
EditText inputPers = mView.findViewById(R.id.cdsInputPers);
int radioId = rg.getCheckedRadioButtonId();
if (radioId == R.id.cdsRadioPers) { //Cest le ID du RADIO LUI MEME
inputPers.setEnabled(true); //TODO CORRIGER CE PROBLEME
}
cBuilder.setView(mView);
android.support.v7.app.AlertDialog dialog = cBuilder.create();
dialog.show();
}
public void dialogConfirmDelete(final User user) {
AlertDialog.Builder popupSure = new AlertDialog.Builder(context); //On creer la fenetre
popupSure.setTitle("Delete"); //Le titre de l'alerte
popupSure.setMessage("Are you sure?");
popupSure.setNegativeButton("CANCEL", null); //Option cancel
//Option OK
popupSure.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//On supprime lutilisateur selon le ID
delete(user);
}
}); //Option ok
popupSure.show(); //On fait apparaitre le popup
}
public void delete(User user) {
db.delete((int)user.getId());
Toast.makeText(context, "" + user.getName() + " deleted", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(context, MainActivity.class);
context.startActivity(intent);
((Activity)context).finish();
} //Supprimer un user
public void intentPageEdit(User user) {
Intent intent;
intent = new Intent(context, AddUserActivity.class);
Bundle args = new Bundle(); //On creer le bundle car cest un objet
args.putSerializable("user", (user)); //On pose les donnees de l'intent pour le convertir en bundle en tant que objet
intent.putExtra("BUNDLE", args); //On place les donnes enregistrer dans l'intent quon fera passe
context.startActivity(intent);
} //Aller vers la page dedition
}
注意:将user.getYourField()
替换为User
类中的任何字段,以检查传入适配器的过滤器文本