简体   繁体   English

android从html表单值(选择标签)填充微调框

[英]android populate spinner from html form values (select tag)

Hi I'm currently looking at the best/simplest way to populate a spinner with the values from a select section of a html page. 嗨,我目前正在寻找用html页面中的select部分中的值填充微调框的最佳/最简单方法。 Eventually the spinner values must be exactly the same as the ones present in the html select section. 最终,微调器的值必须与html select部分中的值完全相同。 I'm looking to do this in the simplest way possible. 我希望以最简单的方式做到这一点。 I thought of the following ideas: 我想到了以下想法:

  • Read the values from the html page (for example using lxml) 从html页面读取值(例如,使用lxml)
  • Add the values to the spinner (directly or if not possible after saving the values in a database) 将值添加到微调器(直接或如果将值保存到数据库后则无法添加)

Does anyone know the simplest way to this (for both the read part and the population part)? 有谁知道最简单的方法(对于阅读部分和总体部分而言)? Is there an android object/class allowing to directly link the values from the html page to a spinner? 是否有一个android对象/类,可以直接将html页面中的值链接到微调器?

Many thanks in for your help! 非常感谢您的帮助! Ben

I used jsoup in an AsyncTask to get the value and text of the options and put them in a text/value TreeMap (sorted HashMap) like so: 我在AsyncTask中使用了jsoup来获取选项的值和文本,然后将它们放入文本/值TreeMap(排序为HashMap)中,如下所示:

class TheaterGetter extends AsyncTask<Context, Void, Document> {    
    private Context context;
    @Override
    protected Document doInBackground(Context... contexts) {
        context = contexts[0];
        Document doc = null;
        try {
            doc = Jsoup.connect("http://landmarkcinemas.com").timeout(10000).get();
        } catch (IOException e) {
            Log.e("website connection error", e.getMessage());
        }
        return doc;
    }

    protected void onPostExecute(Document doc) {
        Element allOptions = doc.select("select[id=campaign").first();
        Elements options = allOptions.getElementsByTag("option");
        options.remove(0);
        TreeMap<String, String> theaters = new TreeMap<String, String>();
        for (Element option:options) {
            theaters.put(option.html(), option.attr("value"));
        }

Then I made this adapter for the spinner: 然后,我为转盘制作了此适配器:

public class TreeMapSpinAdapter extends ArrayAdapter{
    private Context context;
    private TreeMap<String, String> treeMap;

    public TreeMapSpinAdapter(Context context, int textViewResourceId, TreeMap<String, String> treeMap){
        super(context, textViewResourceId, treeMap.values().toArray());
        this.context = context;
        this.treeMap = treeMap;
    }

    @Override
    public int getCount() {
        return this.treeMap.values().size();
    }

    @Override
    public Object getItem(int arg0) {
        return this.treeMap.values().toArray()[arg0];
    }

    public Object getItem(String key) {
        return treeMap.get(key);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TextView label = new TextView(context);
        label.setTextColor(Color.BLACK);
        label.setText(treeMap.keySet().toArray()[position].toString());
        return label;
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        TextView label = new TextView(context);
        label.setTextColor(Color.BLACK);
        label.setText(treeMap.keySet().toArray()[position].toString());
        return label;
    }

}

Then, back in our AsyncTask we set up the spinner like so: 然后,回到我们的AsyncTask中,设置微调器,如下所示:

TreeMapSpinAdapter adapter = new TreeMapSpinAdapter(context, android.R.layout.simple_spinner_item, theaters);
final Spinner spinner = (Spinner) ((Activity) context).findViewById(R.id.spinner1);
spinner.setAdapter(adapter);

And finally we call our AsyncTask like so: 最后,我们这样调用AsyncTask:

new TheaterGetter().execute(this);

Things are called theater this and that because in my case I was getting a list of theater locations. 之所以称为剧院,是因为在我的情况下,我获得了剧院位置的列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM