【发布时间】:2017-09-24 13:08:29
【问题描述】:
我用谷歌搜索了其他解决方案,但找不到任何与我的情况相似的解决方案,起初我试图获取文本视图和过滤器的内容,但没有成功,请帮忙。所以在这里我创建了一个列表视图,它由三个文本视图膨胀,形成一个表格,每个文本视图都是一列,数据是从服务器动态添加的。如何根据用户搜索过滤内容。 这是下面的代码。
MainActivity.java
public class MainActivity extends AppCompatActivity {
private void showJSON2(String response){
String id="";
String name="";
String date_from="";
String date_to="";
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray result = jsonObject.getJSONArray(JSON_ARRAY2);
ListView listView = (ListView) findViewById(R.id.list_view);
list = new ArrayList<HashMap<String, String>>();
for (int i=0; i<result.length(); i++) {
JSONObject notice = result.getJSONObject(i);
id = notice.getString(KEY_ID);
name = notice.getString(KEY_NAME);
date_from = notice.getString(KEY_DATE_FROM);
date_to = notice.getString(KEY_DATE_TO);
HashMap<String, String> temp= new HashMap<String, String>();
temp.put(FIRST_COLUMN, id);
temp.put(SECOND_COLUMN, name);
temp.put(THIRD_COLUMN, date_from);
temp.put(FOURTH_COLUMN, date_to);
list.add(temp);
}
ListViewAdapters adapter = new ListViewAdapters(this, list);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, final View view, int position, long id)
{
int pos=position+1;
Toast.makeText(MainActivity.this, Integer.toString(pos)+" Clicked", Toast.LENGTH_SHORT).show();
}
});
} catch (JSONException e) {
e.printStackTrace();
}
}
}
ListViewAdapters.java -
public class ListViewAdapters extends BaseAdapter{
public ListViewAdapters(Activity activity,ArrayList<HashMap<String, String>> list){
super();
this.activity=activity;
this.list=list;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater=activity.getLayoutInflater();
if(convertView == null){
convertView=inflater.inflate(R.layout.colmn_row, null);
txtFirst=(TextView) convertView.findViewById(R.id.name);
txtSecond=(TextView) convertView.findViewById(R.id.gender);
txtThird=(TextView) convertView.findViewById(R.id.age);
txtFourth=(TextView) convertView.findViewById(R.id.status);
}
HashMap<String, String> map=list.get(position);
txtFirst.setText(map.get(FIRST_COLUMN));
txtSecond.setText(map.get(SECOND_COLUMN));
txtThird.setText(map.get(THIRD_COLUMN));
txtFourth.setText(map.get(FOURTH_COLUMN));
return convertView;
}
}
【问题讨论】:
-
使用 this generic
Filterable适配器(而不是BaseAdapter)作为适配器的基类,并仅覆盖两个方法onBind()和matches() -
你会如何过滤?使用什么值?
-
@npk 用户输入的值。
-
@pskink 你能不能给我一个例子,我很难解决这个问题。谢谢。
-
类似这样的东西:
class YourCustomAdapter extends MatchableArrayAdapter<YourPOJOClass> { ...并覆盖上面的 2 个方法 + 在你的情况下采用ArrayList的构造函数
标签: java android listview filter