在列表视图筛选消失时listItems虽然进入东西放到的EditText [英] Filter on listview disappear listitems while entering something into edittext

查看:251
本文介绍了在列表视图筛选消失时listItems虽然进入东西放到的EditText的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很痛苦一个奇怪的问题。其实我已经在我使用的过滤器的定制列表视图,一切工作正常,但是当我键入文本EDITTEXT它消失的所有列表项。我很奇怪为什么这跟我走的,还是我不是机器人一个冠军所以需要一些帮助。我看到的像<一个计算器很多类似的问题href=\"http://stackoverflow.com/questions/2718202/custom-filtering-in-android-using-arrayadapter\">this,这个,<一个href=\"http://stackoverflow.com/questions/17234598/custom-filtering-for-custom-arrayadapter-in-listview\">this, <一href=\"http://stackoverflow.com/questions/2519317/how-to-write-a-custom-filter-for-listview-with-arrayadapter\">and更多,但没有任何工程在我的案件。我不知道我在做什么错误。

I am suffering a weird problem. Actually I have a customize listview in which I am using a filter, everything working fine but when I am typing a text to edittext it disappear the all listitem. I am strange why this going on with me, still I am not a champ of android so need some help. I have seen many similar problems on stackoverflow like this,this,this, and many more, but nothing works in my case. I dont know where i am doing mistake.

我的列表项的点击做工精细,所以希望它也将在项目搜索到之后的EditText工作。

My listitem click working fine, So hope it will also work after item search into edittext.

这是我MainActivity.java:

package com.sunil.listviewmuntilerowdelete;

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

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;



    public class MainActivity extends Activity {

        private String[] myfriendname = null;
        EditText edtSearch;
        private int[] photo = null;
        ListView listView = null;
        Context contex = null;
        MyListAdapter adapter = null;
        private List<MyFriendsSDetails> list = new ArrayList<MyFriendsSDetails>();

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            contex = this;
            listView = (ListView) findViewById(R.id.listview);
            edtSearch = (EditText) findViewById(R.id.EditText01);
            myfriendname = new String[] { "Sunil Gupta", "Abhishek Tripathi",
                    "Sandeep Pal", "Amit Verma" };

            photo = new int[] { R.drawable.sunil, R.drawable.abhi,
                    R.drawable.sandy, R.drawable.amit };

            final String text[] = { "Sunil is a great man",
                    "Abhishek is hardworking", "Sandeep is same as amit",
                    "Amit is unique" };

            final Integer[] image = { R.drawable.sunil, R.drawable.abhi,
                    R.drawable.sandy, R.drawable.amit,

            };

            for (int index = 0; index < myfriendname.length; index++) {
                MyFriendsSDetails details = new MyFriendsSDetails(
                        myfriendname[index], photo[index]);
                list.add(details);
            }

            adapter = new MyListAdapter(contex, list);
            listView.setAdapter(adapter);
            listView.setTextFilterEnabled(true);
            edtSearch.addTextChangedListener(new TextWatcher() {

                @Override
                public void onTextChanged(CharSequence cs, int arg1, int arg2,
                        int arg3) {
                    // When user changed the Text
                    MainActivity.this.adapter.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 s) {
                    // TODO Auto-generated method stub

                }
            });

            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

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

                    Intent intent = new Intent(MainActivity.this,
                            SecondActivity.class);

                    intent.putExtra("key1", image[position]);
                    intent.putExtra("key2", text[position]);
                    startActivity(intent);

                }
            });

        }
    }

MyListAdapter.java

    package com.sunil.listviewmuntilerowdelete;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MyListAdapter extends ArrayAdapter<MyFriendsSDetails> {

    Context context;
    LayoutInflater inflater;
    List<MyFriendsSDetails> list;

    public MyListAdapter(Context context, List<MyFriendsSDetails> list) {
        super(context, 0, list);

        this.context = context;
        this.list = list;
        inflater = LayoutInflater.from(context);
    }

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

        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.list_item, null);
            holder.name = (TextView) convertView.findViewById(R.id.title);

            holder.photo = (ImageView) convertView.findViewById(R.id.icon);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.name.setText(list.get(position).getMyfriendname());

        holder.photo.setImageResource(list.get(position).getPhoto());
        return convertView;
    }

    private class ViewHolder {
        TextView name;

        ImageView photo;
    }
}

这里是MyFriendsSDetails.java:

    package com.sunil.listviewmuntilerowdelete;

public class MyFriendsSDetails {

    private String myfriendname = null;

    private int photo = 0;

    public MyFriendsSDetails(String friendname, int myphoto) {
        this.myfriendname = friendname;

        this.photo = myphoto;
    }

    public String getMyfriendname() {
        return myfriendname;
    }

    public void setMyfriendname(String myfriendname) {
        this.myfriendname = myfriendname;
    }

    public int getPhoto() {
        return photo;
    }

    public void setPhoto(int photo) {
        this.photo = photo;
    }

}

先谢谢了。

推荐答案

当你正在使用自定义适配器,安卓着承认MainActivity.this.adapter.getFilter()过滤器(CS);

As you are using custom adapter, android cant recognize "MainActivity.this.adapter.getFilter().filter(cs);"

您必须覆盖用getFilter()方法,并做手动更改。

You have to override getFilter() method and do manual changes.

低于code结帐:

 @Override
public Filter getFilter() {
    Filter filter = new Filter() {

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


            mDisplayedValues = (ArrayList<HashMap<String, String>>) results.values; // has the filtered values
            notifyDataSetChanged();  // notifies the data with new filtered values
        }

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();        // Holds the results of a filtering operation in values
            ArrayList<HashMap<String, String>> FilteredArrList = new ArrayList<HashMap<String, String>>();

            if (mOriginalValues == null) {
                mOriginalValues = new ArrayList<HashMap<String, String>>(mDisplayedValues); // saves the original data in mOriginalValues

            }

            /********
             * 
             *  If constraint(CharSequence that is received) is null returns the mOriginalValues(Original) values
             *  else does the Filtering and returns FilteredArrList(Filtered)  
             *
             ********/
            if (constraint == null || constraint.length() == 0) {

                // set the Original result to return  
                results.count = mOriginalValues.size();
                results.values = mOriginalValues;
            } else {
                constraint = constraint.toString().toLowerCase();
                for (int i = 0; i < mOriginalValues.size(); i++) {

                    String data = mOriginalValues.get(i).get("name");

                    if (data.toLowerCase().startsWith(constraint.toString())) {
                        HashMap<String, String> hmap=new HashMap<String, String>();
                        hmap.put("name", mOriginalValues.get(i).get("name"));
                        hmap.put("image", mOriginalValues.get(i).get("image"));
                        FilteredArrList.add(hmap);
                        Log.e("DEBUG", "name : "+data);
                    }
                }
                // set the Filtered result to return
                results.count = FilteredArrList.size();
                results.values = FilteredArrList;
            }
            return results;
        }
    };
    return filter;
}

和适配器getView使用()

 holder.name.setText(mDisplayedValues.get(position).get("name")); 

这篇关于在列表视图筛选消失时listItems虽然进入东西放到的EditText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