如何从 xml 将数据动态添加到微调器中

发布于 2024-12-07 05:14:43 字数 1659 浏览 7 评论 0原文

你好,我正在下载 XML 并解析数据。我想向微调器添加数据。每次运行应用程序时数据都会更新。

public class Main extends ListActivity {
    TextView valueTextView;
    HashMap<String, String> name=null;
    private HashMap<String, String> array_spinner[];

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main)
        ArrayList<HashMap<String, String>>mylist = new ArrayList<HashMap<String, String>>();
        String xml = XMLfunctions.getXML();
        Document doc = XMLfunctions.XMLfromString(xml);
        NodeList nodes = doc.getElementsByTagName("Table");
        Toast.makeText(Main.this, "ID '" + nodes.getLength(),Toast.LENGTH_LONG).show();     
        for (int i = 0; i < nodes.getLength(); i++) {
           HashMap<String, String> map = new HashMap<String, String>(); 
           Element e = (Element)nodes.item(i);
           map.put("id", XMLfunctions.getValue(e, "id"));
           map.put("name", "Name:" + XMLfunctions.getValue(e, "name"));
           map.put("Score", "Score: " + XMLfunctions.getValue(e, "score"));
           mylist.add(map);         
        }

    valueTextView = (TextView)findViewById(R.id.selected);
    Spinner s = (Spinner)findViewById(R.id.spinner);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
}

它的代码不完整我不知道如何应用 SpinnerAdapter 请任何人帮助我

谢谢

Abhishek

Hello I'm downloading XML and parsing data. I want to add data to the spinner. The data updates every time I run the application.

public class Main extends ListActivity {
    TextView valueTextView;
    HashMap<String, String> name=null;
    private HashMap<String, String> array_spinner[];

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main)
        ArrayList<HashMap<String, String>>mylist = new ArrayList<HashMap<String, String>>();
        String xml = XMLfunctions.getXML();
        Document doc = XMLfunctions.XMLfromString(xml);
        NodeList nodes = doc.getElementsByTagName("Table");
        Toast.makeText(Main.this, "ID '" + nodes.getLength(),Toast.LENGTH_LONG).show();     
        for (int i = 0; i < nodes.getLength(); i++) {
           HashMap<String, String> map = new HashMap<String, String>(); 
           Element e = (Element)nodes.item(i);
           map.put("id", XMLfunctions.getValue(e, "id"));
           map.put("name", "Name:" + XMLfunctions.getValue(e, "name"));
           map.put("Score", "Score: " + XMLfunctions.getValue(e, "score"));
           mylist.add(map);         
        }

    valueTextView = (TextView)findViewById(R.id.selected);
    Spinner s = (Spinner)findViewById(R.id.spinner);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
}

Its incomplete code I don't know how to apply SpinnerAdapter Please anyone help me

Thank you

Abhishek

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人生戏 2024-12-14 05:14:43

我会采用你的哈希图并创建一个数组,而不知道你希望你的微调器如何工作,我会结合名称和分数。然后像这样调用适配器:

String[] nameScore = (xml name score data in string array)
ArrayAdapter adapter= new ArrayAdapter(this, android.R.layout.simple_spinner_item, nameScore);
s.setAdapter(adapter);

任何比这更复杂的事情都必须创建一个自定义适配器。

Answer:::

这就是你要做的。创建一个名为 NameData 的类,然后设置包含 ID、名称和分数的属性。

public class NameData {
    public int id;
    public String name;
    public int score;

    public NameData(int i, String n, int s) {
        this.id = i;
        this.name = n;
        this.score = s;
    }
}

接下来创建一个方法来连接到您的数据解析它并将每个项目放入此 NameData 对象中

public List<NameData> getNameData() {
    List<NameData> list = new LinkedList<NameData>();

    //get data from url and parse it to your namedata object
    // /.....for loop (psuedo coding here...
        list.add(new NameData(id, name, score));
    // end for loop
    return list;
}

然后您将需要创建一个自定义列表适配器,该适配器使用您为行设计的布局。

private class ItemsAdapter extends BaseAdapter {
    NameData[] items;

    public ItemsAdapter(Context context, int textViewResourceId,   NameData[] md) {
        this.items = md;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        TextView text1;
        View view = convertView;
        if (view == null) {
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = vi.inflate(R.layout.yourRowForListlayout, null);
        }
        text1 = (TextView) view.findViewById(R.id.yourRowForListlayoutTextView);
        text1.setText("" + (position+1));
        return view;
    }

    @Override
    public int getCount() {
        return this.items.length;
    }

    @SuppressWarnings("boxing")
    @Override
    public Object getItem(int p) {
        return p;
    }

    @Override
    public long getItemId(int p) {
        return p;
    }
}

:然后在您的创建代码中您可以获取此列表并将其直接添加到适配器中:

List<NameData> list = getNameData();
adapter = new ItemsAdapter(this, R.layout.yourRowForListlayout, list.toArray(new NameData[list.size()]) );
setAdapter(adapter);

这就是我对自定义列表执行此操作的方式。

I would take your Hashmap and create an Array instead, without knowing how you want your Spinner to work I would combine Name and Score. Then make the call to the adapter like this:

String[] nameScore = (xml name score data in string array)
ArrayAdapter adapter= new ArrayAdapter(this, android.R.layout.simple_spinner_item, nameScore);
s.setAdapter(adapter);

Anything more complex than that then you will have to make a custom adapter.

Answer:::

Here is what you do. Create a Class called NameData then set properties with ID, name and score.

public class NameData {
    public int id;
    public String name;
    public int score;

    public NameData(int i, String n, int s) {
        this.id = i;
        this.name = n;
        this.score = s;
    }
}

Next create a method to connect to your data parse it and put each item into this NameData Object

public List<NameData> getNameData() {
    List<NameData> list = new LinkedList<NameData>();

    //get data from url and parse it to your namedata object
    // /.....for loop (psuedo coding here...
        list.add(new NameData(id, name, score));
    // end for loop
    return list;
}

then you will need to make a custom List adapter that uses a layout you design for the rows.:

private class ItemsAdapter extends BaseAdapter {
    NameData[] items;

    public ItemsAdapter(Context context, int textViewResourceId,   NameData[] md) {
        this.items = md;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        TextView text1;
        View view = convertView;
        if (view == null) {
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = vi.inflate(R.layout.yourRowForListlayout, null);
        }
        text1 = (TextView) view.findViewById(R.id.yourRowForListlayoutTextView);
        text1.setText("" + (position+1));
        return view;
    }

    @Override
    public int getCount() {
        return this.items.length;
    }

    @SuppressWarnings("boxing")
    @Override
    public Object getItem(int p) {
        return p;
    }

    @Override
    public long getItemId(int p) {
        return p;
    }
}

then in your creation code you can take this list and add it directly to the adapter:

List<NameData> list = getNameData();
adapter = new ItemsAdapter(this, R.layout.yourRowForListlayout, list.toArray(new NameData[list.size()]) );
setAdapter(adapter);

And thats the way I would do it for a custom list.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文