String[] cities= {"Madrid","Barcelona","Valencia","Seville","Zaragoza","Malaga","Murcia","Palma"}
如何使用edittext过滤此字符串数组。例如,我将在edittext中输入“ma”。然后;字符串数组将像这样过滤(或创建新的字符串数组);
String[] cities= {"Madrid","Malaga","Palma"}
你能帮我吗?感谢。
答案 0 :(得分:1)
Android编程无法更改json,但通过编程,您可以按照自己的意愿进行操作。
首先,您必须将所有JSON值存储到POJO,并且通过RecyclerView或ListView可以获得相同的值。
EditText myTextBox = (EditText) findViewById(R.id.myTextBox);
myTextBox.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
public void onTextChanged(CharSequence s, int start,
int before, int count) {
}
});
答案 1 :(得分:1)
String[] cities= {"Madrid","Barcelona","Valencia","Seville","Zaragoza",
"Malaga","Murcia","Palma"}
String[] resultCities=null;
editTextSearch.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
resultCities = search(editTextSearch.getText().toString())
}
@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
}
});
public String[] search(String txtsearch) {
String arr[]=null;
int pos=0;
for (int i = 0; i < cities.length; i++) {
if(cities[i].contains(txtsearch)){
arr[pos]=cities[i];
pos++;
}
}
return arr;
}
答案 2 :(得分:0)
为此使用AutoCompleteTextView。 例如:
在您的布局xml中:
<AutoCompleteTextView
android:id="@+id/autoCompleteTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView"
android:layout_marginLeft="36dp"
android:layout_marginTop="17dp"
android:ems="10"
android:text="">
<requestFocus />
</AutoCompleteTextView>
并在您的活动/片段中
public class MainActivity extends AppCompatActivity {
String[] cities= {"Madrid","Barcelona","Valencia","Seville","Zaragoza","Malaga","Murcia","Palma"}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Creating the instance of ArrayAdapter containing list of fruit names
ArrayAdapter<String> adapter = new ArrayAdapter<String>
(this, android.R.layout.select_dialog_item, cities);
//Getting the instance of AutoCompleteTextView
AutoCompleteTextView actv = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
actv.setThreshold(1);//will start working from first character
actv.setAdapter(adapter);//setting the adapter data into the AutoCompleteTextView
actv.setTextColor(Color.RED);
}
}
答案 3 :(得分:0)
将数组转换为list并使用它:
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(!s.toString().isEmpty()){
List<String> city = filterCity(clubList,s.toString());
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
public List<String> filterCity(List<String> cities,String searchValue){
List<String> newCity = new ArrayList<>();
for(String city:cities){
if(city.contains(searchValue)){
newCity.add(city);
}
}
return newCity;
}
答案 4 :(得分:0)
如果您使用列表/网格视图,而不是在适配器中应该添加过滤器方法,如果您在此处搜索,可以找到大量答案,我建议在适配器中使用过滤器。