2013-01-23 127 views
1

我的Android應用程序基於Job門戶,我已經解析了來自網站的信息並進入了列表視圖,現在列表視圖包含所有類別的作業,在第一個活動中有一個文本框和搜索按鈕,在文本框中給出「標題」的關鍵字,只有這些類別的作業應該顯示在列表視圖中。要做到這一點,我必須比較「文本框」中的元素與網站數據或已經在列表視圖中解析的信息? 用於比較「文本框」中的字符串並將其存儲在單獨的數組中,我應該在哪裏放置代碼,任何人都可以幫忙?Android中的搜索按鈕功能

public class Home extends ListActivity 
{// url to make request 
private static String url = "http://www.example.com/jobs/?json=get_recent_posts"; 

// JSON Node names 
private static final String TAG_POSTS = "posts"; 
private static final String TAG_ID = "id"; 
private static final String TAG_TITLE = "title"; 
private static final String TAG_DATE = "date"; 
private static final String TAG_CONTENT = "content"; 
private static final String TAG_AUTHOR = "author"; 
private static final String TAG_NAME = "name"; 



// contacts JSONArray 
JSONArray posts = null; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    // Hashmap for ListView 
    ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>(); 

    // Creating JSON Parser instance 
    JSONParser jParser = new JSONParser(); 

    // getting JSON string from URL 
    JSONObject json = jParser.getJSONFromUrl(url); 

    try { 
     // Getting Array of Contacts 
     posts = json.getJSONArray(TAG_POSTS); 

     // looping through All Contacts 
     for(int i = 0; i < posts.length(); i++){ 
      JSONObject c = posts.getJSONObject(i); 

      // Storing each json item in variable 
      String id = c.getString(TAG_ID); 
      String title = c.getString(TAG_TITLE); 
      String date = c.getString(TAG_DATE); 
      String content = c.getString(TAG_CONTENT); 

      content = content.replace("<br />", ""); 
      content = content.replace("<p>", ""); 
      content = content.replace("</p>", ""); 


      // Phone number is agin JSON Object 
      JSONObject author = c.getJSONObject(TAG_AUTHOR); 
      String name = author.getString(TAG_NAME); 


      // creating new HashMap 
      HashMap<String, String> map = new HashMap<String, String>(); 

      // adding each child node to HashMap key => value 
      map.put(TAG_ID, id); 
      map.put(TAG_TITLE, title); 
      map.put(TAG_DATE, date); 
      map.put(TAG_NAME, name); 
      map.put(TAG_CONTENT, content); 
      // adding HashList to ArrayList 
      contactList.add(map); 
     } 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 


    /** 
    * Updating parsed JSON data into ListView 
    * */ 
    ListAdapter adapter = new SimpleAdapter(this, contactList, 
      R.layout.activity_home, 
      new String[] { TAG_TITLE, TAG_DATE, TAG_NAME, TAG_CONTENT }, new int[] { 
        R.id.name, R.id.email,R.id.mobile,R.id.content}); 

    setListAdapter(adapter); 

    // selecting single ListView item 
    ListView lv = getListView(); 

    // Launching new screen on Selecting Single ListItem 
    lv.setOnItemClickListener(new OnItemClickListener() { 

     public void onItemClick(AdapterView<?> parent, View view, 
       int position, long id) { 
      // getting values from selected ListItem 
      String title = ((TextView) view.findViewById(R.id.name)).getText().toString(); 
      String date = ((TextView) view.findViewById(R.id.email)).getText().toString(); 
      String name = ((TextView) view.findViewById(R.id.mobile)).getText().toString(); 
      String content = ((TextView) view.findViewById(R.id.content)).getText().toString(); 

      // Starting new intent 
      Intent in = new Intent(getApplicationContext(), Singlemenuitem.class); 
      in.putExtra(TAG_TITLE, title); 
      in.putExtra(TAG_DATE, date); 
      in.putExtra(TAG_NAME, name); 
      in.putExtra(TAG_CONTENT, content); 
      startActivity(in); 

     } 
    }); 

回答

0

有很多方法可以實現這一點,一個非常簡單的方法是將json調用的結果存儲在某種列表結構中。現在,引入另一個顯示結果列表子集的數據結構,您可以使用ArrayAdapter,提供clear(),add(),insert()和remove()方法來操作底層數據結構。

然後,搜索將包括

  1. 迭代您的結果數據結構
  2. 更新visibleResults數據結構,當你沿着
  3. 去通知認爲,該陣列適配器使用notifyDataSetChanged
  4. 改變

或者,如果你覺得奇怪,可以將json查詢的結果緩存在sqlite數據庫中,然後使用CursorAdapter。根據查詢的複雜程度和相關數據量的不同,這可能是過度的。

+0

感謝您的回覆dany,因爲我是新手,你有沒有這方面的任何鏈接,因爲我搜索的一切都是基於靜態listivew。 @justDanyul –

+1

非常歡迎。作爲開始,我會閱讀這個http://www.vogella.com/articles/AndroidListView/article.html,它會讓你很好地理解列表視圖是如何工作的,以及如何讓它們支持陣列適配器。得到這些後,其餘的應該是相當平凡的。 – JustDanyul