本文介绍了在列表视图的Android增加额外项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我会想添加一个额外的项目我的列表视图。现在,我设法列出所有从PHP类别,但我会想多加一个全部类别列表视图。例如:
How will i do that??
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);
解决方案
simply call the following to add more new item(s):
SimpleAdapter adapter = new SimpleAdapter(this, cateList, R.layout.list_cate,
new String[] {TAG_CATEGORIES }, new int[] {R.id.categories});
setListAdapter(adapter);
//to add more new items in list
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_CATEGORIES, "All");
cateList.add(map);
//refreshing the contents of list to show newly added items
adapter.notifyDataSetChanged();
这篇关于在列表视图的Android增加额外项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!