【问题标题】:Android Adding extra item on listviewAndroid在listview上添加额外的项目
【发布时间】: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);

【问题讨论】:

    标签: android listview


    【解决方案1】:

    只需调用以下命令即可添加更多新项目:

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

    【讨论】:

    • 嗨,瓦卡斯。我在 setListAdapter(adapter) 之后添加了这两行。但我收到错误消息 对于适配器类型 ListAdapter 的方法 add(String)/notifyDataSetChanged() 未定义 用于适配器。_add_ 和 notifyDataSetChanged();
      请帮帮我。谢谢
    • 我无法启动项目,因为它是语法错误。 add () 和 notifyDataSetChanged() 上的红色下划线
    • 哦,我明白了。检查我的新更新答案。我做了一些更改,例如 SimpleAdaptercateList。看看它是否有效:)
    • add() 上仍有红色下划线,但消息不同。 The method add(HashMap&lt;String,String&gt;) in the type ArrayList&lt;HashMap&lt;String,String&gt;&gt; is not applicable for the arguments (String)
    • 哎呀,对不起,我的错误:(我也没有在我的日食上验证它。尝试使用新的更新答案
    【解决方案2】:

    您可以调用 notifyDatasetChanged() 函数,该函数使用更新的值在列表视图上调用重绘。

    您还可以做的是让您的 listView 和 listadapter 成员可验证,然后创建一个函数,将新适配器分配给 listview

    private SimpleAdapter adapter;
    private ListView listView;
    
    private void update()
    {
    adapter = new SimpleAdapter(this, cateList, 
                    R.layout.list_cate, 
                    new String[] {TAG_CATEGORIES }, new int[] { 
                            R.id.categories});
    listView.setAdapter(adapter);
    }
    

    【讨论】:

    • 它是 = 覆盖 notifyDataSetChanged 函数,有时调用 notifyDataSetChanged() 不适用于特殊情况,覆盖 notifyDataSetChanged 并实现自定义函数是理想的。
    • 如果简单的方法行得通,那我们为什么要重新创建新的适配器?
    • 因为有时它不会,正如我所说,尝试 notifyDataSetChanged() 如果这不起作用,请使用提供的“Sledgehammer”
    【解决方案3】:

    解析Json的时候多加一项

    map.put("ALL", All);
    cateList.add(map);
    

    将上述一项静态项添加到您的列表后(始终为“全部”)

    现在您可以通过适配器在列表视图中显示。

    【讨论】:

    • 打电话给adapter.notifyDataSetChanged();怎么样?
    • TAG_CATEGORIES 在 map 中的 key 以供适配器读取其对应值呢?
    猜你喜欢
    • 1970-01-01
    • 2020-05-14
    • 2013-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-09
    相关资源
    最近更新 更多