1

こんにちは、私は現在、html ページの選択セクションからの値をスピナーに入力するための最良/最も簡単な方法を検討しています。最終的に、スピナーの値は、html 選択セクションにある値とまったく同じでなければなりません。私はこれを可能な限り簡単な方法で行うことを目指しています。以下の案を考えました。

  • HTML ページから値を読み取ります (たとえば、lxml を使用)
  • 値をスピナーに追加します (直接、またはデータベースに値を保存した後に不可能な場合)。

これに対する最も簡単な方法を知っている人はいますか (読み取り部分と人口部分の両方について)。HTMLページの値をスピナーに直接リンクできるAndroidオブジェクト/クラスはありますか?

ご協力ありがとうございました。ベン

4

1 に答える 1

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

私の場合、劇場の場所のリストを取得していたため、物事は劇場と呼ばれています。

于 2013-08-11T00:42:01.413 に答える