搜索栏中的Android应用程序中的活动

时间:2014-12-05 06:16:30

标签: java android

我宣布了

HeaderListView list;

我尝试实现搜索栏。一切都很好,但这条线显示错误。我没有使用listview我用过列表 这是行

list.setTextFilterEnabled(true);

我必须在这做什么?..

提前致谢

 list = new HeaderListView(this);

    list.setAdapter(new SectionAdapter() {

        @Override
        public int numberOfSections() {
            return aStatesDetails.size();
        }

        @Override
        public int numberOfRows(int section) {
            return aStatesDetails.get(section).getCoursesDetails().size();
        }

        @Override
        public Object getRowItem(int section, int row) {
            return null;
        }

        @Override
        public boolean hasSectionHeaderView(int section) {
            return true;
        }

        @Override
        public View getRowView(int section, int row, View convertView, ViewGroup parent) {

            ViewHolder holder = null;
            if (convertView == null ) {
                //Log.i("section Details","value : "+ section + " data : " + si + " view created");
                convertView = SearchGolfCourseActivity.this.getLayoutInflater().inflate(R.layout.searchcourse_adapter, null);
                holder = new ViewHolder();
                holder.mTextView_CourseName = (TextView) convertView.findViewById(R.id.mTextView_CourseName);
                holder.mTextView_Update = (TextView) convertView.findViewById(R.id.mTextView_Update);

                holder.mTextView_CourseName.setPadding(dScreenSizeWidth/50, dScreenSizeHeight/50, dScreenSizeWidth/50, dScreenSizeHeight/50);
                holder.mTextView_Update.setPadding(0, dScreenSizeHeight/50, dScreenSizeWidth/50, dScreenSizeHeight/50);

                /*holder.mTextView_Update.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View view) {
                        // TODO Auto-generated method stub
                        int getSection = (Integer) view.getTag(R.id.mTextView_CourseName);
                        int getPosition = (Integer) view.getTag(R.id.mTextView_Update);
                        Log.v("Item Clicked", getSection + "  " + getPosition);
                    }
                });*/

                convertView.setTag(holder);
                convertView.setTag(R.id.mTextView_CourseName, holder.mTextView_CourseName);
                convertView.setTag(R.id.mTextView_Update, holder.mTextView_Update);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            if(holder.mTextView_CourseName != null)
            {
                holder.mTextView_CourseName.setText(aStatesDetails.get(section).getCoursesDetails().get(row).getCourseName());
                holder.mTextView_CourseName.setTag(aStatesDetails.get(section).getCoursesDetails().get(row).getCourseId());
            }

            if(holder.mTextView_Update != null)
                if(aStatesDetails.get(section).getCoursesDetails().get(row).getDownloadStatus())
                {
                    holder.mTextView_CourseName.setTextColor(Color.parseColor("#FF0000"));
                    holder.mTextView_Update.setVisibility(View.VISIBLE);
                    holder.mTextView_Update.setText("Update");
                    holder.mTextView_Update.setTag(R.id.mTextView_CourseName, section);
                    holder.mTextView_Update.setTag(R.id.mTextView_Update, row);
                }
                else
                {
                    holder.mTextView_CourseName.setTextColor(Color.parseColor("#000000"));
                    holder.mTextView_Update.setVisibility(View.GONE);
                }


            return convertView;
        }

        @Override
        public int getSectionHeaderViewTypeCount() {
            return 2;
        }

        @Override
        public int getSectionHeaderItemViewType(int section) {
            return section % 2;
        }

        @Override
        public View getSectionHeaderView(int section, View convertView, ViewGroup parent) {

            if (convertView == null) {
                convertView = (TextView) getLayoutInflater().inflate(getResources().getLayout(android.R.layout.simple_list_item_1), null);
            }

            ((TextView) convertView).setText(aStatesDetails.get(section).getStateName());

            convertView.setBackgroundColor(getResources().getColor(R.color.holo_brown_light));

            return convertView;
        }

        @Override
        public void onRowItemClick(AdapterView<?> parent, View view, final int section, final int row, long id) {
            super.onRowItemClick(parent, view, section, row, id);

            boolean isDownloaded = aStatesDetails.get(section).getCoursesDetails().get(row).getDownloadStatus();
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(SearchGolfCourseActivity.this);

            // Setting Dialog Title
            alertDialog.setTitle("Info");
            // Setting Dialog Message
            if(isDownloaded)
                alertDialog.setMessage("This course has been already downloaded. Do you want to update this course?");
            else
                alertDialog.setMessage("Do you want to download this course?");

            // Setting alert dialog icon
            alertDialog.setIcon(R.drawable.tab_info_light);

            // On pressing Yes button
            alertDialog.setPositiveButton("Yes",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            //StandardRouteFinderActivity.mapObject.redrawMap();
                            dialog.cancel();
                            String courseId = aStatesDetails.get(section).getCoursesDetails().get(row).getCourseId();
                            String courseName = aStatesDetails.get(section).getCoursesDetails().get(row).getCourseName();

                            new MyAsyncTaskDownloadCourse().execute(courseId, courseName);
                        }
                    });

            // on pressing No button
            alertDialog.setNegativeButton("No",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                        }
                    });

            // Showing Alert Message
            alertDialog.show();
            Toast.makeText(SearchGolfCourseActivity.this, "Section " + section + " Row " + row, Toast.LENGTH_SHORT).show();

        }
    });

   list.setTextFilterEnabled(true);
    mCourseListRelativeLayout.addView(list);

