先生,先生,我是Android编程的新手。我有一个任务,我需要从MYSql检索数据,并对我之前检索的数据进行搜索功能。
我的问题是我无法对从MYSql提取到listview的数据进行搜索功能,我试过谷歌,但大多数人已经将这些数据设为默认值。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final ArrayList<String> data = new ArrayList<String>();
data.add("apple");
data.add("banana");
data.add("grape");
data.add("orange");
data.add("pear");
data.add("peach");
data.add("tomato");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
R.layout.item, R.id.itemName, data);
setListAdapter(dataAdapter);
getListView().setTextFilterEnabled(true);
}
这是我提到的代码,可以创建搜索功能
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=(ListView)findViewById(R.id.listview);
editText=(EditText)findViewById(R.id.txtsearch);
initList();
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().equals("")){
// reset listview
initList();
} else {
// perform search
searchItem(s.toString());
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
public void searchItem(String textToSearch){
for(String item:items){
if(!item.contains(textToSearch)){
listItems.remove(item);
}
}
adapter.notifyDataSetChanged();
}
public void initList(){
items=new String[]{"Canada","China","Japan","USA"};
listItems=new ArrayList<>(Arrays.asList(items));
adapter=new ArrayAdapter<String>(this,
R.layout.list_item, R.id.txtitem, listItems);
listView.setAdapter(adapter);
}
这就是我将数据从MYSql提取到listview的方式。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_retrieve_data);
listview = (Listview)findViewById(R.id.ListView1)
new RetrieveTask().execute();
}
@Override
protected Void doInBackground(Void... params) {
try{
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}catch (Exception e){
Toast.makeText(getApplicationContext(), exceptionMessage+", Ex1", Toast.LENGTH_SHORT).show();
}try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
while((line=reader.readLine())!=null){
sb.append(line+"\n");
}
result = sb.toString();
System.out.println("-----JSON Data-----");
System.out.println(result);
is.close();
}catch(Exception e){
Toast.makeText(getApplicationContext(), exceptionMessage+", Ex2", Toast.LENGTH_SHORT).show();
}try{
JSONArray jsonArray = new JSONArray(result);
int totalCount = jsonArray.length();
for(int i=0; i<totalCount; i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
names += jsonObject.getString("name")+":";
emails += jsonObject.getString("email")+":";
phones += jsonObject.getString("phone")+":";
combinedText += (i+1)+". Name '"+jsonObject.getString("name")+"', "+
"Email '"+jsonObject.getString("email")+"', "+
"Phone '"+jsonObject.getString("phone")+"':";
}
}catch (Exception e){
Toast.makeText(getApplicationContext(), exceptionMessage+", Ex3", Toast.LENGTH_SHORT).show();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
name = names.split(":");
email = emails.split(":");
phone = phones.split(":");
combinedArray = combinedText.split(":");
listView.setAdapter(new ArrayAdapter<String>(RetrieveData.this,
android.R.layout.simple_list_item_1, combinedArray));
progressDialog.dismiss();
}
这是数据的输出,但是可以使用&#39; edie&#39;来对列表视图进行过滤。来自我从MYSQL获取的所有数据列表中的EditText
?