【问题标题】:Class extending fragment and listview类扩展片段和列表视图
【发布时间】:2013-01-08 11:35:05
【问题描述】:

我正在尝试使用片段创建一个选项卡,该选项卡将在解析 xml 页面后显示条目列表。但是我无法在选项卡类中扩展listactivity,因为它已经扩展了片段。我尝试实现listfragments,但它在ListAdapter adapter = new SimpleAdapter 上给我一个错误,说“构造函数SimpleAdapter(Protab1,ArrayList>,int , String[], int[]) is undefined"。你能帮帮我吗。

package com.example.pro1;
import java.util.ArrayList;
import java.util.HashMap;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class Protab1 extends ListFragment {
    // All static variables
        static final String URL = "http://api.androidhive.info/pizza/?format=xml";
        // XML node keys
        static final String KEY_ITEM = "item"; // parent node
        static final String KEY_ID = "id";
        static final String KEY_NAME = "name";
        static final String KEY_COST = "cost";
        static final String KEY_DESC = "description";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        return inflater.inflate(R.layout.protab1, container, false);
        ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();

        XMLParser parser = new XMLParser();
        String xml = parser.getXmlFromUrl(URL); // getting XML
        Document doc = parser.getDomElement(xml); // getting DOM element

        NodeList nl = doc.getElementsByTagName(KEY_ITEM);
        // looping through all item nodes <item>
        for (int i = 0; i < nl.getLength(); i++) {
            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();
            Element e = (Element) nl.item(i);
            // adding each child node to HashMap key => value
            map.put(KEY_ID, parser.getValue(e, KEY_ID));
            map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
            map.put(KEY_COST, "Rs." + parser.getValue(e, KEY_COST));
            map.put(KEY_DESC, parser.getValue(e, KEY_DESC));

            // adding HashList to ArrayList
            menuItems.add(map);
        }

        // Adding menuItems to ListView
        ListAdapter adapter = new SimpleAdapter(this, menuItems,
                R.layout.list_item,
                new String[] { KEY_NAME, KEY_DESC, KEY_COST }, new int[] {
                        R.id.name, R.id.desciption, R.id.cost });

        setListAdapter(adapter);

        }

}

【问题讨论】:

  • 嗨,谁能帮我解决这个问题。我是安卓新手。

标签: android android-fragments listactivity android-listfragment


【解决方案1】:

尝试使用this.getActivity() 而不是this 作为 SimpleAdapter 的第一个参数。第一个参数接受一个 Context 对象,但由于 ListFragment(或任何 Fragment)没有扩展 Context,因此它不起作用。

【讨论】:

  • 嗨尼克,非常感谢。这有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-01
  • 1970-01-01
  • 2023-02-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多