Reputation: 4092
My array is like {"Samsung Tab","Samsung Note","Samsung Galaxy","Samsung Galaxy Pro","Nokia Lumia","Nokia 5130","Sony Xperia"} some thing like that.i have edit text type GALAXY and click the button i have GO i want to show only Samsung Galaxy , Samsung Galaxy Pro in next activity list view.any know please help me.
Upvotes: 1
Views: 1446
Reputation: 27549
There are some ways to do it, Here is one way ,you can create a custom method something like below.
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.
}
Call this method in your go button press, or from next Activity. to get list of names.
Upvotes: 2
Reputation: 3259
try this,
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());
}
}
}
}
});
}
}
You will get searched items arrayList_sort
. Put this array list to another Activity
Intent i = new Intent(MainActivity.this,NextActivity.class);
i.putStringArrayListExtra("arraylist", arrayList_sort);
startActivity(i);
Upvotes: 0