简体   繁体   中英

How to implement search in list in given listView using edit text

I am creating an app.I have three data in one row (Name,id,other) of list view. so I want to implement search on list view by name using edit text. I am getting search data by name its good but id is not change according to name on search its still showing fix just like(1,2,3...) in list view.So i want when i search by name then id should be show according to name.

//MainActicity

public class MainActivity extends AppCompatActivity {


private ListView mSearchNFilterLv;

private EditText mSearchEdt;

private ArrayList<String> mStringList;
private ArrayList<String> mStringList1;

private ValueAdapter valueAdapter;

private TextWatcher mSearchTw;

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

    initUI();

    initData();

    valueAdapter=new ValueAdapter(mStringList,mStringList1,this);

    mSearchNFilterLv.setAdapter(valueAdapter);

    mSearchEdt.addTextChangedListener(mSearchTw);
}


private void initData() {

    mStringList=new ArrayList<String>();

    mStringList.add("one");

    mStringList.add("two");

    mStringList.add("three");

    mStringList.add("four");

    mStringList.add("five");

    mStringList.add("six");
    mStringList.add("six");
    mStringList.add("six");
    mStringList.add("six");


    mStringList.add("seven");

    mStringList.add("eight");

    mStringList.add("nine");

    mStringList.add("ten");

    mStringList.add("eleven");

    mStringList.add("twelve");

    mStringList.add("thirteen");

    mStringList.add("fourteen");


    mStringList1=new ArrayList<String>();

    mStringList1.add("1");

    mStringList1.add("2");

    mStringList1.add("3");

    mStringList1.add("4");

    mStringList1.add("5");

    mStringList1.add("6");

    mStringList1.add("7");

    mStringList1.add("8");

    mStringList1.add("9");

    mStringList1.add("10");

    mStringList1.add("11");

    mStringList1.add("12");

    mStringList1.add("13");
    mStringList1.add("15");

    mStringList1.add("16");
    mStringList1.add("17");


    mSearchTw=new TextWatcher() {

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

            valueAdapter.getFilter().filter(s);
        }

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

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    };

}

private void initUI() {

    mSearchNFilterLv=(ListView) findViewById(R.id.list_view);

    mSearchEdt=(EditText) findViewById(R.id.txt_search);
}
}

//value adapter

 public class ValueAdapter extends BaseAdapter implements Filterable {

private ArrayList<String> mStringList;
private ArrayList<String> mStringList1;

private ArrayList<String> mStringFilterList;

private LayoutInflater mInflater;

private ValueFilter valueFilter;

public ValueAdapter(ArrayList<String> mStringList,ArrayList<String> mStringList1,Context context) {

    this.mStringList=mStringList;
    this.mStringList1=mStringList1;

    this.mStringFilterList=mStringList;

    mInflater=LayoutInflater.from(context);

    getFilter();
}

//How many items are in the data set represented by this Adapter.
@Override
public int getCount() {

    return mStringList.size();
}

//Get the data item associated with the specified position in the data set.
@Override
public Object getItem(int position) {

    return mStringList.get(position);
}

//Get the row id associated with the specified position in the list.
@Override
public long getItemId(int position) {

    return position;
}



//Get a View that displays the data at the specified position in the data set.
@Override
public View getView(int position, View convertView, ViewGroup parent) {

    Holder viewHolder;

    if(convertView==null) {

        viewHolder=new Holder();

        convertView=mInflater.inflate(R.layout.list_item,null);

        viewHolder.nameTv=(TextView)convertView.findViewById(R.id.txt_listitem);
        viewHolder.nameTv1=(TextView)convertView.findViewById(R.id.txt_listitem1);

        convertView.setTag(viewHolder);

    }else{

        viewHolder=(Holder)convertView.getTag();
    }

    viewHolder.nameTv.setText(mStringList.get(position).toString());
    viewHolder.nameTv1.setText(mStringList1.get(position).toString());

    return convertView;
}

private class  Holder{

    TextView nameTv;
    TextView nameTv1;
}

//Returns a filter that can be used to constrain data with a filtering pattern.
@Override
public Filter getFilter() {

    if(valueFilter==null) {

        valueFilter=new ValueFilter();
    }

    return valueFilter;
}


private class ValueFilter extends Filter {


    //Invoked in a worker thread to filter the data according to the constraint.
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {

        FilterResults results=new FilterResults();

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

            ArrayList<String> filterList=new ArrayList<String>();

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

                if(mStringFilterList.get(i).contains(constraint)) {

                    filterList.add(mStringFilterList.get(i));

                }
            }


            results.count=filterList.size();

            results.values=filterList;

        }else{

            results.count=mStringFilterList.size();

            results.values=mStringFilterList;

        }

        return results;
    }


    //Invoked in the UI thread to publish the filtering results in the user interface.
    @SuppressWarnings("unchecked")
    @Override
    protected void publishResults(CharSequence constraint,
                                  Filter.FilterResults results) {

        mStringList=(ArrayList<String>) results.values;

        notifyDataSetChanged();


    }

}
}

When i search by name that time id should be show according according to name in list view.

Thanks.

您可以对EditText施加约束,如果输入数字,它将在ListView的该位置读取项目。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM