listView filter search - android

I was looking to filter a listView by an editText.
My ListView is populate by an ArrayString, and when I filter it everything goes right. But I have a problem to recognize the element clicked. When I filter the listView and I click on the item filtered I want to have the number of the row in the original arrayString in order to make an Intent. Whit my code when I click the item in the filtered arrayString the row is 0 and it's not correct. If I search "Anche Se Pesa" I want to return the row 2 like the position in the original ArrayString.
<string-array name="titoli">
<item>An Val Dondona</item>
<item>Anche Mio Padre</item>
<item>Anche Se Pesa</item>
<item>Andremo In Francia</item>
<item>Aprite Le Porte</item>
<item>Arda Berghem</item>
<item>Ascoltate Amici Cari</item>
<item>Ave, O Vergine</item>
<item>Aver na Figlia Sola Soletta</item>
<item>Ancora sti quatro</item>
<item>Andouma Prou</item>
</string-array>
Here Is the code
public class activity_titoli extends AppCompatActivity {
Button btt_backHome;
ListView lV_titoli;
EditText eT_search;
private ArrayAdapter<CharSequence> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_titoli);
btt_backHome = (Button) findViewById(R.id.btt_backHome);
lV_titoli = (ListView) findViewById(R.id.lV_titoli);
eT_search = (EditText) findViewById(R.id.eT_search);
adapter = ArrayAdapter.createFromResource(this, R.array.titoli, android.R.layout.simple_list_item_1);
lV_titoli.setAdapter(adapter);
final Intent to_Home = new Intent (this , Activity_Main.class);
eT_search.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
}
#Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
(activity_titoli.this).adapter.getFilter().filter(arg0);
}
});
btt_backHome.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(to_Home);
}
});
lV_titoli.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Log.e("Element: " , Integer.toString(i));
}
});
}
}

Try this code :
lV_titoli.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Resources res = getResources();
List<CharSequence> list = Arrays.asList(res.getStringArray(R.array.titoli));
Log.e("Element: " , Integer.toString(list.indexOf(adapter.filgetItem(i))));
}
});
Hope this helps

Related

Using edittext to search listview

I have this Listview:
#Override
public void onComplete(List<Profile> friends) {
runOnUiThread(new Runnable() {
#Override
public void run() {
mSpinner.setVisibility(View.GONE);
mSpinner.clearAnimation();
}
});
// populate list
List<String> values = new ArrayList<String>();
for (Profile profile : friends) {
//profile.getInstalled();
values.add(profile.getName());
}
listView = (ListView) findViewById(R.id.list);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> av, final View view, final int i, long i2) {
Animation pushLeftIn = AnimationUtils.loadAnimation(CallActivity.this, R.anim.jump_no_fade);
view.startAnimation(pushLeftIn);
}
});
ArrayAdapter<String> friendsListAdapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.list_items2, values);
friendsListAdapter.sort(new Comparator<String>() {
#Override
public int compare(String lhs, String rhs) {
return lhs.compareTo(rhs);
}
});
mFriendsList.setAdapter(friendsListAdapter);
}
};
I am trying to make the edittext filter the listview, this is what i have tried:
mySearchView.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
CallActivity.this.friendsListAdapter.getFilter().filter(cs);
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
I get no errors in the code but when i try to search in the edittext it doesn't work at all, what is the problem or how should i do instead?
Try do it in afterTextChanged(Editable arg0).
And did you implement your getFilter().filter(cs). Can you show implementation?

Search in ListView, incorrect result on get id onItemClick

I have Activiti in which there is a search for ListView. When I return the result of anything I do not use the filter, everything is fine. choose the element 520 and I get it id, but as soon as I use a filter and find the same item, get the id 0-10. Please tell me how to solve this problem. Thanks in advance!
P.S.
I am sorry for my english!
public class Clients extends Activity {
private ListView lv;
ArrayAdapter<String> adapter;
EditText etInputSearch;
DB dbSync;
DB dbUser;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.clients);
dbSync = new DB(this);
dbUser = new DB(this);
lv = (ListView) findViewById(R.id.list_view);
etInputSearch = (EditText) findViewById(R.id.etInputSearch);
adapter = new ArrayAdapter<String>(this, R.layout.clients_item, R.id.clients_name, dbSync.createlistClients(dbUser.getCodeAgent()));
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> l, View v, int position,
long id) {
Log.d("TAG", position + " " + id);
}
});
etInputSearch.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
Clients.this.adapter.getFilter().filter(cs);
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,int arg3) {}
#Override
public void afterTextChanged(Editable arg0) {}
});
}
}
You are using standard ArrayAdapter. In the realization of standard AndroidAdapter you can find following code:
public long getItemId(int position) {
return position;
}
That's why you get the id quals to position. To avoid this behavior you must implement your own adapter (extending ArrayAdapter for example) and overrid getItemId method.
Or use CursorAdapter which use _id column in database as id.

how to take the listview item from addTextChangedListener and display it in the corresponding edittext box

//Listview adapter
adapter_q = new ArrayAdapter<String>(Authors.this, R.layout.list_item_author,R.id.product_name, Quotes);
lv.setAdapter(adapter_q);
//textViewq=(MultiAutoCompleteTextView)findViewById(R.layout.list_item_author);
System.out.println("before printing");
// filtering text
inputSearch.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
Authors.this.adapter_q.getFilter().filter(cs);
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
taking the selected item from the list and seeting it to the edittext box
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg01, View arg11, int arg21, long arg31) {
valc= arg01.getItemAtPosition(arg21).toString();
System.out.println(valc);
inputSearch.setText(valc);
}
});
This is my search edittext box,i am getting values in it and it is filtering correctly,
but my problem is that when i select a value from the list view, the listview still remains there,
how to dismiss it as i take value from that list view?
Assuming that Quotes is your arraylist of string that you passed into the adapter, you need to do the following:
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg01, View arg11, int arg21, long arg31) {
System.out.println(Quotes.get(arg21));
inputSearch.setText(Quotes.get(arg21));
}
});
Because the data you want to display is stored in the array list instead.

Android Listview

now i am making a program in android using edittext and listview.
I want to search the listview item using edittext above.
After populate data to listview, when user type text in edittext, the listview will
scroll to the position start with that text.
Example: i have item:
apple,
application,
book,
boy,
car,
cat,
cash.....
when i type b in edittext then listview will scroll to book.
I want to use the listview.setSelection(position), but i don't know how can i get the position from my edittext search.
I hope everybody can help me. Thanks in advances !
you can make implement like the below code:
YOUR_EDITTEXT.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
//LOGIC MAY DIFFER ACCORDING TO YOUR REQUIREMENT..
int POSITION = 0;
for(int i =0;i<list.size();i++) {
if(list.get(i).startsWith(s.toString()))
{
POSITION = i;
break;
}
}
listview.smoothScrollToPosition(POSITION);
}
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
});
Hope it will help you..!!!
try this example,
public class MainActivity extends Activity {
private ListView list1;
// private String array[] ;
EditText inputSearch;
ArrayAdapter<String> adapter;
String[] testArray = new String[] { "one", "two", "three", "etc" };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inputSearch = (EditText) findViewById(R.id.inputSearch);
list1 = (ListView) findViewById(R.id.ListView01);
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, testArray);
list1.setAdapter(adapter);
inputSearch.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2,
int arg3) {
// When user changed the Text
MainActivity.this.adapter.getFilter().filter(cs);
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
list1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Log.i("check", "" + position);
}
});
}
}

ListView and TextFilter

When looking through the list of the given position is not correct. How to convey that I needed the position?
method getItemId(position) did not help!
There is some sample code:
lv1 = (ListView) findViewById(R.id.list);
inputSearch = (EditText) findViewById(R.id.inputSearch);
final String listcode[] = getResources().getStringArray(R.array.list);
adapter = new ArrayAdapter<String>(this, R.layout.list_item,
R.id.product_name, listcode);
lv1.setAdapter(adapter);
lv1.setTextFilterEnabled(true);
lv1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position,
long id) {
long line = adapter.getItemId(position);
Integer itemname = new Integer((int) line);
Intent intent = new Intent();
intent.setClass(Main.this, ShowKnot.class);
Bundle b = new Bundle();
b.putString("defStrID", itemname.toString());
b.putString("defStrName", listcode[itemname]);
intent.putExtras(b);
startActivity(intent);
}
});
inputSearch.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2,
int arg3) {
Main.this.adapter.getFilter().filter(cs);
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
Instead of
long line = adapter.getItemId(position);
you can use "position" in the method arguement returns the list item position number
public void onItemClick(AdapterView<?> a, View v, int position,
long id) {
a.getItemAtPosition(position);
here "int position" parameter is the index of the listitem.

Categories

Resources