在列表视图的Andr​​oid增加额外项目 [英] Android Adding extra item on listview

查看:112
本文介绍了在列表视图的Andr​​oid增加额外项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会想添加一个额外的项目我的列表视图。现在,我设法列出所有从PHP类别,但我会想多加一个全部类别列表视图。例如:


  

所有结果民以食为天结果饮料结果糕点

我会怎么做?

CategoryActivity.java

  @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);        //为哈希映射的ListView
        ArrayList的<&HashMap的LT;字符串,字符串>> cateList =新的ArrayList<&HashMap的LT;字符串,字符串>>();
        //创建JSON解析器实例
        JSONParserList jParser =新JSONParserList();
        // URL从获取JSON字符串
        JSONObject的JSON = jParser.getJSONFromUrl(URL);        尝试{
            //获取类别的数组            catelist = json.getJSONArray(TAG_CATELIST);            //通过所有类别循环
            的for(int i = 0; I< catelist.length();我++){
                JSONObject的C = catelist.getJSONObject(I)             //存储在变量中的每个JSON项目                串类= c.getString(TAG_CATEGORIES);                HashMap的<字符串,字符串>地图=新的HashMap<字符串,字符串>();                map.put(TAG_CATEGORIES,类别);                cateList.add(地图);
            }        }赶上(JSONException E){
            e.printStackTrace();
        }
        / **
         *更新解析JSON数据到ListView控件
         * * /
        ListAdapter适配器=新SimpleAdapter(这一点,cateList,
                R.layout.list_cate,
                新的String [] {} TAG_CATEGORIES,新的INT [] {
                        R.id.categories});
        setListAdapter(适配器);
 }

categories.php

  $查询=SELECT类别的店铺GROUP BY类别';
    $解析度=的mysql_query($查询)或死亡(mysql_error());    $ catelist ['catelist'] =阵列();
    而($输出= mysql_fetch_assoc($水库)){
    $ catelist ['catelist'] [] = $输出;
    }
    回声json_en code($ catelist);


解决方案

只需调用下面添加更多新的项目(S):

  SimpleAdapter适配器=新SimpleAdapter(这一点,cateList,R.layout.list_cate,
                新的String [] {} TAG_CATEGORIES,新的INT [] {R.id.categories});setListAdapter(适配器);
//添加列表中的多个新项目
HashMap的<字符串,字符串>地图=新的HashMap<字符串,字符串>();
map.put(TAG_CATEGORIES,全部);
cateList.add(地图);//清爽列表的内容以显示新添加的项目
adapter.notifyDataSetChanged();

I will like to add an extra item for my listview. For now i manage to list all of the categories from php but i will like to add one more "All" categories to the listview. For example:

"All"
"Food"
"Beverage"
"Pastry"

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();

这篇关于在列表视图的Andr​​oid增加额外项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