简体   繁体   English

Android中的搜索按钮功能

[英]Search Button Functionality in Android

My Android App is based on Job portal, i have parsed the info from website and got into listview, now the listview contains all categories of jobs, In the First activity there is a textbox and search button, what ever keyword for "Title" is given in the textbox only those categories of jobs should be displayed in the listview. 我的Android应用程序基于Job门户,我已经从网站上解析了信息并进入了listview,现在listview包含了所有类别的工作,在第一个活动中有一个文本框和搜索按钮,“ Title”的关键字是在文本框中给出,只有那些类别的作业应显示在列表视图中。 To do this should i have to compare the elements in the "textbox" with website data or already parsed info in listview? 为此,我是否必须将“文本框”中的元素与网站数据或已在listview中解析的信息进行比较? for comparing the string in "textbox" and storing it separate array, where should i place the code, can anybody help? 比较“文本框”中的字符串并将其存储在单独的数组中,我应该在哪里放置代码,有人可以帮忙吗?

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

        }
    });

There's alot of ways you can accomplish this, one pretty straight forward way would be to store the result of the json call in some sort of list structure. 有很多方法可以完成此操作,一种很简单的方法是将json调用的结果存储在某种列表结构中。 Now, introduce another datastructure which shows a subset of the results list, you could use an ArrayAdapter, with offers both clear(), add(), insert() and remove() methods to manipulate the underlying data-structure. 现在,引入另一个显示结果列表子集的数据结构,您可以使用ArrayAdapter,同时提供clear(),add(),insert()和remove()方法来操作基础数据结构。

Then, a search would consist of 然后,搜索将包括

  1. iterating over your results data structure 遍历结果数据结构
  2. updating your visibleResults data structure as you go along 进行时更新您的visibleResults数据结构
  3. notify the view that the array adapter has changed using notifyDataSetChanged 使用notifyDataSetChanged通知视图阵列适配器已更改

Or, if you are feeling fancy, you could cache the results of the json query in a sqlite database, and use a CursorAdapter instead. 或者,如果您觉得花哨的话,可以将json查询的结果缓存在sqlite数据库中,并改用CursorAdapter。 Depending on the complexity of your queries, and the amount of data concerned, this might be overkill though. 但是,根据查询的复杂性和相关数据量的不同,这可能会显得过高。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM