Android listview with filter issue

别说谁变了你拦得住时间么 提交于 2019-12-23 02:36:36

问题


I have a listview of items coming from a database. Now I have put an edit text at the top of my listview. What I want is that when a user start typing in the edit text, the listview should be filtered. For example, if the user enters a letter "A", only the names starting with an "A" should appear in the list. I have used a custom adapter in my code. I asked this question many times, but haven't found a solution yet.

This is my data list activity code:

 public class DataListActivity extends Activity {

    ListView listView;
    SQLiteDatabase sqLiteDatabase;
    FoodDbHelper foodDbHelper;
    Cursor cursor;
    ListDataAdapter listDataAdapter;
    private Button button1;
    ListDataAdapter dataAdapter = null;
    Button button;
    DataProvider dataProvider;
    ArrayList<HashMap<String, String>> namessList;
    EditText inputSearch;
    String search_name;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.data_list_layout);
   inputSearch = (EditText)findViewById(R.id.inputSearch);
        inputSearch.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                // When user changes the Text
                listDataAdapter.getFilter().filter(cs);
            }

            @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
            }
        });

        listView = (ListView) findViewById(R.id.list_View);

        listDataAdapter = new ListDataAdapter(getApplicationContext(),
                R.layout.row_layout) {

            @Override
            protected void showCheckedButton(int position, boolean value) {
                // TODO Auto-generated method stub

                DataProvider item = (DataProvider) listDataAdapter
                        .getItem(position);
                Log.i("", "");

                item.setSelected(value);

                Button myButton = (Button) findViewById(R.id.findSelected);
                myButton.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        StringBuffer responseText = new StringBuffer();
                        responseText
                               .append("The following dishes were selected...\n");

                        ArrayList<DataProvider> list = listDataAdapter
                                .getSelectedIndexes();

                        int sum = 0;
                        for (int i = 0; i < list.size(); i++) {
                            DataProvider dataProvider = list.get(i);

                            sum = sum + dataProvider.getCalorie();

                            responseText.append("\n" + dataProvider.getName()
                                    + " : " + dataProvider.getCalorie()
                                    + " kcal"
                                    );

                        }

                        Toast.makeText(getApplicationContext(), ""+responseText+"\n"+"................................."
                                       +"\n"+"Total Calories In Your Menu Is : " +sum,
                               Toast.LENGTH_LONG).show();
                    }
                });
          }
        };
        listView.setAdapter(listDataAdapter);
        foodDbHelper = new FoodDbHelper(getApplicationContext());
        sqLiteDatabase = foodDbHelper.getReadableDatabase();
        cursor = foodDbHelper.getInformations(sqLiteDatabase);
        if (cursor.moveToFirst()) {
            do {
                String name, quantity, fat, protein, sugar, vitamins;
                boolean selected = false;
                String names = null;
                Integer calorie;

                name = cursor.getString(0);
                quantity = cursor.getString(1);
                calorie = Integer.valueOf(cursor.getString(2));
                fat = cursor.getString(3);
                protein = cursor.getString(4);
                sugar = cursor.getString(5);
                vitamins = cursor.getString(6);

                DataProvider dataProvider = new DataProvider(name, quantity,
                        calorie, fat, protein, sugar, vitamins, names, selected);

                listDataAdapter.add(dataProvider);

            } while (cursor.moveToNext());
        }

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {

                String name = (String) ((TextView) view
                        .findViewById(R.id.text_dish_name)).getText();
                String quantity = (String) ((TextView) view
                        .findViewById(R.id.text_dish_quantity)).getText();
                String calorie = (String) ((TextView) view
                        .findViewById(R.id.text_dish_calorie)).getText();
                String fat = (String) ((TextView) view
                        .findViewById(R.id.text_dish_fat)).getText();
                String protein = (String) ((TextView) view
                        .findViewById(R.id.text_dish_protein)).getText();
                String sugar = (String) ((TextView) view
                        .findViewById(R.id.text_dish_sugar)).getText();
                String vitamins = (String) ((TextView) view
                        .findViewById(R.id.text_dish_vitamins)).getText();

                String.valueOf(parent.getItemAtPosition(position));

                Toast.makeText(getApplicationContext(),
                        "dish name is : " + name, Toast.LENGTH_SHORT).show();

                Intent intent = new Intent(getApplicationContext(),
                        Detail.class);
                intent.putExtra("Dish name", name);
                intent.putExtra("Dish quantity", quantity);
                intent.putExtra("Dish calorie", calorie);
                intent.putExtra("Dish fat", fat);
                intent.putExtra("Dish protein", protein);
                intent.putExtra("Dish sugar", sugar);

                intent.putExtra("Dish vitamins", vitamins);

                startActivity(intent);
           }

        });
    }

    public void gobackk(View view) {
        Intent intent = new Intent(this, MainMenu.class);
        startActivity(intent);
    }

回答1:


Use the addTextChangedListener() method of your EditText. It would look something like this

