1

我的阵列就像 {"Samsung Tab","Samsung Note","Samsung Galaxy","Samsung Galaxy Pro","Nokia Lumia","Nokia 5130","Sony Xperia"} 之类的东西。我有编辑文本键入GALAXY并单击按钮我有GO我只想在下一个活动列表视图中显示三星 Galaxy 、三星 Galaxy Pro 。任何知道的请帮助我。

4

2 回答 2

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 按钮或从下一个活动中调用此方法。获取名称列表。

于 2013-02-11T05:04:44.010 回答
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);
于 2013-02-11T05:52:32.040 回答