java - 如何将数据从 Activity 传递到自定义适配器

标签 java android android-activity

大家好,我需要将 arraylist 从 Activity 传递到自定义适配器类,但使用我的代码它没有通过......我不知道为什么!

我使用 Intent 来传递数据:

Activity :

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


}

获取 Intent 的自定义适配器:

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

而且getIntent()是IDE用红色写的...

最佳答案

这是因为 getIntent() 在适配器类中不可用,除非您在其中声明一个名为 getIntent() 的函数。在适配器中添加一个函数来接收适配器中的数据并调用notifyDataSetChanged()来刷新列表。例如:

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
}

在您的 Activity 中创建适配器的实例并将其绑定(bind)到列表/微调器,然后调用该函数以在适配器中添加数据。

关于java - 如何将数据从 Activity 传递到自定义适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44405742/

相关文章:

java - 从类内部调用 Activity

java - 如何在Java [Ubuntu平台]中重命名文件?

java - 断言对象是一个有效的顶级 json 可序列化

java - 图像在 ListView 中消失

android - 强制我的 Activity 前景?

Android:添加单页标题条栏

android - 如何将 Activity 分配为选项卡内容?

java - AnyLogic:狄利克雷分布的实现?

Windows 7 和 gnuplot 4.6 下的 Javaplot - 消失的结果

java - 如何获取Json数据