开发者

How to add data dynamically into the spinner from xml

开发者 https://www.devze.com 2023-04-09 19:03 出处:网络
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.

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消