Android Listview - android

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

Related

listView filter search

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

Getting item position in AutoCompleteTextView addTextChangeListener method

I have Autocomplete textviews in my custom adapter. when i write something in onTextChange listener, i want to get item position. I it gives all items positions. Please help me regarding this issue. Thanks!
autoCompleteTextView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {
String selection = (String)parent.getItemAtPosition(position);
//TODO Do something with the selected text
}
});
This is how you can get position of Item you typed in AutoCompleteTextView. But you have to add the full name of item like "apple" or "apples" then this code will give you the position of that particular item. But its very expensive process if you have lots of items in your list.
public class Test extends AppCompatActivity {
private AutoCompleteTextView autoCompleteTextView;
private List<String> list;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chintan_test);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
list = new ArrayList<>();
list.add("Apple");
list.add("Apples");
list.add("Banana");
list.add("Aunt");
list.add("Orange");
autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
autoCompleteTextView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list));
autoCompleteTextView.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
for (String string : list)
if (string.toLowerCase().equals(s.toString().toLowerCase())) {
int pos = list.indexOf(string);
Toast.makeText(Test.this, "" + pos, Toast.LENGTH_SHORT).show();
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
}
}
Let me know if this code helps you.
Use textwatcher, implement it in activity class.
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
hi can you please check there is some issue
AutoCompleteTextView tv = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
final ArrayList<Student> list = new ArrayList<MainActivity.Student>();
list.add(new Student("abc"));
list.add(new Student("abc"));
ArrayAdapter<Student> adapter = new ArrayAdapter<MainActivity.Student>(
this, android.R.layout.simple_dropdown_item_1line, list);
tv.setAdapter(adapter);
tv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Student selected = (Student) arg0.getAdapter().getItem(arg2);
Toast.makeText(MainActivity.this,
"Clicked " + arg2 + " name: " + selected.name,
Toast.LENGTH_SHORT).show();
}
});
for more checking you can go from here...
you can use addTextChangedListener for this purpose
yourAutoCompleteTextView.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void afterTextChanged(Editable s) {
} });

implement search function to listview in fragments

I'm setting some values taken from a database i created into a listview. The listview is populated correctly.Now i want to implement a search function to my listview. How can i do this? I've already created search bar layout.I've referred to many tutorials but could not get it done.
My class
public class StockWatchView extends Fragment {
private ListView mListView;
private LazyListViewAdapter mAdapter;
private EditText search;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.stock_watchlist_view, container, false);
mListView = (ListView) view.findViewById(R.id.listview);
search = (EditText) view.findViewById(R.id.search);
Toast.makeText(StockWatchView.this.getActivity(), "Do Background Service", Toast.LENGTH_LONG).show();
DBHandler dbHandler = new DBHandler(StockWatchView.this.getActivity(), null, null, 0);
ArrayList<BaseElement> items = dbHandler.getAllPric();
if (items.size() != 0) {
mAdapter = new LazyListViewAdapter(items, StockWatchView.this.getActivity(), ListElement.PRICE_LIST.getPosition());
mListView.setAdapter(mAdapter);
Toast.makeText(this.getActivity(), "Toast " + dbHandler.getLastUpdateTime(), Toast.LENGTH_LONG).show();
} else {
//new BackgroundService().execute();
}
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) {
}
// TODO Auto-generated method stub
});
return view;
}
}
You will have to declare a text watcher like::
TextWatcher mSearchTw = new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
#Override
public void afterTextChanged(Editable editable) {
//put length after which this would be called
if (editable.length() > 2) {
//here you search in db
// or the stuff you want to do in view
}
}
}
` editText.addTextChangedListener(mSearchTw);
Make the items ArrayList as public .
Then fetch the required data(search result as type ArrayList<BaseElement>) by querying the database as like you did dbHandler.getAllPric(); in OnTextChanged() method by passing the editable text as parameter.
In the query you are going to use "like" phrase to get appropriate data.
For example,
items = dbHandler.getSearchResult(editableText);//editableText as String
after do adapter.notifyDataSetChanged();
Hope this approach will help you.

Add search to ListView using SimpleAdapter in Android

I have a ListView from json file that is in my assets folder. The listView works perfectly, but I want to add Search to it. I want to add editText before the listView, and when the user types some text, the list will be filtered.
I'm using SimpleAdapter for the listView.
I've tried some tutorials but they don't work, or when I type in the editText textBox the items of the list are duplicated. Sorry for my bad English.
this is the code:
ListAdapter adapter = new SimpleAdapter(this, herbList,
R.layout.caevi_list_item, new String[] { HERB_ID, HERB_NAME, HERB_DISCRIPTION},
new int[] { R.id.idbilka, R.id.name, R.id.discription});
setListAdapter(adapter);
inputSearch = (EditText) findViewById(R.id.inputSearch);
inputSearch.setSingleLine();
inputSearch.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
((Filterable) Caevi.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
}
});

Filtered Listview returns original position

I'm filtering a listview that it's working properly. But when I click in an item of the filtered listview, it gives me the name of the item in the original position.
Example:
Items: A, B, C , D, E.
If I filter C, it returns only 1 item (C) but it shows A because it is position 0.
I have been looking for information but none of the similar questions helped me.
This is my code:
final FilteredListviewAdapter adapter = new FilteredListviewAdapter(AddAgentProfile.this, android.R.layout.simple_list_item_1, arraymls);
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
addProfile.setMLSId(mlslist.get(position).getId());
// THIS IS THE LINE THAT GIVES ME THE WRONG RESULT!!!
textmls.setText(mlslist.get(position).getName());
mls.setText("");
dialog.dismiss();
}
});
list.setAdapter(adapter);
final EditText filter = (EditText) inflated.findViewById(R.id.et_filter);
filter.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
adapter.getFilter().filter(s);
adapter.notifyDataSetChanged();
}
});
And FIlteredListViewAdapter:
public class FilteredListviewAdapter extends ArrayAdapter<String> {
ArrayList<String> list;
public FilteredListviewAdapter(Context context, int layout, ArrayList<String> array) {
super(context, layout, array);
list = array;
}
#Override
public String getItem(int position) {
return super.getItem(position);
}
}
What I'm doing wrong?
I have finally found the solution.
The code of the Adapter is ok as I wrote it.
The only lines I have changed are in the OnItemClickListener, so now the code is like this:
final FilteredListviewAdapter adapter = new FilteredListviewAdapter(AddAgentProfile.this, android.R.layout.simple_list_item_1, arraymls);
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
addProfile.setMLSId(mlslist.get(arraymls.indexOf(adapter.getItem(position))).getId());
textmls.setText(adapter.getItem(position));
mls.setText("");
dialog.dismiss();
}
});
list.setAdapter(adapter);
Now I call the function adapter.getItem instead of list.get
getItem should return the item inside the dataset at position:
#Override
public String getItem(int position) {
return list.get(position);
}

Categories

Resources