listview中的绑定项目在片段中

时间:2017-03-02 13:23:27

标签: android android-fragments

我有一个字符串值,我在片段中的数组中动态获取。现在我想在列表视图中动态绑定这些值。以下是我告诉的代码。我该怎么办?

NodeList name;
Element line;
ArrayList<HashMap<String, String>> arrayList = arrayList = new ArrayList<HashMap<String, String>>();

TextView textView;

String i[]=subject_id.split(",");

int a=i.length;

                arrayList.clear();

                for(int j=0;j<a;j++)
                {
                    FetchData fetchData=new FetchData(SERVICE_SUBJECT);
                    fetchData.call(ACTION3,METHOD_FETCH_SUBJECT,OBJECT_NAME_SUBJECT,"bus:Id",i[j]);

                    response = fetchData.getResult();

                    DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
                    InputSource is = new InputSource();
                    is.setCharacterStream(new StringReader(response));

                    Document doc = db.parse(is);
                    NodeList nodes = doc.getElementsByTagName("a:BLSubject");

                    count = String.valueOf(nodes.getLength());

                    for (int k = 0; k < nodes.getLength(); k++) {
                        HashMap<String, String> map = new HashMap<String, String>();
                        Element element = (Element) nodes.item(k);

                        name = element.getElementsByTagName("a:Subject");
                        line = (Element) name.item(0);
                        subject_name = getCharacterDataFromElement(line);
                        System.out.println("Subject:" + subject_name);
                        map.put("Subject", subject_name);

                        name = element.getElementsByTagName("a:Id");
                        line = (Element) name.item(0);
                        subjectid = getCharacterDataFromElement(line);
                        System.out.println("Id:" + subjectid);
                        map.put("Id", subjectid);

                        arrayList.add(map);
                    }

                }

在这里,&#39;我&#39;是包含主题编号的数组名称。

1 个答案:

答案 0 :(得分:0)

您需要使用ArrayAdapter

有一些文章有一些信息:
https://github.com/codepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView

首先像这样创建适配器:

ArrayAdapter<String> itemsAdapter = 
    new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);

其中this是您当前的Activity,第二个参数是带有目标listview的布局,items是包含您数据的列表。

listview映射到itemsAdapter

myListView.setAdapter(itemsAdapter);

items进行任何更改后,请调用itemsAdapter.notifyDataSetChanged()方法。这使listview从修改后的集合中更新。