简体   繁体   中英

how to make input search for custom list view

hello to every one i have a custom list view with a search and animation all works cool but in search i have a little problem . I would like to, for example, I search for a word in a sentence. But the way I used to enter the letters i have to put them in sequence. here is my code:

public class MainActivity extends Activity {
ListView list;
ListViewAdapter adapter;
EditText editsearch;
String[] country;
Typeface tf;
ArrayList<WorldPopulation> arraylist = new ArrayList<WorldPopulation>();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listview_main);
    editsearch = (EditText) findViewById(R.id.search);
    list = (ListView) findViewById(R.id.listview);
    tf = Typeface.createFromAsset(getAssets(), "fonts/BKOODB.TTF");

    country = new String[54];
    for (int x = 1; x < 54 + 1; x = x + 1) {
        String this_subject = "mo_" + String.valueOf(x);
        int resID = getResources().getIdentifier(this_subject, "string", getPackageName());
        country[x - 1] = getResources().getString(resID);
    }
    for (int i = 0; i < 54; i++) {
        WorldPopulation wp = new WorldPopulation(country[i]);
        // Binds all strings into an array
        arraylist.add(wp);
    }
    adapter = new ListViewAdapter(this, arraylist);
    list.setAdapter(adapter);
    editsearch.addTextChangedListener(new TextWatcher() {

        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub
            String text = editsearch.getText().toString().toLowerCase(Locale.getDefault());
            adapter.filter(text);
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            // TODO Auto-generated method stub
        }
    });
    list.setOnScrollListener(new OnScrollListener() {

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            hideKeyboard();

        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

        }
    });
    new CountDownTimer(500, 500) {
        @Override
        public void onTick(long millisUntilFinished) {
        }

        @Override
        public void onFinish() {
            hideKeyboard();
        }
    }.start();
}

public class WorldPopulation {

    private String country;

    public WorldPopulation(String country) {

        this.country = country;
    }

    public String getCountry() {
        return this.country;
    }

}

public class ListViewAdapter extends BaseAdapter {

    // Declare Variables
    Context mContext;
    LayoutInflater inflater;
    private List<WorldPopulation> worldpopulationlist = null;
    private ArrayList<WorldPopulation> arraylist;
    Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_in_right);

    public ListViewAdapter(Context context, List<WorldPopulation> worldpopulationlist) {
        mContext = context;
        this.worldpopulationlist = worldpopulationlist;
        inflater = LayoutInflater.from(mContext);
        this.arraylist = new ArrayList<WorldPopulation>();
        this.arraylist.addAll(worldpopulationlist);
    }

    public class ViewHolder {

        TextView country;

    }

    @Override
    public int getCount() {
        return worldpopulationlist.size();
    }

    @Override
    public WorldPopulation getItem(int position) {
        return worldpopulationlist.get(position);
    }

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

    public View getView(final int position, View view, ViewGroup parent) {
        LayoutInflater inflater = getLayoutInflater();
        View row;

        row = inflater.inflate(R.layout.listview_item, parent, false);
        TextView textview = (TextView) row.findViewById(R.id.country);
        ImageView im = (ImageView) row.findViewById(R.id.imageitem);
        im.setImageResource(R.drawable.hair);
        textview.setText(worldpopulationlist.get(position).getCountry());
        textview.setTypeface(tf);
        Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_in_right);
        row.startAnimation(animation);

        row.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // *********************************************************
                // in here it does not send right number to secend activity*
                // *********************************************************
                int orgPos = 0;
                if (country.length != worldpopulationlist.size()) {
                    notifyDataSetChanged();
                    // The list on which we clicked is sorted!
                    String clickedText = worldpopulationlist.get(position).toString();
                    int i1 = 0;
                    boolean found = false;

                    while (i1 < country.length && found == false) {
                        if (clickedText == country[i1]) {
                            orgPos = i1;
                            found = true;
                        } else {
                            i1++;
                        }
                    }

                    Intent i2 = new Intent(mContext, SingleItemView.class);
                    String Subject_number = String.valueOf(orgPos + 1);
                    i2.putExtra("subject_number", Subject_number);
                    startActivity(i2);

                } else {
                    Intent i = new Intent(mContext, SingleItemView.class);
                    String Subject_number = String.valueOf(position + 1);
                    i.putExtra("subject_number", Subject_number);
                    startActivity(i);

                }
            }
        });

        return row;
    }

    // Filter Class
    public void filter(String charText) {
        charText = charText.toLowerCase(Locale.getDefault());
        worldpopulationlist.clear();
        if (charText.length() == 0) {
            worldpopulationlist.addAll(arraylist);
        } else {
            for (final WorldPopulation wp : arraylist) {
                if (wp.getCountry().toLowerCase(Locale.getDefault()).contains(charText)) {
                    worldpopulationlist.add(wp);
                }
            }
        }
        notifyDataSetChanged();
    }

}

private void hideKeyboard() {
    View view = this.getCurrentFocus();
    if (view != null) {
        InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }
}

}

try doing it in afterTextChanged Method because this allows you to enter all the text. and follow this link

    public void afterTextChanged(Editable s) {

    }

Try with custom adapter.

this will help you. Link

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