0

这是我用于加载片段的视图寻呼机适配器。在 getItem() 方法中,我尝试使用传递给该函数的索引动态插入值。我在 productData 中有所有数据。但它总是获取

 productData.get(0).get("name")

进入所有的片段。索引值被正确地传递到片段中。我验证过了。而且productData里面的数据也是正确的。这里的问题在哪里?

  public class ProductViewPagerAdapter extends FragmentPagerAdapter {
    public ProductViewPagerAdapter(FragmentManager fm) {
        super(fm);
    }
    @Override
    public Fragment getItem(int i) {            
        Fragment fragment = new ProductViewFragment();
        Bundle args = new Bundle();
        args.putInt("number", i + 1);
        args.putString("name", productData.get(i).get("name"));
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getCount() {
        return 10;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0: return "test1";
            case 1: return "2";
            case 2: return "3";
            case 3: return "4";
        }
        return null;
    }     

 }
4

2 回答 2

1

问题是您只有一个HashMap. 即使您不断更改 中的数据HashMap,该列表productItems也只包含 10 个对单个 . 的引用HashMap。所以最后,它将包含最终数据集的 10 个副本。

您想要做的是HashMap为每个循环迭代创建一个新的。这是修改后的代码:

public ArrayList<HashMap<String,String>> getData(AsyncTask<Void, Void, Void> asynctask, String query) throws URISyntaxException
{
    api_url += query;
    //  Log.v("API url",api_url);
    XMLParser parser = new XMLParser();
    String xml = parser.getXmlFromUrl(api_url); // getting XML
    Document doc = parser.getDomElement(xml); // getting DOM element
    NodeList nl = doc.getElementsByTagName(KEY_PRODUCT);
    ArrayList<HashMap<String, String>> productItems = new ArrayList<HashMap<String, String>>();
    for (int i = 0; i < 10; i++) {
        Element e = (Element) nl.item(i);           
        // adding each child node to HashMap key => value
        HashMap<String, String> map = new HashMap<String, String>();
        map.put(KEY_PROD_NAME, e.getAttribute(KEY_PROD_NAME));
        map.put(KEY_ID, e.getAttribute(KEY_MER_ID));
        map.put(KEY_PRICE, e.getAttribute(KEY_MER_PRICE));      
        Log.v("fsd:",e.getAttribute(KEY_PROD_NAME));
        // adding HashList to ArrayList
        productItems.add(map);      
    }       
    return productItems;    
}
于 2012-10-05T09:32:05.750 回答
0

就是这样,

public ArrayList<HashMap<String, String>> productItems = new ArrayList<HashMap<String, String>>();
public HashMap<String, String> map = new HashMap<String, String>();

public ArrayList<HashMap<String,String>> getData(AsyncTask<Void, Void, Void> asynctask, String query) throws URISyntaxException
{   
        api_url += query;
        //  Log.v("API url",api_url);
        XMLParser parser = new XMLParser();
        String xml = parser.getXmlFromUrl(api_url); // getting XML
        Document doc = parser.getDomElement(xml); // getting DOM element
        NodeList nl = doc.getElementsByTagName(KEY_PRODUCT);
    for (int i = 0; i < 10; i++) {          
        Element e = (Element) nl.item(i);           
        // adding each child node to HashMap key => value
        map.put(KEY_PROD_NAME, e.getAttribute(KEY_PROD_NAME));
        map.put(KEY_ID, e.getAttribute(KEY_MER_ID));
        map.put(KEY_PRICE, e.getAttribute(KEY_MER_PRICE));      
        Log.v("fsd:",e.getAttribute(KEY_PROD_NAME));
        // adding HashList to ArrayList
        productItems.add(map);      
    }       
    return productItems;    
}
于 2012-10-05T09:15:40.490 回答