此代码为listview
,显示exercises[]
字符串中的项目。您还可以使用EditText
inputSearch和charsequence搜索这些项目,并通过比较字符串来检测选择的列表项目。
我需要的是: 一种使用多个微调器过滤listview(String exercise [])的方法吗?
完整代码:
public class CalExercises extends Activity {
private ListView myListView;
ArrayAdapter<String> myAdapter;
EditText inputSearch;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calexercise);
String exercises[] = {"Wall Pushup", "Knees Pushup", "Regular Pushup", "Pullup"};
myListView = (ListView) findViewById(R.id.exp_list);
inputSearch = (EditText) findViewById(R.id.itemSearch);
myAdapter = new ArrayAdapter<String>(this, R.layout.parent_layout, R.id.parent_txt, exercises);
myListView.setAdapter(myAdapter);
myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
String PosItem = (String) arg0.getItemAtPosition(arg2);
Intent itemIntent = new Intent(getBaseContext(), TempCalEx.class);
String exIntent = "";
if ("Wall Pushup".equals(PosItem))
exIntent = "wallpushup";
else if ("Knees Pushup".equals(PosItem))
exIntent = "kneepushup";
else if ("Regular Pushup".equals(PosItem)) {
exIntent = "regpushup";
Toast.makeText(CalExercises.this, "List Detect Test Success - Pushup", Toast.LENGTH_LONG).show();
} else if ("Pullup".equals(PosItem)) {
exIntent = "pullup";
Toast.makeText(CalExercises.this, "List Detect Test Success - Pullup", Toast.LENGTH_LONG).show();
}
itemIntent.putExtra("exString", exIntent);
startActivity(itemIntent);
;
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
CalExercises.this.myAdapter.getFilter().filter(cs);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
}
@Override
public void afterTextChanged(Editable arg0) {
}
});
}
;
});
}}
答案 0 :(得分:0)
首先,我建议使用此代码而不是复制粘贴;)
String PosItem = (String)arg0.getItemAtPosition(arg2);
Intent itemIntent = new Intent(getBaseContext(), TempCalEx.class);
String exIntent = "";
if ("Wall Pushup".equals(PosItem))
exIntent = "wallpushup";
else if ("Knees Pushup".equals(PosItem))
exIntent = "kneepushup";
else if ("Regular Pushup".equals(PosItem)) {
exIntent = "regpushup";
Toast.makeText(CalExercises.this, "List Detect Test Success - Pushup",Toast.LENGTH_LONG).show();
} else if ("Pullup".equals(PosItem)) {
exIntent = "pullup";
Toast.makeText(CalExercises.this, "List Detect Test Success - Pullup", Toast.LENGTH_LONG).show();
}
itemIntent.putExtra("exString", exIntent);
startActivity(itemIntent);
我没有在编辑器中对此进行测试,但它应该完全相同。它更容易阅读,并且由于使用了else,应该会略微提高性能。
另外,如果你想比较字符串,总是在你知道的对象上调用equals,永远不会为null。
关于你原来的问题:
显示将显示哪些子字符串的方法。
我不知道你想要展示的是什么。是否要过滤列表,或者只是在设置过滤器时对某些点击做出反应?你能详细说明吗?
修改
这应该有用。
// setting up the map, you might have to add more.
Map <String, String[]> map = new Map<String, String[]>();
map.put("normal",new String[]{"Wall Pushup", "Knees Pushup", "Regular Pushup","Pullup"});
map.put("beginner",new String[]{"Knees Pushup","Wall Pushup"});
// this would be the spinner that selects the "normal", "beginner" etc
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
String[] content = map.get(spinner.getSelectedItem().toString());
if (content = null)
myListView.setAdapter(new ArrayAdapter<String>(this, R.layout.parent_layout, R.id.parent_txt, content));
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
}
});
但是你可能不得不向地图添加更多数组,因为我不知道所有可能发生的状态。
并使用正确的String作为键,如果你的微调器显示&#34;初学者&#34;你必须使用put(key,value)作为键的确切文本,否则它将始终为null。我建议使用自定义适配器,因为arrayadapter的选项有限,但这是一个不同的故事。
编辑2:
public class CalExercises extends Activity {
private ListView myListView;
ArrayAdapter<String> myAdapter;
EditText inputSearch;
List<String> exercises;
//TODO set those 2
Spinner spindif;
Spinner spinmus;
String[] array1;
String[] array2;
Map <String, String[]> map1 = new HashMap<String, String[]>();
Map <String, String[]> map2 = new HashMap<String, String[]>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calexercise);
exercises = new ArrayList<String>(Arrays.asList(new String[]{"Wall Pushup", "Knees Pushup", "Regular Pushup", "Pullup"}));
myListView = (ListView) findViewById(R.id.exp_list);
inputSearch = (EditText) findViewById(R.id.itemSearch);
spindif = (Spinner) findViewById((R.id.filterDif));
spinmus = (Spinner) findViewById((R.id.filterMus));
myAdapter = new ArrayAdapter<String>(this, R.layout.parent_layout, R.id.parent_txt, exercises);
myListView.setAdapter(myAdapter);
myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
String PosItem = (String) arg0.getItemAtPosition(arg2);
Intent itemIntent = new Intent(getBaseContext(), TempCalEx.class);
String exIntent = "";
if ("Wall Pushup".equals(PosItem))
exIntent = "wallpushup";
else if ("Knees Pushup".equals(PosItem))
exIntent = "kneepushup";
else if ("Regular Pushup".equals(PosItem)) {
exIntent = "regpushup";
Toast.makeText(CalExercises.this, "List Detect Test Success - Pushup", Toast.LENGTH_LONG).show();
} else if ("Pullup".equals(PosItem)) {
exIntent = "pullup";
Toast.makeText(CalExercises.this, "List Detect Test Success - Pullup", Toast.LENGTH_LONG).show();
}
itemIntent.putExtra("exString", exIntent);
startActivity(itemIntent);
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
CalExercises.this.myAdapter.getFilter().filter(cs);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
}
@Override
public void afterTextChanged(Editable arg0) {
}
});
}
;
});
// this would be the spinner that selects the "normal", "beginner" etc
spindif.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
array1 = map1.get(spinner1.getSelectedItem().toString());
updateList();
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
}
});
spinmus.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
array2 = map2.get(spinner2.getSelectedItem().toString());
updateList();
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
}
});
}
public void updateList() {
List<Integer> aList = Arrays.asList(array1);
List<Integer> bList = Arrays.asList(array2);
aList.retainAll(bList);
exercises = aList;
myAdapter.notifyDataSetChanged();
}
}
所以这应该做到。