inputSearch = (EditText)findViewById(R.id.editText);
....
....
listView.setAdapter(listDataAdapter);
inputSearch.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
            // When user changes the Text
            listDataAdapter.getFilter().filter(cs);
        }

        @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
        }
    });



回答2:


Instead of EditText can you use SearchView in your Xml?? If that's not problem then here you have complete working code with adapter and xml everything.!

XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MyActivity" >

<SearchView
    android:id="@+id/searchPlayers"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:background="@color/Blue600"
    android:layout_alignParentTop="true" />

<ListView
    android:id="@+id/playersList"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/searchPlayers"
    android:divider="@color/Teal"
    android:dividerHeight="1dp"
    android:scrollingCache="false"
    android:smoothScrollbar="true" />

Let your mainActivity extends Activity implements SearchView.OnQueryTextListener, Filterable,SearchView.OnCloseListener

Inside onCreate() of MainActivity:

searchView = (SearchView) findViewById(R.id.searchPlayers);

    searchView.setOnQueryTextListener(this);
    searchView.setOnCloseListener(this);
    searchView.setQueryHint("Search by Player");

Override these in you MainActivity:

@Override
public boolean onQueryTextChange(String newText) {list will get filtered as per input.
    if (TextUtils.isEmpty(newText)) {
        listView.clearTextFilter();
         customAdapter.getFilter().filter("");
    } else {
        if(customAdapter!=null){
        PlayersCustomAdapter ca = (PlayersCustomAdapter) listView.getAdapter();
        ca.getFilter().filter(newText);
        }
    }
    return true;
}

@Override
public boolean onQueryTextSubmit(String abc) {
    return false;
}

@Override
public android.widget.Filter getFilter() {
    return null;
}

@Override
public boolean onClose() {//it will set the list to initial position when searchView is closed
   customAdapter.getFilter().filter("");
    return false;
}

Here is the Custom Adapter..!

public class PlayersCustomAdapter extends BaseAdapter implements Filterable {


Context context;
ArrayList<PlayersMapper> playersMapperList, filteredplayersMapperList;

public PlayersCustomAdapter(Context context, ArrayList<PlayersMapper> list) {
    super();
    //PlayersMapper is the model~mapper class, where i have all views which i am getting from my web service. 
    this.context = context;
    this.playersMapperList = list;
}

public PlayersCustomAdapter(){

}
class ViewHolder {
    protected TextView tvPlayerName;
    protected TextView tvTeamName;
    protected TextView tvNationality;
    protected ImageView ivPlayerPic;
}

@Override
public Filter getFilter() {
    return new Filter() {

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            final FilterResults filteringResult = new FilterResults();
            final ArrayList<PlayersMapper> filterdResult = new ArrayList<PlayersMapper>();

            if (filteredplayersMapperList == null)
                filteredplayersMapperList = new ArrayList<PlayersMapper>(playersMapperList);
            if (constraint != null) {
                if (filteredplayersMapperList != null
                        && filteredplayersMapperList.size() > 0) {

                    for (final PlayersMapper pmObj : filteredplayersMapperList) {
                        if (pmObj.PlayerName.contains(constraint.toString()))
                            filterdResult.add(pmObj);
                    }
                }


                filteringResult.values = filterdResult;
                filteringResult.count = filterdResult.size();
            }
            return filteringResult;
        }

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint,
                FilterResults results) {

            if (results.count == 0) {
                notifyDataSetInvalidated();
            } else {
                playersMapperList = (ArrayList<PlayersMapper>) results.values;
                notifyDataSetChanged();
            }

        }

    };
}

public void notifyDataSetChanged() {
    super.notifyDataSetChanged();
}

@Override
public int getCount() {

    return playersMapperList.size();
}

@Override
public Object getItem(int position) {

    return playersMapperList.get(position);
}

@Override
public long getItemId(int pos) {
    return pos;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View view = null;
    LayoutInflater inflator = ((Activity) context).getLayoutInflater();
    view = inflator.inflate(R.layout.players_item, null);

    final ViewHolder viewHolder = new ViewHolder();

    viewHolder.tvPlayerName = (TextView) view
            .findViewById(R.id.txtViewPlayerName);
    viewHolder.tvTeamName = (TextView) view
            .findViewById(R.id.txtViewTeamName);
    viewHolder.tvNationality = (TextView) view
            .findViewById(R.id.txtViewCountryName);
    viewHolder.ivPlayerPic = (ImageView) view
            .findViewById(R.id.imgViewPlayers);

    view.setTag(viewHolder);

    ViewHolder holder = (ViewHolder) view.getTag();
    holder.tvPlayerName.setText(playersMapperList.get(position).PlayerName);
    holder.tvTeamName.setText(playersMapperList.get(position).TeamName);
    holder.tvNationality
            .setText(playersMapperList.get(position).Nationality);


    return view;
 }

}


来源:https://stackoverflow.com/questions/30818595/android-listview-with-filter-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!