我在实现ListView的搜索功能时遇到问题。
基本上,我从远程服务器获取数据,然后将其放入列表中。我认为它可能与ArrayAdapter有关,因为列表项应该有一个id和name而不是一个参数。
以下是涉及的相关文件: ItemListActivity.java
public class ItemListActivity extends ListActivity {
// Progress Dialog
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> itemList;
List<String> items_search = new ArrayList<String>();
ListView lv;
ArrayAdapter<String> adapter;
// url to get all products list
private static String url_all_items = "http://199.0.0.103/pawnshop_2/index.php/android/get_item_list";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_ITEMS = "items";
private static final String TAG_IID = "i_id";
private static final String TAG_NAME = "name";
// products JSONArray
JSONArray items = null;
//int cid;
TextView tvNoItems,tvDebug;
EditText etSearchItem;
//ListView searched;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item_list);
tvNoItems = (TextView) findViewById(R.id.tvNoItems);
etSearchItem = (EditText) findViewById(R.id.etSearchItem);
lv = (ListView) findViewById(android.R.id.list);
tvDebug = (TextView) findViewById(R.id.tvDebug);
Bundle extras = getIntent().getExtras();
// Hashmap for ListView
itemList = new ArrayList<HashMap<String, String>>();
//adapter = new ArrayAdapter<String>(this, R.layout.)
// Loading products in Background Thread
new LoadAllItems().execute();
// Get listview
//lv = getListView();
//final int i=0;
String products[] = {"Diamond And Emerald Necklace","Diamond Drop Earrings","New Item","Pokemon X","Pokemon Y","Heliptile"};
adapter = new ArrayAdapter<String>(this, R.layout.activity_item_detail, R.id.name, products);
// updating listview
//setListAdapter(adapter);
lv.setAdapter(adapter);
// on selecting single product
// launching Edit Product Screen
/*
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String i_id = ((TextView) view.findViewById(R.id.i_id)).getText()
.toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(),
SingleItemActivity.class);
// sending pid to next activity
in.putExtra(TAG_IID, i_id);
// starting new activity and expecting some response back
startActivity(in);
}
});*/
etSearchItem.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
ItemListActivity.this.adapter.getFilter().filter(cs);
String items= "";
for(int i=0; i<items_search.size();i++){
items+=" "+items_search.get(i);
}
tvDebug.setText("Search: "+ items);
}
@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 product by making HTTP Request
* */
class LoadAllItems extends AsyncTask<String, String, String> {
int success = 0;
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(ItemListActivity.this);
pDialog.setMessage("Loading items. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All products from url
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
//params.add(new BasicNameValuePair("c_id",cid+""));
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_items, "POST", params);
Log.d("Items: ", json.toString());
try {
// Checking for SUCCESS TAG
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// transactions found
// Getting Array of Products
items = json.getJSONArray(TAG_ITEMS);
// looping through All Products
for (int i = 0; i < items.length(); i++) {
JSONObject c = items.getJSONObject(i);
// Storing each json item in variable
String i_id = c.getString(TAG_IID);
String name= c.getString(TAG_NAME);
items_search.add(name);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_IID, i_id);
map.put(TAG_NAME, name);
// adding HashList to ArrayList
itemList.add(map);
}
}
} 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 products
pDialog.dismiss();
if (success==0){
tvNoItems.setText("No items found.");
}
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
ItemListActivity.this, itemList,
R.layout.activity_item_detail, new String[] { TAG_IID,
TAG_NAME},
new int[] { R.id.i_id, R.id.name });
// updating listview
setListAdapter(adapter);
}
});
}
}