将数据传递到Android中的寻呼机适配器内的片段时出现问题

时间:2012-10-05 08:25:46

标签: java android android-fragments android-fragmentactivity

这是我的视图寻呼机适配器来加载片段。在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;
    }     

 }

2 个答案:

答案 0 :(得分:1)

问题是你只有一个HashMap。即使您不断更改HashMap中的数据,列表productItems也只包含对单个HashMap的10个引用。所以最后,它将包含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;    
}

答案 1 :(得分: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;    
}