如何将数据从活动传递到自定义适配器 [英] how pass data from activity to custom adapter

查看:81
本文介绍了如何将数据从活动传递到自定义适配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们,我需要将数组列表从活动传递到自定义适配器类,但是对于我的代码,它没有传递...我不知道为什么!

hi guys i need to pass arraylist from activity to custom adapter class but with my code it doesn't passed...i don't know why!

我使用意图来传递数据:

i use intent to pass data:

活动:

private void azz() {
    int a=0;
    String status="";
    boolean statuss = false;
    String intenttt= path;

    System.out.println("INTENT:"+intenttt);



    file = new File(intenttt);
    System.out.println("FILE:"+file);

    FileInputStream is = null;
    try {
        is = new FileInputStream(file);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = null;
    try {
        dBuilder = dbFactory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }
    Document doc = null;
    try {
        doc = dBuilder.parse(is);
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    Element element = doc.getDocumentElement();
    element.normalize();

    NodeList nList = doc.getElementsByTagName("checkboxes_pizza");

    for (int i = 0; i < nList.getLength(); i++) {
        Node node = nList.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            Element element2 = (Element) node;
            //tv1.setText(tv1.getText()+"\nName : " + getValue("name", element2)+"\n");
            String id = getValue("id", element2);
            a = Integer.parseInt(id);
            status = getValue("status", element2);

            statuss= Boolean.parseBoolean(status);

            System.out.println("XML:" + a);

            hm.add(a);

            System.out.println("AZZ:" + hm);





        }

    }

    Intent intent = new Intent(ScrollableTabsActivity.this,PlanetAdapter.class) ;
    intent.putIntegerArrayListExtra("a", hm);
    startActivity(intent);


     // ok();


}

自定义适配器以获取意图:

CUSTOM ADAPTER TO GET INTENT:

 Intent intent = getIntent();
        Arraylist<Integer> email=intent.getIntegerArrayListExtra("a");

此外,IDE用红色将getIntent()写成红色...

moreover getIntent() is written in red by IDE...

推荐答案

这是因为,除非在适配器类中声明了名为getIntent()的函数,否则在适配器类中getIntent()不可用.在适配器中添加一个函数以接收适配器中的数据,然后调用notifyDataSetChanged()刷新列表.例如:

That is because getIntent() is not available in adapter class unless you declare a function named getIntent() in it. Add a function in the adapter to receive the data in the adapter and call notifyDataSetChanged() to refresh the list. for example:

class DataAdapter extends ArrayAdapter<String> {

    private ArrayList<String> items = new ArrayList<>();

    public DataAdapter(@NonNull Context context, @LayoutRes int resource) {
        super(context, resource);
    }


    public void setItems(ArrayList<String> items) {
        this.items = items;
        notifyDataSetChanged();
    }
    //some more code
}

在您的活动中创建适配器的实例,并将其绑定到列表/旋转器,然后调用该函数以在适配器中添加数据.

in your activity create an instance of the adapter and bind it to the list/spinner and then call the function to add data in the adapter.

这篇关于如何将数据从活动传递到自定义适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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