简体   繁体   English

向Android应用添加搜索功能。 能够搜索列表

[英]Adding search function to android app. Being able to search list

I currently have a simple App that displays news articles from a url in a gridView . 我目前有一个简单的应用程序,可在gridView中显示来自url新闻报道。 I have added a search icon to my main menu xml but I don't know how to get the search function to work. 我已经在主菜单xml添加了搜索图标,但是我不知道如何使搜索功能正常工作。 I just need it to display the relevant articles in GridView if the string the user searches is matches the article title. 如果用户搜索的字符串与文章标题匹配,我只需要它在GridView显示相关文章。 Any help would be appreciated, I don't know how to get started! 任何帮助将不胜感激,我不知道如何开始!

I have other files of course, let me know if you need to see them, I am not sure what is helpful. 我当然还有其他文件,如果您需要查看它们,请告诉我,我不确定有什么帮助。

ActivityMain.java

public class MainActivity extends AppCompatActivity {

private List<NewsRecord> newsListData = new ArrayList<NewsRecord>();

private GridView newsListView;

private NewsListAdapter adapter;

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

GridView newsListView = (GridView) findViewById(R.id.newsFeedList);

adapter = new NewsListAdapter(this, R.layout.adapter_news_list,  newsListData, this);

newsListView.setAdapter(adapter);

newsListView.setOnItemClickListener(itemClicked);

nextStart = 0;
updateListData(nextStart, 20);
}

public int nextStart = 0;

public void updateListData(int StartPoint, int count){
String url = "http://www.efstratiou.info/projects/newsfeed/getList.php?        start=" + StartPoint + "&count=" + count;

EDANewsApp app = EDANewsApp.getInstance();

JsonArrayRequest jsonRequest = new JsonArrayRequest(url, listener, errorListener);
app.requestQueue.add(jsonRequest);

nextStart +=count;
}

@Override
public boolean onCreateOptionsMenu (Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}

main_menu.xml main_menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"> xmlns:app =“ http://schemas.android.com/apk/res-auto”>

<item
    android:id="@+id/action_about"
    android:orderInCategory="10"
    app:showAsAction="never"
    android:title="About"/>

<item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    app:showAsAction="never"
    android:title="Settings" />

 <item android:id="@+id/action_search"
    android:title="Search"
    android:icon="@android:drawable/ic_menu_search"
    app:showAsAction="ifRoom|collapseActionView"
    app:actionViewClass="android.support.v7.widget.SearchView"
    android:textColor="#FFF"
    />

</menu>

activity_main.xml activity_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/newsListItem"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context=".MainActivity">



 <GridView
    android:id="@+id/newsFeedList"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:verticalSpacing="0dp"
    android:horizontalSpacing="0dp"
    android:stretchMode="columnWidth"
    android:numColumns="2"/>

</FrameLayout>

ok I will assume that following is your NewsRecord.java 好的,我假设以下是您的NewsRecord.java

public class NewsRecord {

    private String articleTitle;

    public String getArticleTitle() {
        return articleTitle;
    }

    public void setArticleTitle(String articleTitle) {
        this.articleTitle = articleTitle;
    }
}

this is the code you will be using to search 这是您将用于搜索的代码

/**
 * @param list - this is the Input list in which you want to look up the search text
 * @param string - this is the string which would be looked up in the list
 * @return this method returns list of NewsRecord of NewsRecord which contains search string in thier article 
 * */
private static ArrayList<NewsRecord> searchInList(ArrayList<NewsRecord> list, String string) {
    // TODO Auto-generated method stub
    ArrayList<NewsRecord> temp = new ArrayList<NewsRecord>();

    for (NewsRecord item : list) {
        if (item.getArticleTitle().contains(string)) {
            temp.add(item);
        }
    }

    if (!temp.isEmpty()) {
        return temp;
    }

    return null;
}

following is the code how to utilize this method 以下是代码如何利用此方法

lets say you have a editText from which you would be searching.. 假设您有一个editText将从中进行搜索。

String searchArticle = editText.getText().toString();
ArrayList<NewsRecord> templist = searchInList(list, searchArticle); 
if (!templist .isEmpty()) {
        adapter = new NewsListAdapter(this, R.layout.adapter_news_list,  templist , this);
        newsListView.setAdapter(adapter);
    }

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

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