【问题标题】:android populate spinner from html form values (select tag)android从html表单值填充微调器(选择标签)
【发布时间】:2013-08-11 08:48:19
【问题描述】:

您好,我目前正在研究用 html 页面的选定部分中的值填充微调器的最佳/最简单方法。最终,微调器值必须与 html 选择部分中的值完全相同。我希望以最简单的方式做到这一点。我想到了以下想法:

  • 从 html 页面读取值(例如使用 lxml)
  • 将值添加到微调器(直接或在将值保存到数据库后如果不可能)

有谁知道最简单的方法(对于读取部分和填充部分)?是否有允许将值从 html 页面直接链接到微调器的 android 对象/类?

非常感谢您的帮助! 本

【问题讨论】:

  • HTML 页面是您的还是您正在抓取其他网站?换句话说,你有 sql 访问选择值吗?
  • 没错,html页面来自一个网站(不在我的控制之下)
  • 我正要问完全相同的问题时,我改进了我的谷歌搜索,发现这个帖子只是一个小时前发布的。奇怪的巧合。任何人都可以帮忙吗?这似乎是某人已经完成的一项常见任务,我相信我和我的朋友在这里宁愿不重新发明轮子。

标签: android html forms select spinner


【解决方案1】:

我在 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"));
        }

然后我为微调器制作了这个适配器:

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;
    }

}

然后,回到我们的 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);

最后我们像这样调用我们的 AsyncTask:

new TheaterGetter().execute(this);

事物被称为剧院这个和那个,因为在我的例子中,我得到了一个剧院位置列表。

【讨论】:

  • 谢谢你 Sajatack。我希望有一种更直接的方法可以避免解析并不总是很有趣的 html 页面:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-11
  • 1970-01-01
  • 2014-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多