2014-02-06 200 views
-1

現在,我得到的來自網站的問題的列表,並把它ArrayList<String>內。ArrayList中搜索與用戶輸入

現在,我如何接受用戶輸入,我將其轉換爲字符串(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
public String getQuestion(String[] questions, String sSearchValue){ 
    for(String question : questions){ 
     if(question.equals(sSearchValue)) 
      return question; 
    } 

    return ""; 
} 

精確匹配這段代碼的搜索,如果你需要找到ressemble搜索詞的問題,我會adivse你對模式匹配google搜索方法串爲String類型,也正則表達式的用途。

+0

好的,謝謝!從看我的jsoup解析,你知道我是否正確地執行它?在中,我是否將正確的數據放入ArrayList中? – user3224105

+0

另外,在你的代碼中,你正在使用string []問題和疑問,你能解釋一下,如果它的字符串是sSearchValue,它如何搜索用戶輸入? – user3224105

+0

示例代碼循環遍歷一個字符串數組(我想這包含問題),如果他找到一個匹配發送的字符串(sSearchValue),他會返回該問題 對不起,我不熟悉Jsoup解析系統; ) – nunoh123