java 适用于Android ListView的简单字符串适配器,它具有一个过滤器,可以提供它找到的任何内容并忽略字边界

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 适用于Android ListView的简单字符串适配器,它具有一个过滤器,可以提供它找到的任何内容并忽略字边界相关的知识,希望对你有一定的参考价值。

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/list_view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:textSize="16sp" >

</TextView>
package com.yourco.yourapp;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;

// The standard text view adapter only seems to search from the beginning of whole words
// so we've had to write this whole class to make it possible to search
// for parts of the arbitrary string we want
public class SearchableAdapter extends BaseAdapter implements Filterable {
	
	private List<String>originalData = null;
	private List<String>filteredData = null;
	private LayoutInflater mInflater;
	private ItemFilter mFilter = new ItemFilter();
	
	public SearchableAdapter(Context context, List<String> data) {
		this.filteredData = data ;
		this.originalData = data ;
    	mInflater = LayoutInflater.from(context);
    }

	public int getCount() {
		return filteredData.size();
	}

	public Object getItem(int position) {
		return filteredData.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

    public View getView(int position, View convertView, ViewGroup parent) {
        // A ViewHolder keeps references to children views to avoid unnecessary calls
        // to findViewById() on each row.
        ViewHolder holder;

        // When convertView is not null, we can reuse it directly, there is no need
        // to reinflate it. We only inflate a new View when the convertView supplied
        // by ListView is null.
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.list_item, null);

            // Creates a ViewHolder and store references to the two children views
            // we want to bind data to.
            holder = new ViewHolder();
            holder.text = (TextView) convertView.findViewById(R.id.list_view);

            // Bind the data efficiently with the holder.

            convertView.setTag(holder);
        } else {
            // Get the ViewHolder back to get fast access to the TextView
            // and the ImageView.
            holder = (ViewHolder) convertView.getTag();
        }

        // If weren't re-ordering this you could rely on what you set last time
        holder.text.setText(filteredData.get(position));

        return convertView;
    }
	
    static class ViewHolder {
        TextView text;
    }

	public Filter getFilter() {
		return mFilter;
	}

	private class ItemFilter extends Filter {
		@Override
		protected FilterResults performFiltering(CharSequence constraint) {
			
			String filterString = constraint.toString().toLowerCase();
			
			FilterResults results = new FilterResults();
			
			final List<String> list = originalData;

			int count = list.size();
			final ArrayList<String> nlist = new ArrayList<String>(count);

			String filterableString ;
			
			for (int i = 0; i < count; i++) {
				filterableString = list.get(i);
				if (filterableString.toLowerCase().contains(filterString)) {
					nlist.add(filterableString);
				}
			}
			
			results.values = nlist;
			results.count = nlist.size();

			return results;
		}

		@SuppressWarnings("unchecked")
		@Override
		protected void publishResults(CharSequence constraint, FilterResults results) {
			filteredData = (ArrayList<String>) results.values;
			notifyDataSetChanged();
		}

	}
}

以上是关于java 适用于Android ListView的简单字符串适配器,它具有一个过滤器,可以提供它找到的任何内容并忽略字边界的主要内容,如果未能解决你的问题,请参考以下文章

Android ListView Edge Fading 适用于 2.2 虚拟设备,但不适用于三星 2.2.1

TextView 更改适用于使用 ListView 和 ArrayAdapter 的多个 TextView

使用适用于 Android 应用的自定义适配器将项目动态添加到列表视图

适用于 Android 的付费组件

颤振 ListView.builder 使 onPressed 仅适用于关注项

Spannable String 仅适用于 ListView 中的最后一项