大家好我需要将arraylist从活动传递到自定义适配器类但是我的代码没有通过...我不知道为什么!
我使用意图来传递数据:
活动:
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 = getIntent();
Arraylist<Integer> email=intent.getIntegerArrayListExtra("a");
而且getIntent()由IDE写成红色......
答案 0 :(得分:1)
这是因为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
}
在您的活动中创建适配器的实例并将其绑定到list / spinner,然后调用该函数以在适配器中添加数据。
答案 1 :(得分:0)
要传递数组,我建议在适配器中创建一个静态数组变量,如下所示:
Arraylist<Integer> variable
然后通过使用传递值
adapterFileName.variable = hm
(其中hm
是一个arrayList,因为它在您开始活动之前的活动中就是这样称呼它。)
答案 2 :(得分:0)
// 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;
}
}