使用用户输入搜索ArrayList

时间:2014-02-06 03:36:22

标签: android string search arraylist

现在,我从网站上获取一个问题列表并将其放入ArrayList<String>

现在,我如何获取用户输入,我将其转换为字符串(sSearchValue),并使用ArrayList检查是否匹配?如果它找到包含用户搜索的问题,它会显示它吗?

package com.malthorn.anyquestion;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.malthorn.gasculator.R;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Document doc;
        ArrayList<String> urls = new ArrayList<String>();
        EditText iSearchValue = (EditText) findViewById(R.id.iQuestion);
        String sSearchValue = iSearchValue.getText().toString();

        try {

            doc = Jsoup.connect("http://www.reddit.com/r/askscience/search?q=flair%3A%27Psych%27&sort=top&restrict_sr=on").get();

            Elements links = doc.select("a[href]");
            for (Element link : links) {
                String href1=link.attr("href");
                //
                if(href1.startsWith("http://www.reddit.com/r/askscience/"))
                 {
                    urls.add(link.attr("href"));
                 }

                System.out.println("\nlink : " + link.attr("href"));

            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

1 个答案:

答案 0 :(得分:1)

public String getQuestion(String[] questions, String sSearchValue){
    for(String question : questions){
        if(question.equals(sSearchValue))
            return question;
    }

    return "";
}

此代码搜索完全匹配,如果您需要查找重新组合搜索词的问题,我将通过Google搜索方法subString获取String类型以及使用Regex进行模式匹配。