搜索ListView的其他方法 [英] Other ways to search a ListView

查看:78
本文介绍了搜索ListView的其他方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用TextChangedListener在ListView上实现搜索功能.但是在EditText中添加了一些字符之后; ListView为空白.我已经在ArrayAdapter类中实现了filter方法.

I am trying to use TextChangedListener to implement search functionality on my ListView. But after adding some character in EditText; the ListView goes blank. I have implemented filter method in my ArrayAdapter class.

我正在从JSON获取数据.

I am getting my data from JSON.

我的Logcat显示:getSlotFromBufferLocked:未知缓冲区

My Logcat shows: getSlotFromBufferLocked: unknown buffer

还有其他搜索ListView的方法吗?

Is there any other way to search ListView?

这是我的代码:

UserList.java

public class UserList extends AppCompatActivity {

private ListView listView;
private ArrayList<MyDataModel> list;
private MyArrayAdapter adapter;
private EditText search;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.user_list);

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

    //Array List for Binding Data from JSON to this List
    list = new ArrayList<>();

    //Binding that List to Adapter
    adapter = new MyArrayAdapter(this, list);

    //Getting List and Setting List Adapter
    listView = (ListView) findViewById(R.id.listView);
    listView.setAdapter(adapter);

    listView.setTextFilterEnabled(true);
    search.addTextChangedListener(new TextWatcher() {

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

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

            String text = search.getText().toString().toLowerCase(Locale.getDefault());
            adapter.filter(text);
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    });


    //Checking Internet Connection
    if (InternetConnection.checkConnection(getApplicationContext())) {
        new GetDataTask().execute();
    } else {
        Snackbar.make(findViewById(R.id.parentLayout),"Internet Connection Not Available", Snackbar.LENGTH_LONG).show();
    }
}

//Creating Get Data Task for Getting Data From Web
class GetDataTask extends AsyncTask<Void, Void, Void> {

    ProgressDialog dialog;
    int jIndex;
    int x;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        //Progress Dialog for User Interaction
        x=list.size();

        if(x==0)
            jIndex=0;
        else
            jIndex=x;

        dialog = new ProgressDialog(UserList.this);
        dialog.setTitle("Please Wait..."+x);
        dialog.setMessage("Retrieving Data");
        dialog.show();

    }

    @Nullable
    @Override
    protected Void doInBackground(Void... params) {

        //Getting JSON Object from Web Using okHttp
        JSONObject jsonObject = JSONParser.getDataFromWeb();

        try {

            if (jsonObject != null) {

                if(jsonObject.length() > 0) {

                    JSONArray array = jsonObject.getJSONArray(Keys.KEY_CONTACTS);

                    //Check Length of Array...
                    int lenArray = array.length();
                    if(lenArray > 0) {
                        for( ; jIndex < lenArray; jIndex++) {

                            //Creating Every time New Object and adding to List
                            MyDataModel model = new MyDataModel();

                            JSONObject innerObject = array.getJSONObject(jIndex);
                            String name = innerObject.getString(Keys.KEY_NAME);

                            model.setName(name);
                            list.add(model);
                        }
                    }
                }
            } else {

            }
        } catch (JSONException je) {
            Log.i(JSONParser.TAG, "" + je.getLocalizedMessage());
        }
        return null;
    }


    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        dialog.dismiss();

        //Checking if List size if more than zero then update ListView
        if(list.size() > 0) {
            adapter.notifyDataSetChanged();

        } else {
            Snackbar.make(findViewById(R.id.parentLayout), "No Data Found", Snackbar.LENGTH_LONG).show();
        }

    }
}
}

我已经在ArrayAdapter类中实现了filter方法.

I have implemented the filter method in my ArrayAdapter class.

这是我的ArrayAdapter类:

Here's my ArrayAdapter class:

MyArrayAdapter.java

public class MyArrayAdapter extends ArrayAdapter<MyDataModel> implements Filterable{

List<MyDataModel> modelList;
Context context;
private LayoutInflater mInflater;
private ArrayList<MyDataModel> arrayList;

public MyArrayAdapter(Context context, List<MyDataModel> objects) {
    super(context, 0, objects);
    this.context = context;
    this.mInflater = LayoutInflater.from(context);
    modelList = objects;

    this.arrayList = new ArrayList<MyDataModel>();
    this.arrayList.addAll(modelList);
}

@Override
public MyDataModel getItem(int position) {
    return modelList.get(position);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final ViewHolder vh;
    if (convertView == null) {
        View view = mInflater.inflate(R.layout.layout_row_view, parent, false);
        vh = ViewHolder.create((RelativeLayout) view);
        view.setTag(vh);
    } else {
        vh = (ViewHolder) convertView.getTag();
    }

    MyDataModel item = getItem(position);

    vh.textViewName.setText(item.getName());
    return vh.rootView;
}


private static class ViewHolder {
    public final RelativeLayout rootView;

    public final TextView textViewName;

    private ViewHolder(RelativeLayout rootView, TextView textViewName) {
        this.rootView = rootView;
        this.textViewName = textViewName;
    }

    public static ViewHolder create(RelativeLayout rootView) {
        TextView textViewName = (TextView) rootView.findViewById(R.id.textViewName);
        return new ViewHolder(rootView, textViewName);
    }
}

// Filter Class
public void filter(String charText) {
    charText = charText.toLowerCase(Locale.getDefault());
    modelList.clear();
    if (charText.length() == 0) {
        modelList.addAll(arrayList);
    } else {
        for (MyDataModel wp : arrayList) {
            if (wp.getName().toLowerCase(Locale.getDefault()).contains(charText)) {
                modelList.add(wp);
            }
        }
    }
    notifyDataSetChanged();
}

推荐答案

与此一起使用链接,并按照以下步骤实施:

Go with this link, and follow steps for implementing the same:

,如果您在其中添加或删除任何字符或空格,则每次调用textchanged侦听器时都会调用缓冲区问题.所以避免使用它.它将导致内存泄漏问题.

and the problem with buffer is called every textchanged listener will call everytime if you add or delete any character or space in it. So avoid using that. It will cause memory leak problems.

这篇关于搜索ListView的其他方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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