嗨,我是新的Android开发人员,想知道如何在点击搜索按钮后对数据进行排序。但在搜索我的数据不是按字母顺序排列。 不知道如何在此搜索活动中对数据进行排序。显示字符串中给出的列表。希望搜索活动按字母顺序显示我的数据。
public class Search extends Activity {
protected static final Object TAG_DISTANCE = null;
// Listview Adapter
ArrayAdapter<String> adapter;
// Search EditText
EditText inputSearch;
// List view
private ListView lv;
// ArrayList for Listview
ArrayList<HashMap<String, String>> productList;
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#6B8E23")));
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
// Listview Data
String products[] ={"Atherosclerosis","Coranary Heart Disease","Stable Angina","Unstable Angina","Myocardial Infarction","Cardiac Risk of Non-Cardiac Surgery","Sinus Rhythms","Atrial Tachyarrhthmias",
"Supraventricular Tachycardias","Ventricular Tachyarrhthmias","Sinoatrial Disease","Atrioventricular and Bundle Branch Block",
"Peripheral Arterial Disease","Aortic Aneurysm","Aortic Dissection","Hypertension",
"Rheumatic Heart Disease","Mitral Stenosis","Mitral Regurgitaion","Mitral Valve Prolapse","Aortic Stenosis","Aortic Regurgitation","Tricuspid Stenosis","Tricuspid Regurgitation","Pulmonary Stenosis","Pulmonary Regurgitation","Infective Endocarditis","Persistent Ductus Arteriosis","Conginital Heart Disease","Diseases of the Myocardium",
"Diseases of the Pericardium"};
lv = (ListView) findViewById(R.id.list_view);
inputSearch = (EditText) findViewById(R.id.inputSearch);
// Adding items to listview
adapter = new ArrayAdapter<String>(this, R.layout.list_items, R.id.searchdisease,products);
lv.setAdapter(adapter);
/**
* Enabling Search Filter
* */
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
Search.this.adapter.getFilter().filter(cs);
}
});
}
}
答案 0 :(得分:1)
试试这个。
Arrays.sort(product);
for(int i = 0; i < products.length; i++)
System.out.println(products[i]);
还
adapter.notifyDataSetChanged();在TextChanged。