【发布时间】:2014-07-08 13:03:23
【问题描述】:
我正在尝试实现一个基于字符串的简单列表视图。我跟着这个教程 http://www.androidhive.info/2012/10/android-multilevel-listview-tutorial/
现在,我想添加一个过滤功能,但没有找到简单的方法。
ListViewAdapter 类提供方法 getfilter() 但不提供 ListAdapter。
你能帮我解决这个问题吗?
问候。
public class AlbumsActivity extends ListActivity {
// Connection detector
ConnectionDetector cd;
// Alert dialog manager
AlertDialogManager alert = new AlertDialogManager();
// Progress Dialog
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jsonParser = new JSONParser();
ArrayList<HashMap<String, String>> albumsList;
// albums JSONArray
JSONArray hotels = null;
// albums JSON url
private static final String URL_HOTELS = "http://10.0.2.2:8888/slim/hotels";
// ALL JSON node names
private static final String TAG_ID = "id";
private static final String TAG_NAME = "hotel";
ListAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_albums);
cd = new ConnectionDetector(getApplicationContext());
// Check for internet connection
if (!cd.isConnectingToInternet()) {
// Internet Connection is not present
alert.showAlertDialog(AlbumsActivity.this, "Internet Connection Error",
"Please connect to working Internet connection", false);
// stop executing code by return
return;
}
// Hashmap for ListView
albumsList = new ArrayList<HashMap<String, String>>();
// Loading Albums JSON in Background Thread
new LoadAlbums().execute();
// get listview
ListView lv = getListView();
/**
* Listview item click listener
* TrackListActivity will be lauched by passing album id
* */
lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int arg2,
long arg3) {
// on selecting a single album
// TrackListActivity will be launched to show tracks inside the album
Intent i = new Intent(getApplicationContext(), SingleTrackActivity.class);
// send album id to tracklist activity to get list of songs under that album
String hotel_id = ((TextView) view.findViewById(R.id.album_id)).getText().toString();
i.putExtra("hotel_id", hotel_id);
startActivity(i);
}
});
TextView inputSearch = (TextView) findViewById(R.id.inputSearch);
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
//AlbumsActivity.this.adapter.getFilter().filter(cs);
Log.d("filter :", cs.toString());
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
}
/**
* Background Async Task to Load all Albums by making http request
* */
class LoadAlbums extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(AlbumsActivity.this);
pDialog.setMessage("Loading hotels ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting Albums JSON
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
String json;
json = jsonParser.makeHttpRequest(URL_HOTELS, "GET",
params);
// Check your log cat for JSON reponse
Log.d("Hotels JSON: ", "> " + json);
try {
hotels = new JSONArray(json);
if (hotels != null) {
// looping through All albums
for (int i = 0; i < hotels.length(); i++) {
JSONObject c = hotels.getJSONObject(i);
// Storing each json item values in variable
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_ID, id);
map.put(TAG_NAME, name);
// adding HashList to ArrayList
albumsList.add(map);
}
}else{
Log.d("Albums: ", "null");
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all albums
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
adapter = new SimpleAdapter(
AlbumsActivity.this, albumsList,
R.layout.list_item_albums, new String[] { TAG_ID,
TAG_NAME }, new int[] {
R.id.album_id, R.id.album_name });
// updating listview
setListAdapter(adapter);
}
});
}
}
}
【问题讨论】:
-
I am trying to implement a simple listview based on strings.看起来你不像教程是针对多级列表视图的。最好试试这个:androidhive.info/2012/09/…
标签: android listview search filter