- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个从 JSON 数组填充的 ListView 。首先从 JSON 数组中提取值,然后将其放入 HashMap 中。然后将这个 HashMap 添加到一个 ArrayList 中。必要的数据从这里添加到 ListAdapter。
我在网上找了好几天,想办法给这个加上搜索功能,但是都没有用ListAdapater、ArrayList和HashMap的这种组合。我是初学者,有人可以帮我做这件事吗?
这是我的代码
public class ListNew extends Activity {
ListView list;
TextView eid;
TextView ename;
TextView edesc;
//Button Btngetdata;
ArrayList<HashMap<String, String>> oslist = new ArrayList<HashMap<String, String>>();
//URL to get JSON Array
private static String url = "somelink";
//JSON Node Names
private static final String TAG_OS = "events";
private static final String TAG_EID = "eid";
private static final String TAG_ENAME = "ename";
private static final String TAG_ETYPE = "etype";
private static final String TAG_EDESC = "edesc";
private static final String TAG_ESDATE = "esdate";
private static final String TAG_EEDATE = "eedate";
private static final String TAG_ESTIME = "estime";
private static final String TAG_EETIME = "eetime";
private static final String TAG_LOCATION = "location";
private static final String TAG_CREATED_AT = "created_at";
private static final String TAG_EDITED_AT = "edited_at";
private static final String TAG_CREATEDBY = "createdby";
private static final String TAG_IMAGE = "image";
private static final String TAG_APPROVED = "approved";
EditText inputSearch;
ListAdapter adapter;
public static final String TABLE_EVENT = "event";
JSONArray events = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//from here
inputSearch = (EditText) findViewById(R.id.etSearch);
setContentView(R.layout.listnew);
oslist = new ArrayList<HashMap<String, String>>();
new JSONParse().execute();
}
private class JSONParse extends AsyncTask<String, String, JSONObject> {
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
eid = (TextView)findViewById(R.id.vers);
ename = (TextView)findViewById(R.id.name);
edesc = (TextView)findViewById(R.id.api);
pDialog = new ProgressDialog(ListNew.this);
pDialog.setMessage("Getting Data ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected JSONObject doInBackground(String... args) {
JSONParser jParser = new JSONParser();
// Getting JSON from URL
JSONObject json = jParser.getJSONFromUrl2(url);
return json;
}
@Override
protected void onPostExecute(JSONObject json) {
pDialog.dismiss();
try {
// Getting JSON Array from URL
events = json.getJSONArray(TAG_OS);
for(int i = 0; i < events.length(); i++){
JSONObject c = events.getJSONObject(i);
// Storing JSON item in a Variable
String eid = c.getString(TAG_EID);
String ename = c.getString(TAG_ENAME);
String etype = c.getString(TAG_ETYPE);
String edesc = c.getString(TAG_EDESC);
String esdate = c.getString(TAG_ESDATE);
String eedate = c.getString(TAG_EEDATE);
String estime = c.getString(TAG_ESTIME);
String eetime = c.getString(TAG_EETIME);
String location = c.getString(TAG_LOCATION);
String created_at = c.getString(TAG_CREATED_AT);
String edited_at = c.getString(TAG_EDITED_AT);
String createdby = c.getString(TAG_CREATEDBY);
String image = c.getString(TAG_IMAGE);
String approved = c.getString(TAG_APPROVED);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_EID, eid);
map.put(TAG_ENAME, ename);
map.put(TAG_ETYPE, etype);
map.put(TAG_EDESC, edesc);
map.put(TAG_ESDATE, esdate);
map.put(TAG_EEDATE, eedate);
map.put(TAG_ESTIME, estime);
map.put(TAG_EETIME, eetime);
map.put(TAG_LOCATION, location);
map.put(TAG_CREATED_AT, created_at);
map.put(TAG_EDITED_AT, edited_at);
map.put(TAG_CREATEDBY, createdby);
map.put(TAG_IMAGE, image);
map.put(TAG_APPROVED, approved);
oslist.add(map);
list=(ListView)findViewById(R.id.list);
TextView emptyText = (TextView)findViewById(R.id.empty);
list.setEmptyView(emptyText);
//Log.d("TAG_is d first one", TAG_APPROVED + "," + approved);
adapter = new SimpleAdapter(ListNew.this, oslist,
R.layout.list_v,
new String[] { TAG_ENAME,TAG_CREATEDBY, TAG_CREATED_AT }, new int[] {
R.id.vers,R.id.name, R.id.api});
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent i = new Intent(ListNew.this, NewEvent.class);
i.putExtra("eid", oslist.get(+position).get("eid") );
i.putExtra("ename", oslist.get(+position).get("ename") );
i.putExtra("etype", oslist.get(+position).get("etype") );
i.putExtra("edesc", oslist.get(+position).get("edesc") );
i.putExtra("esdate", oslist.get(+position).get("esdate") );
i.putExtra("eedate", oslist.get(+position).get("eedate") );
i.putExtra("estime", oslist.get(+position).get("estime") );
i.putExtra("eetime", oslist.get(+position).get("eetime") );
i.putExtra("location", oslist.get(+position).get("location") );
i.putExtra("created_at", oslist.get(+position).get("created_at") );
i.putExtra("edited_at", oslist.get(+position).get("edited_at") );
i.putExtra("createdby", oslist.get(+position).get("createdby") );
i.putExtra("image", byteArray);
i.putExtra("approved", oslist.get(+position).get("approved") );
startActivity(i);
}
});
}//end if
} catch (JSONException e) {
e.printStackTrace();
}
}
}
最佳答案
根据您的描述,我相信您有针对 ListView 的自定义适配器实现。如果是自定义适配器,则需要通过实现来创建自己的过滤器 http://developer.android.com/reference/android/widget/Filterable.html
或
http://developer.android.com/reference/android/widget/Filter.html
下面是获取灵感的简单教程
http://www.survivingwithandroid.com/2012/10/android-listview-custom-filter-and.html
========== 简单搜索示例 ===
您需要在 ListView 顶部创建一个编辑文本
<EditText
android:id="@+id/editTxt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:maxLines="1" />
然后将文本更改监听器应用于此编辑文本
editTxt.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
adapter.getFilter().filter(s.toString());
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
你就完成了。
关于android - 如何实现对使用 ListAdapater、ArrayList 和 HashMap 的 ListView 的搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30283992/
我有一个从 JSON 数组填充的 ListView 。首先从 JSON 数组中提取值,然后将其放入 HashMap 中。然后将这个 HashMap 添加到一个 ArrayList 中。必要的数据从这里
我是一名优秀的程序员,十分优秀!