【发布时间】:2017-08-26 04:18:11
【问题描述】:
我正在使用普通方法和凌空 on_response 或 on_error 方法。我的代码是从 sqlite 获取数据并将其添加到数组列表中,并使用此列表创建适配器并将此适配器设置为自动完成文本视图。为此,我在两种方法中都使用了相同的代码行,但在正常方法中,适配器中只有一个元素,但在 volley 方法中,适配器中会有所有元素,我不明白有什么问题。我需要正常的方法来工作
常规方法:
private void prepareMyLists() {
ArrayList<String> arrlist = new ArrayList<String>();
String selectQuerys = "select distinct location from tb_user where location like" +
"'"+autoservice.getText().toString()+"%'"+" or "+"'% "+autoservice.getText().toString()+"%'";
SQLiteDatabase dbs = sq.getReadableDatabase();
Cursor cursors = dbs.rawQuery(selectQuerys, null);
while (cursors.moveToNext())
arrlist.add((cursors.getString(cursors.getColumnIndex("location"))));
adapter1 = new ArrayAdapter<String>(
getBaseContext(),
R.layout.drop_list_item,
arrlist);
autocompletetextview.setAdapter(adapter1);
}
}
截击法:
private void prepareMyList() {
String LOCATION = "http://android.thusharaphotos.com/sear778ch11.php";
StringRequest stringRequest = new StringRequest(Request.Method.POST,LOCATION, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
ArrayList<String> arrlist = new ArrayList<String>();
String selectQuerys = "select distinct location from tb_user where location like" +
"'"+autoservice.getText().toString()+"%'"+" or "+"'% "+autoservice.getText().toString()+"%'";
SQLiteDatabase dbs = sq.getReadableDatabase();
Cursor cursors = dbs.rawQuery(selectQuerys, null);
while (cursors.moveToNext())
arrlist.add((cursors.getString(cursors.getColumnIndex("location"))));
adapter1 = new ArrayAdapter<String>(
getBaseContext(),
R.layout.drop_list_item,
arrlist);
autocompletetextview.setAdapter(adapter1);
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("location_name", autoservice.getText().toString());
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(getBaseContext());
requestQueue.add(stringRequest);
}
点击自动完成文本视图
autocompletetextview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
setLocation((String)parent.getItemAtPosition(position));
}
});
【问题讨论】:
-
sq在哪里定义?
-
在 oncreate 中定义的平方
-
虽然调试时arrlist的大小一样?
-
数组列表和适配器大小相同
-
但是 autocompletetextview 的 onitemClickListner 父级只获得一项意味着 parent.getCount()=1 总是
标签: android arraylist android-arrayadapter autocompletetextview