带有过滤问题的Android列表视图

时间:2015-06-13 12:17:55

标签: android database listview filter custom-adapter

我有来自数据库的项目的列表视图。现在我在列表视图的顶部放置了一个编辑文本。我想要的是当用户开始输入编辑文本时,应该过滤列表视图。例如,如果用户输入一个字母" A",只有名称以" A"开头。应该出现在列表中。我在代码中使用了自定义适配器。我多次问过这个问题,但尚未找到解决方案。

这是我的数据列表活动代码:

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

2 个答案:

答案 0 :(得分:0)

使用EditText的addTextChangedListener()方法。它看起来像这样

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

答案 1 :(得分:0)

您可以在Xml中使用SearchView代替EditText吗?如果那不是问题,那么这里有完整的工作代码,包括适配器和xml的一切。!

<强> 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" />

让你的mainActivity extends Activity implements SearchView.OnQueryTextListener, Filterable,SearchView.OnCloseListener

MainActivity的onCreate()内部:

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

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

在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;
}

这是自定义适配器..!

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

}