简体   繁体   中英

how pass data from activity to custom adapter

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:

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


}

CUSTOM ADAPTER TO GET INTENT:

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

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

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.

To pass an array I would suggest to create a static array variable in your adapter like this: Arraylist<Integer> variable
and then pass the value by using adapterFileName.variable = hm (where hm is an arrayList as it looks you call it like that) in the activity just before you start activity.

  // In activity pass Data to Your adapter like DefaulterAdapter 
  // (mContext,modelDefulterLists,schemeId1)

  class DefaulterAdapter extends BaseAdapter
  {

   //init here your data
   String schemeId1;
   Context mContext;
   ArrayList<ModelDefulterList> modelDefulterLists;

    // in adapter i get data from custructer 
    public DefaulterAdapter(Context mContext, ArrayList<String> 
    modelDefulterLists, String schemeId1)
    {
      this.mContext = mContext;
      this.modelDefulterLists = modelDefulterLists;
      prefsHelper = new PrefsHelper(mContext);
     this.schemeId1 = schemeId1;
    }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM