Sunday, February 23, 2014

How to achieve similar contacts android comes quick search function!


The most important thing is to dynamically change listview
with TextWatcher letter changes after listeningBuilt-in address book can not read the source code, I hope the great God who made similar examples, explain the trouble, the best source attached! !<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
Own source book to read. . . . That I do not know where to start. . .
Reply:
cited a floor yiyaaixuexi reply:
own source book to read. . . . That I do not know where to start. . .


If you want to update simpleadapter data source handler is not required to use it?
How to use it?
Reply:
Unwanted, android under a control called autocomplete, had this effect, you do not need to achieve this effect.
Reply:
reference to the third floor a220315410 reply:
unwanted, android under a control called autocomplete, had this effect, you do not need to implement this effect.


Text automatically get the effect I want, I want to dynamically change the listview item, just the same as the system comes with the search function.
Reply:
I think it should be this: EditText inside OnKeyListener event listener, then refresh ListView based on the values ​​entered in the Listener inside onKey callback method, you can use the handler to achieve.
Reply:
references, 5th Floor iampy reply:
I think it should be like this: listen EditText inside OnKeyListener event, and then inside onKey Listener callback method based on the input value refresh ListView, you can use the handler to achieve.


I use
mEditText.addTextChangedListener TextWatcher ()
This is better for general use
Reply:
addTextChangedListener this is indeed better, LZ mean should be to achieve this effect it:
refresh_list.xml
 

android: orientation = "vertical" android: layout_width = "fill_parent"
android: layout_height = "fill_parent">
android: id = "@ + id / txt_input"
android: layout_width = "fill_parent"
android: layout_height = "wrap_content"
/>
android: layout_width = "fill_parent"
android: layout_height = "fill_parent"
/>




 
package com.joyband.android;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

/ **
* @ Author iampy
*
* /
public class RefreshListByEditText extends Activity implements
OnItemClickListener {
private static final String TAG = "==== RefreshListByEditText_TAG ====";
private EditText input;
private ListView listView;
private String [] listValue = {"Java", "JavaSE", "JavaEE", "Oracle 8",
"Oracle 9i", "Oracle 10g"};
private ArrayAdapter adapter;
private Handler handler;
private static final int MSG_KEY = 0x1234;

public void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.refresh_list);

input = (EditText) findViewById (R.id.txt_input);
input.addTextChangedListener (new TextWatcher () {
public void afterTextChanged (Editable editer) {
Log.d (TAG, "afterTextChanged");
}

public void beforeTextChanged (CharSequence value, int arg0,
int arg1, int arg2) {
Log.d (TAG, "beforeTextChanged");
}

public void onTextChanged (CharSequence value, int arg0, int arg1,
int arg2) {
Log.d (TAG, "onTextChanged");
Log.w (TAG, "input.text =" + value.toString ());
Message msg = new Message ();
msg.what = MSG_KEY;
Bundle data = new Bundle ();
data.putString ("value", value.toString ());
msg.setData (data);
handler.sendMessage (msg);
}
});
listView = (ListView) findViewById (R.id.list1);
adapter = new ArrayAdapter (this,
android.R.layout.simple_list_item_1, listValue);
listView.setAdapter (adapter);
listView.setOnItemClickListener (this);

handler = new Handler () {
@ Override
public void handleMessage (Message msg) {
switch (msg.what) {
case MSG_KEY:
refreshListView (msg.getData () get ("value") toString ()..);
}
}
};
}

private void refreshListView (String value) {
if (value == null | | value.trim () length () == 0.) {
adapter = new ArrayAdapter (this,
android.R.layout.simple_list_item_1, listValue);
listView.setAdapter (adapter);
}
ArrayList tmpList = new ArrayList ();
for (String s: listValue) {
if (s.indexOf (value)> = 0) {
tmpList.add (s);
}
}
if (tmpList.size () == 0)
return;
adapter = new ArrayAdapter (this,
android.R.layout.simple_list_item_1, tmpList);
listView.setAdapter (adapter);
listView.invalidateViews ();
}

public void onItemClick (AdapterView parent, View view, int position,
long id) {
Toast.makeText (this, "select list item:" + position, Toast.LENGTH_SHORT). Show ();
}

@ Override
public boolean onCreateOptionsMenu (Menu menu) {
menu.add (Menu.NONE, 1, 0, "refresh");
menu.add (Menu.NONE, 2, 0, "Exit");
return super.onCreateOptionsMenu (menu);
}

@ Override
public boolean onOptionsItemSelected (MenuItem item) {
switch (item.getItemId ()) {
case 1:
input.setText ("");
Message msg = new Message ();
msg.what = MSG_KEY;
Bundle data = new Bundle ();
data.putString ("value", "");
msg.setData (data);
handler.sendMessage (msg);
break;
case 2:
finish ();
break;
default:
break;
}

return super.onOptionsItemSelected (item);
}

}

Reply:
Thank you very much upstairs code, do not know why not implement dynamic refresh on my computer, but I read the code, very helpful to me! Thank you
Reply:
You're welcome, wherein the method private void refreshListView (String value) in if (tmpList.size () == 0)
return;
This one little problem, resulting in not refresh after deleting text input box, this sentence should be removed, or refresh the entire list.
Reply:
Look, learn next!
Reply:
MD must be registered to see a reply login, you will increase the amount of users rely on this it? Dirty!
Reply:
Give me a kind of shield!
Reply:
budong
Reply:
Learned a lot of the top one
Reply:
Learning and have a good look

No comments:

Post a Comment