Android dynamically displays data based on the EditText search box ListView
According to the EditText search box, the ListView dynamically displays data as needed. I think the content involved may be interesting, so I 'd like to write it first, hoping to help you a bit.
First, let's analyze the entire process:
1. Create a layout that contains an EditText search box and a ListView
2. Create a dataset mData for creating the ListView Adapter
3. Add the text change listener of EditText
4. Use yydatasetchanged () to dynamically update ListView
Step 1: Create a search box
This is still relatively easy. Here I am using idea.
Add a Relativelayout layout, and then add two controls (three controls) to the layout ),
The xml Code is as follows:
Step 2: Create a dataset mData
SimpleAdapter is used here, so the format of dataset creation is as follows. You can create metadata based on your own Adapter. The data stored in mListTitle and mListText will not be changed, while mData will change in the text box, mData data will also change accordingly, which is required for the update operation. Create a metadata set,
The Code is as follows:
ListView mListView; ArrayList
> mData = new ArrayList
>(); ArrayList
mListTitle = new ArrayList
(); ArrayList
mListText = new ArrayList
(); private void getmData(ArrayList
> mDatas) { Map
item = new HashMap
(); mListTitle.add(This is a title!); mListText.add(this is a text.2014.09.18.16.33); item.put(title, mListTitle.get(0)); item.put(text, mListText.get(0)); mDatas.add(item); mListTitle.add(This is an another title!); mListText.add(this is an another text.2014.09.18.16.33); item = new HashMap
(); item.put(title, mListTitle.get(1)); item.put(text, mListText.get(1)); mDatas.add(item); }
Then, use mData to create an Adapter.
private void set_mListView_adapter() { mListView = (ListView) findViewById(R.id.mListView); getmData(mData); adapter = new SimpleAdapter(this,mData,android.R.layout.simple_list_item_2, new String[]{title,text},new int[]{android.R.id.text1,android.R.id.text2}); mListView.setAdapter(adapter); }
At this point, the initial state of the program is displayed. If there is no search box, you can do it here.
Step 3: add the text change listener of EditText
Because we want to dynamically modify the display of the ListView, we must listen and then perform the corresponding action. When listening for text changes, Handler post a Runnable to make the corresponding changes.
Private void set_eSearch_TextChanged () {eSearch = (EditText) findViewById (R. id. etSearch); eSearch. addTextChangedListener (new TextWatcher () {@ Override public void onTextChanged (CharSequence arg0, int arg1, int arg2, int arg3) {// TODO Auto-generated method stub // this should be the action that will be performed during the change, but it has never been used yet.} @ Override public void beforeTextChanged (CharSequence arg0, int arg1, int arg2, int arg3) {// TODO Auto-generated method stub // This is the action that will be executed before the text box is changed} @ Override public void afterTextChanged (Editable s) {// TODO Auto-generated method stub/** this is the action that will be executed after the text box is changed * because what we need to do is, when the text box is changed, the data of our listview is also changed accordingly and displayed on the interface, for example. * Here we need to add the data modification action. */If (s. length () = 0) {ivDeleteText. setVisibility (View. GONE); // if the text box is empty, the cross-fork disappears} else {ivDeleteText. setVisibility (View. VISIBLE); // when the text box is not empty, myhandler is displayed. post (eChanged );}}});}
Handler represents a huge purpose here. Based on a post function like Handler, we can make the desired changes to the interface, not just the modification of the ListView, for example, each word input can change the font to another color or something.
Step 4: Use yydatasetchanged () to dynamically update ListView
Here is the most critical step for returning to the question. If we post the content, we need to filter the metadata based on the text in the search text box and then display the matching data on the ListView.
(/* Ah, is it a sin to write a blog with S4 ...*/)
The adapter has a yydatasetchanged () method. You can use this method to update the bound ListView when updating data. The effect is as follows:
Enter a 5 or two items. In my choice, as long as the title and text contain the characters in the text box, it is the target item.
When 50 is entered, because only the title or text in one item contains the text in the search box, only
Add a zero value. Because the text without an item contains 500, the Listview does not have an item.
It worked well. LZ was too good and encountered some problems.
Some problems may occur here:
1. notifyDataSetChanged (). The updated mData dataset is indeed changed, but the ListView is not updated. This is what I did before. Later I found that the reference of the mData dataset has changed, so it is useless for the Adapter to restart Y. Because the Adapter is bound to the reference of mData, the reference changes, the data is not updated to the ListView. This is also the reason why the get function parameter is passed in. If a reference is returned directly, this problem will occur. Therefore, pay attention to this. See http://www.bkjia.com/kf/201401/273017.html
2. You can use Handler to update the UI by posting a Runnable. Runnable updates the data in mData based on the text in the search box.
The Code is as follows:
Runnable eChanged = new Runnable() { @Override public void run() { // TODO Auto-generated method stub String data = eSearch.getText().toString(); mData.clear(); getmDataSub(mData, data); adapter.notifyDataSetChanged(); }};
3. It may be a bit difficult to understand mData, because after data is updated, every filtered data is stored in mData. What about the original data, of course, it is in mListTitle and mListText. According to the Code of getmDataSub, we can see that.
private void getmDataSub(ArrayList
> mDataSubs, String data){ int length = mListTitle.size(); for(int i = 0; i < length; ++i){ if(mListTitle.get(i).contains(data) || mListText.get(i).contains(data)){ Map
item = new HashMap
(); item.put(title, mListTitle.get(i)); item.put(text, mListText.get(i)); mDataSubs.add(item); } }}
4. Because the text box is in the first one, when the program runs, the text box will get the focus, and then the input method will pop up. Here we add a LinearLayout with a length and width of 0 in the xml file to get the focus, the Code is as follows:
In summary, this dynamic method may need to be improved, such as the filtering method. The control is only for the Listview of SampleAdapter. If it is a custom ListView, it should be possible to optimize it again. In addition, you can set the height of the ListView to wrap_content and fill_parent. In fact, you can experience two kinds of gaps by changing to a real machine. Obviously, it is the process of downgrading.
The writing is simple and crude. Add a copy of the code
I always like sharing, so it is free to download. Timestamp