2

您好,我正在下载 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 请任何人帮助我

谢谢

阿布舍克

4

1 回答 1

1

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

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

任何比这更复杂的东西都必须制作一个自定义适配器。

回答:::

这就是你要做的。创建一个名为 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);

这就是我为自定义列表做的方式。

于 2011-09-30T17:28:48.740 回答