3 个答案:

答案 0 :(得分:1)

正如official documentation所指出的那样,你的适配器应该实现Filterable接口,只能使用list.setTextFilterEnabled(true)。你的SectionAdapter是否实现了它?如果没有,请尝试这样做。

答案 1 :(得分:0)

我正在使用编辑文字,希望这会对你有帮助!

inputSearch = (EditText)findViewById(R.id.inputSearch);

ListView lv = getListView();
        inputSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {

                youractivityname.this.adapter.getFilter().filter(cs);
            }
            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                    int arg3) {


            }
            @Override
            public void afterTextChanged(Editable s) {


            }

        });

答案 2 :(得分:0)

使用Edittext从Listview过滤数据。 这里我在我的Adapter类和我的活动中做了什么来过滤数据:

适配器类

public class FindAProfessorAdapter extends ArrayAdapter<Data> implements
        Filterable {

    int layoutResourceId;
    LayoutInflater mInflater;
    ArrayList<Data> list;
    ArrayList<Data> secondlist;
    Context context;
    Data data;

    public FindAProfessorAdapter(Context context, int layoutResourceId,
            ArrayList<Data> data) {
        super(context, layoutResourceId, data);

        mInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        list = new ArrayList<Data>();
        secondlist = new ArrayList<Data>();
        secondlist.addAll(data);
        list.addAll(secondlist);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.list = data;
        getFilter();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        final ViewHolder holder;

        if (convertView == null) {

            mInflater = LayoutInflater.from(context);

            convertView = mInflater.inflate(layoutResourceId, parent, false);

            holder = new ViewHolder();

            holder.textView = (TextView) convertView
                    .findViewById(R.id.findproname);

            convertView.setTag(holder);

        } else {

            holder = (ViewHolder) convertView.getTag();
        }

        holder.textView.setText(list.get(position).getName());

        return convertView;
    }

    @Override
    public Filter getFilter() {

        Filter filter = new Filter() {

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

                list = (ArrayList<Data>) results.values;

                notifyDataSetChanged();

                clear();

                for (int i = 0; i < list.size(); i++) {
                    add(list.get(i));
                    notifyDataSetChanged();
                }
            }

            @Override
            protected FilterResults performFiltering(CharSequence constraint) {

                FilterResults filterResults = new FilterResults();

                ArrayList<Data> FilteredArrList = new ArrayList<Data>();

                if (secondlist == null) {

                    secondlist = new ArrayList<Data>(list);
                }

                if (constraint != null && constraint.toString().length() != 0) {

                    constraint = constraint.toString().toLowerCase(
                            Locale.getDefault());

                    for (int i = 0; i < list.size(); i++) {

                        Data name = secondlist.get(i);

                        if (name.getName().toLowerCase(Locale.getDefault())
                                .contains(constraint)) {

                            FilteredArrList.add(name);
                        }
                    }
                    filterResults.count = FilteredArrList.size();
                    filterResults.values = FilteredArrList;

                } else {
                    synchronized (this) {
                        filterResults.count = secondlist.size();
                        filterResults.values = secondlist;
                    }

                }
                return filterResults;
            }
        };
        return filter;
    }

    class ViewHolder {

        TextView textView;
    }
}

在我的活动中:

editText = (EditText) view.findViewById(R.id.findaprofessor);

    editText.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {

                adapter.getFilter().filter(s.toString());
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

您需要将此作为示例并根据此编辑适配器。