在android中搜索功能?

时间:2013-02-11 04:59:16

标签: android android-listview android-searchmanager

我的阵列就像{“三星Tab”,“三星Note”,“三星Galaxy”,“三星Galaxy Pro”,“诺基亚Lumia”,“诺基亚5130”,“索尼Xperia”}等等。编辑文本类型 GALAXY ,然后点击我想要在下一个活动列表视图中仅显示 Samsung Galaxy,Samsung Galaxy Pro 的按钮 GO 。任何人都知道请帮帮我。

2 个答案:

答案 0 :(得分:2)

有一些方法可以做到这一点,这是一种方法,你可以创建一个类似下面的自定义方法。

 public ArrayList<String> getSearchedItems(String searchString){
 ArrayList<String>  list = new ArrayList<String>();

 for(int i = 0; i<array.length ;i++) { // array is the String array or list which contains all the Phone model names you want to search in.
  if((array[i].toLowerCase()).contains(searchString.toLowerCase())) { // contains method will check if user enterred string is available in your Model names
    list.add(array[i]);
    }
 }
 return list; // list of strings/names containing search String.
}

按下go按钮或从下一个Activity调用此方法。获取名单。

答案 1 :(得分:0)

试试这个,

  public class MainActivity extends Activity {

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

private EditText searchEdt;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    // making it full screen
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_ugsimply_beta);
    list.add("Samsung Tab");
    list.add("Samsung Note");
    list.add("Nokia Lumia");
    list.add("Nokia 5130");
    list.add("Sony Xperia");
    searchEdt = (EditText) findViewById(R.id.searchEdt);
    searchEdt.addTextChangedListener(new TextWatcher() {

        private int textlength;
        private ArrayList<String> arrayList_sort = new ArrayList<String>();

        public void afterTextChanged(Editable s) {
            // Abstract Method of TextWatcher Interface.
        }

        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // Abstract Method of TextWatcher Interface.
        }

        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            textlength = searchEdt.getText().length();
            if (arrayList_sort != null)
                arrayList_sort.clear();

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

                if (textlength <= list.get(i).toString().length()) {

                    if (searchEdt
                            .getText()
                            .toString()
                            .equalsIgnoreCase(
                                    (String) list.get(i).toString()
                                            .subSequence(0, textlength))) {
                        arrayList_sort.add(list.get(i));
                        Log.d("TAG", "log" + arrayList_sort.size());
                    }
                }
            }

        }
    });

}

}

您将获得搜索项目arrayList_sort。将此数组列表放到另一个Activity

Intent i = new Intent(MainActivity.this,NextActivity.class);

i.putStringArrayListExtra("arraylist", arrayList_sort);
startActivity(i);