Java 过滤从JSON生成的ListView
我有一个活动,它使用JSON和volley存储库从Java 过滤从JSON生成的ListView,java,android,json,listview,Java,Android,Json,Listview,我有一个活动,它使用JSON和volley存储库从MySQL数据库检索数据,然后将其显示在列表视图中 以下是显示列表视图的活动: SearchDestActivity.java public class SearchDestActivity extends AppCompatActivity implements View.OnClickListener { public static final String JSON_URL = "http://iwander.pe.hu/sample/s
MySQL
数据库检索数据,然后将其显示在列表视图中
以下是显示列表视图的活动:
SearchDestActivity.java
public class SearchDestActivity extends AppCompatActivity implements View.OnClickListener {
public static final String JSON_URL = "http://iwander.pe.hu/sample/sample.php";
private Button buttonGet;
private ListView listView;
CustomList cl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_dest);
sendRequest();
buttonGet = (Button) findViewById(R.id.buttonGet);
buttonGet.setOnClickListener(this);
listView = (ListView) findViewById(R.id.listView2);
}
private void sendRequest(){
StringRequest stringRequest = new StringRequest(JSON_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
showJSON(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(SearchDestActivity.this,error.getMessage(),Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
private void showJSON(String json){
ParseJSON pj = new ParseJSON(json);
pj.parseJSON();
CustomList cl = new CustomList(this, ParseJSON.ids,ParseJSON.names,ParseJSON.emails);
listView.setAdapter(cl);
}
@Override
public void onClick(View v) {
}
public class CustomList extends ArrayAdapter<String> {
private String[] ids;
private String[] names;
private String[] emails;
private Activity context;
public CustomList(Activity context, String[] ids, String[] names, String[] emails) {
super(context, R.layout.list_view_layout, ids);
this.context = context;
this.ids = ids;
this.names = names;
this.emails = emails;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View listViewItem = inflater.inflate(R.layout.list_view_layout, null, true);
TextView textViewId = (TextView) listViewItem.findViewById(R.id.route);
TextView textViewName = (TextView) listViewItem.findViewById(R.id.busName);
TextView textViewEmail = (TextView) listViewItem.findViewById(R.id.terminal);
textViewId.setText(ids[position]);
textViewName.setText(names[position]);
textViewEmail.setText(emails[position]);
return listViewItem;
}
}
我有一个editText
在ListView
的顶部,我想用输入的文本对其进行过滤。我试着在我的代码中添加以下内容:
listView.setTextFilterEnabled(true);
EditText myFilter = (EditText) findViewById(R.id.dest);
myFilter.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) {
cl.getFilter().filter(s.toString());
}
});
但当我尝试输入文本时,应用程序崩溃了。logcat显示:
仅供参考,以下是结果的屏幕上限:
editText
中的文本应与红色字体颜色的文本相匹配
编辑
由于上一个错误的原因是cl
没有被全球化,所以我修复了这个问题,并且过滤工作正常。但另一个问题是,它不能像下面这样显示正确的过滤结果:
在您的onTextChanged
方法上,您将获得一个NullPointerException
cl
对象为空,因为showJSON
创建了一个局部变量CustomList cl
。它必须是全局的。您从不初始化cl字段,因为当您执行CustomList cl=new Custom。。。在showJSON()方法中,您正在创建一个局部变量。相反,您应该编写cl=newcustom。。。感谢您的回复,是的,您是对的。我改变了那个部分,不知怎的应用程序没有崩溃,并且能够显示结果。问题是,每当我在editText上键入时,它都会过滤,但它与红色的文本不匹配。这种行为的发生是因为您设置适配器的方式。当前流程:输入搜索文本->适配器根据该文本进行筛选,最终得到一个元素匹配(就像图像中的示例)->在getView()方法中,根据位置绑定未筛选初始列表中的数据。因此,如果您得到一个匹配项,您将得到列表中的第一个元素(位置0),如果您得到两个匹配项,您将得到两个第一个元素(位置0和1),等等。实现您自己的适配器(以及过滤),ArrayAdapter适用于非常基本的场景。@Luksprog您有这样的示例吗?或者我可以基于的任何项目?这样做了,尝试了它,它运行了,但过滤结果不正确。我将编辑我的问题。