Y a-t-il un moyen de créer un filtre de recherche sur ma liste personnalisée créée en utilisant BaseAdapter plutôt que ArrayAdapter, car je n'ai rencontré que des méthodes qui utilisent ArrayAdapters dans les listes personnalisées. Follwing est ce que j'ai rencontré Making a filter using Array AdapterAndroid: ListView faire une recherche Filtrer
Répondre
Si vous utilisez la collection de listes, étendez ArrayAdapter - c'est la sous-classe de BaseAdapter. Remplacer la méthode getView (Dans votre cas, copiez simplement votre ancien code getView sans trop de modifications).
Remplacer toString de l'objet qui est dans votre collection et définissez:
listView.setTextFilterEnabled(true);
Je le fais en ce moment 'class FriendsListAdapter extends BaseAdapter implémente Filterable' s'il vous plaît dites-moi ce que vous voulez dire en remplaçant le BaseAdapter? – hassanadnan
Comme je l'ai écrit, il serait beaucoup plus simple de remplacer ArrayAdapter: 'class FriendsListAdapter extends ArrayAdapter
ou ajouter android: textFilterEnabled = "true" pour ListView dans la mise en page – pawelzieba
ArrayAdapter est une classe enfant de BaseAdapter. Le code source de ArrayAdapter est here. Jetez-y un coup d'oeil ..
Cela devrait résoudre votre problème.
Voici un exemple. Editez et utilisez ce dont vous avez besoin.
public class CustomListAdapter extends BaseAdapter {
private ArrayList<String> countryStringList;
private ArrayList<Integer> countryImageList;
private LayoutInflater mInFlater;
// private Bitmap mIcon1;
// private Bitmap mIcon2;
private int layoutID;
private Context context;
// private LayoutInflater mInflater;
public CustomListAdapter(Context context, int textViewResourceId,
ArrayList<String> stringObjects, ArrayList<Integer> imgObjects) {
super();
this.context = context;
countryStringList = stringObjects;
countryImageList = imgObjects;
layoutID = textViewResourceId;
mInFlater = LayoutInflater.from(context);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
// return countryStringList.size();
return countryStringList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final ViewHolder holder;
if (convertView == null) {
convertView = mInFlater.inflate(layoutID, null);
holder = new ViewHolder();
holder.textCountryName = (TextView) convertView
.findViewById(R.id.txtSelectedCountryName);
holder.icon = (ImageView) convertView
.findViewById(R.id.imgSelectedCountryImage);
holder.textCountryPosition = (TextView) convertView
.findViewById(R.id.txtSelectedCountryPosition);
// holder.checkBoxListView=(CheckBox)convertView.findViewById(R.id.CheckBoxListView);
holder.relativeLayoutList = (RelativeLayout) convertView
.findViewById(R.id.relativeLayoutListItem);
holder.checkBoxListView = (CheckBox) convertView
.findViewById(R.id.checkBoxSelectedCountry);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
// holder=(ViewHolder)convertView.findViewById(R.id.TextViewIcon1)
}
holder.checkBoxListView.setTag(position);
holder.textCountryName.setText(countryStringList.get(position));
holder.textCountryPosition.setText(position + 1 + "/"
+ countryStringList.size());
holder.icon.setImageResource((countryImageList.get(position)));
if (ListViewController.checkStatusList.get(position).equalsIgnoreCase("present")) {
holder.checkBoxListView.setVisibility(View.VISIBLE);
} else {
holder.checkBoxListView.setVisibility(View.GONE);
}
/*
* holder.checkBoxListView.setOnCheckedChangeListener(new
* OnCheckedChangeListener(){
*
* @Override public void onCheckedChanged(CompoundButton buttonView,
* boolean isChecked) { // TODO Auto-generated method stub
*
* ListActivityImplementation listActivityImplementation = new
* ListActivityImplementation(); //show("Checkbox");
* ListActivityImplementation.countryStringList.remove(position);
* ListActivityImplementation.countryImageList.remove(position);
* ListActivityImplementation.lv.invalidateViews();
*
* //ListActivityImplementation.show("Checkbox"); }
*
* }) ;
*/
holder.textCountryName.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
holder.textCountryPosition.setText(position + 1 + "/"
+ countryStringList.size() + "clicked");
}
});
holder.checkBoxListView
.setOnCheckedChangeListener(new android.widget.CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
MathHelper.showToast(context, buttonView.getTag()
+ " clicked");
}
});
return convertView;
}
}
class ViewHolder {
TextView textCountryName;
ImageView icon;
TextView textCountryPosition;
RelativeLayout relativeLayoutList;
CheckBox checkBoxListView;
}
Merci pour cela, mais je veux savoir comment utiliser l'interface Filterbale avec l'adaptateur de base, de sorte que je peux filtrer chercher ma liste voir – hassanadnan
Voici un exemple pour une vue de la liste créée en utilisant l'adaptateur de base. J'ai beaucoup cherché mais je n'ai pas trouvé de solution satisfaisante. D'où j'ai pensé à le mettre en place pour qu'à l'avenir ça puisse aider les autres.
est ici le code de history_container.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
<EditText android:layout_width="fill_parent" android:layout_height="wrap_content"
android:id="@+id/filter_text" />
<FrameLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="@+id/historyContainerLayout" />
</LinearLayout>
Voici le code de history_list_view.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:gravity="left|center"
android:layout_width="wrap_content" android:paddingBottom="5px"
android:paddingTop="5px" android:paddingLeft="5px">
<TextView android:text="@+id/historytext" android:id="@+id/historytext"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_marginLeft="10px" android:textColor="#0099CC"/>
</LinearLayout>
Voici le code de history_schedule.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ScrollView android:id="@+id/scrollItemInfo"
android:layout_width="fill_parent" android:layout_height="1000dip">
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<ListView android:id="@+id/historylist" android:layout_height="1000dip"
android:layout_width="fill_parent"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
Désolé, je ne suis pas en mesure de mettre le code format.Seems corriger quelque chose a changé car il n'y a pas d'étiquette plus qui était nice.But pour la commodité de l'utilisateur en est aussi une bonne idée d'avoir onglet. Peut-être que c'est de ma faute si je ne suis pas en mesure de poster dans un format correct. Et il semble que nous ne pouvons pas poster le contenu du fichier xml .. Alors, pensez à la façon dont la mise en page aurait conçu.
Le code d'activité est: -
package com.testfilter;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.TextView;
public class TestFilterListView extends Activity {
FrameLayout historyContainer;
ViewStub viewStub;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.history_container);
historyContainer = (FrameLayout) findViewById(R.id.historyContainerLayout);
EditText filterEditText = (EditText) findViewById(R.id.filter_text);
filterEditText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
historyContainer.removeAllViews();
final List<String> tempHistoryList = new ArrayList<String>();
tempHistoryList.addAll(historyList);
for(String data : historyList) {
if(data.indexOf((s.toString())) == -1) {
tempHistoryList.remove(data);
}
}
viewStub = new ViewStub(TestFilterListView.this, R.layout.history_schedule);
viewStub.setOnInflateListener(new ViewStub.OnInflateListener()
{
public void onInflate(ViewStub stub, View inflated)
{
setUIElements(inflated, tempHistoryList);
}
});
historyContainer.addView(viewStub);
viewStub.inflate();
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
setViewStub();
}
/********************************************************************************************************/
private void setViewStub()
{
historyList.add("first");
historyList.add("second");
historyList.add("third");
historyList.add("fourth");
historyList.add("fifth");
historyList.add("sixth");
historyList.add("seventh");
viewStub = new ViewStub(TestFilterListView.this, R.layout.history_schedule);
viewStub.setOnInflateListener(new ViewStub.OnInflateListener()
{
public void onInflate(ViewStub stub, View inflated)
{
setUIElements(inflated, historyList);
}
});
historyContainer.addView(viewStub);
viewStub.inflate();
}
/********************************************************************************************************/
final List<String> historyList = new ArrayList<String>();
String displayName = "";
ListView historyListView;
private void setUIElements(View v, List<String> historyLists)
{
if (v != null)
{
historyScheduleData.clear();
//historyList.clear();
historyScheduleData.addAll(historyLists);
historyListView = (ListView) findViewById(R.id.historylist);
historyListView.setAdapter(new BeatListAdapter(this));
registerForContextMenu(historyListView);
}
}
/********************************************************************************************************/
private static class BeatListAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public BeatListAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return historyScheduleData.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.history_list_view, null);
holder = new ViewHolder();
holder.historyData = (TextView) convertView
.findViewById(R.id.historytext);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.historyData.setText(historyScheduleData.get(position));
return convertView;
}
static class ViewHolder {
TextView historyData;
}
}
private static final List<String> historyScheduleData = new ArrayList<String>();
}
voici l'url où vous pouvez voir mon blog [link] http://androidsearchfilterlistview.blogspot.com/ –
- 1. Comment faire une recherche ListView dans Android (Style du contact)
- 2. Comment faire une recherche dans listview
- 3. Filtrer une recherche onkeyup
- 4. Filtrer une ListView à l'aide d'un compteur en android
- 5. Filtrer les données dans listview
- 6. Android: Filtrer une application
- 7. comment filtrer plusieurs lignes dans listview dans android
- 8. Comment filtrer ListView à travers EditText
- 9. Recherche d'un ListView qui utilise SimpleAdapter android
- 10. Comment filtrer un listview avec et éditer la boîte - Android
- 11. Comment filtrer ListView dans WPF?
- 12. Comment puis-je créer une barre de recherche avec un ListView sur Mono pour Android?
- 13. Filtrer ListView Basé sur les entrées de l'utilisateur dans Android?
- 14. Android listview avec recherche à l'intérieur de SlidingDrawer
- 15. comment faire une recherche de filtre pour l'en-tête dans listview
- 16. Recherche Android dans un ListView (Android API niveau 8)
- 17. Android: Comment faire référence à listView?
- 18. Comment faire une recherche NSMutableDictionary
- 19. Android: Filtrage d'un ListView SimpleCursorAdapter
- 20. Faire un ListView rebondissant dans Android
- 21. Fonctionnalité de recherche pour Listview
- 22. Comment faire un filtre ListView sur Android
- 23. Faire une fonction de recherche
- 24. Filtrer les données dans ListView [mvvm]
- 25. Filtrer recherche dans la langue
- 26. comment faire une recherche de filtre
- 27. Comment faire l'argument pour filtrer() une variable?
- 28. Comment faire pour rechercher un listview dans android?
- 29. ListView filtre pour android
- 30. Recherche d'un listview avec adaptateur personnalisé
SHOUD j'implémenter l'interface filtrables dans mon BaseAdapter? – hassanadnan
Qu'est-ce que vous utilisez, Cursor ou Array ou autre chose? – pawelzieba
J'utilise BaseAdapter – hassanadnan