【发布时间】:2012-03-16 07:40:21
【问题描述】:
我想为我的列表视图添加一个额外的项目。现在我设法列出了 php 中的所有类别,但我想在列表视图中再添加一个“所有”类别。例如:
“全部”
“食品”
“饮料”
“糕点”
我该怎么做??
CategoryActivity.java
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Hashmap for ListView
ArrayList<HashMap<String, String>> cateList = new ArrayList<HashMap<String, String>>();
// Creating JSON Parser instance
JSONParserList jParser = new JSONParserList();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
try {
// Getting Array of Categories
catelist = json.getJSONArray(TAG_CATELIST);
// looping through All Categories
for(int i = 0; i < catelist.length(); i++){
JSONObject c = catelist.getJSONObject(i);
// Storing each json item in variable
String categories = c.getString(TAG_CATEGORIES);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_CATEGORIES, categories);
cateList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(this, cateList,
R.layout.list_cate,
new String[] {TAG_CATEGORIES }, new int[] {
R.id.categories});
setListAdapter(adapter);
}
categories.php
$query = 'SELECT categories FROM shop GROUP BY categories';
$res = mysql_query($query) or die(mysql_error());
$catelist['catelist'] = array();
while ($output = mysql_fetch_assoc ($res)) {
$catelist['catelist'][]=$output;
}
echo json_encode($catelist);
【问题讨论】: