The problem that I am having is that once I perform a search on my ListView, I am unable to delete or add anything else to my List. Additionally, I am getting an IndexOutofBounds error. My code is as follows:
public class MainActivity extends AppCompatActivity {
public ArrayList<String> array;
public ArrayAdapter<String> adapt;
public ListView list;
public EditText search;
#Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.list_item);
array = new ArrayList<>();
adapt = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, array);
list.setAdapter(adapt);
setupListViewListener();
search();
}
public void onAddItem(View v) {
EditText edit = (EditText) findViewById(R.id.edit);
String item = edit.getText().toString();
adapt.add(item);
edit.setText("");
}
public void setupListViewListener() {
list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View v, int i, long l) {
array.remove(i);
adapt.notifyDataSetChanged();
return true;
}
});
}
public void search()
{
search = (EditText) findViewById(R.id.inputSearch);
search.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
adapt.getFilter().filter(cs);
adapt.notifyDataSetChanged();
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}
#Override
public void afterTextChanged(Editable arg0) {
}
});
}
public void deleteAll(View view) {
adapt.clear();
adapt.notifyDataSetChanged();
}
}
Related
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
In the code below the filtering is not case-sensitive. I would like the filtering would be case-sensitive, for example when I write ABC in my app it would not ALSO display aBc, Abc, aBc, abC and abc:
private ListView lv;
ArrayAdapter<String> adapter;
EditText inputSearch;
ArrayList<HashMap<String, String>> productList;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String products[] = {"ABC", "abc", "aBc", "Abc", "abC"
};
lv = (ListView) findViewById(R.id.list_view);
inputSearch = (EditText) findViewById(R.id.inputSearch);
adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, products);
lv.setAdapter(adapter);
inputSearch.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
MainActivity.this.adapter.getFilter().filter(cs);
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
}
#Override
public void afterTextChanged(Editable arg0) {
}
});
}
}
//You can try this
inputSearch.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
MainActivity.this.adapter.getFilter().filter(cs.toLowerCase(Locale.getDefault()));
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
}
#Override
public void afterTextChanged(Editable arg0) {
}
});
I want to set text from selected item from spinner to textview, But when i tried to do application craches. Can any one help me .
public class MainActivity extends AppCompatActivity {
TextView txt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt=(TextView) findViewById(R.id.txt1) ;
ArrayList<ItemData> list=new ArrayList<>();
list.add(new ItemData("Cash",R.drawable.paymentcashnew));
list.add(new ItemData("Paytm",R.drawable.paymentpaytmnew));
Spinner sp=(Spinner)findViewById(R.id.select_cash);
String result = sp.getSelectedItem().toString();
txt.setText(result);
SpinnerAdapter adapter=new SpinnerAdapter(this,
R.layout.spinner,R.id.txt,list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp.setAdapter(adapter);
}
}
sp.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String result = sp.getSelectedItem().toString();
txt.setText(result);
}
});
This may help Frnd.
spinner has no "getSelectedItem". https://developer.android.com/reference/android/widget/Spinner.html https://developer.android.com/guide/topics/ui/controls/spinner.html
and here's how you get the text-value of the selected row: https://stackoverflow.com/a/15545909/3022387
spin.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
String imc_met=spin.getSelectedItem().toString();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Implement spinner.setOnItemClickListener and then find text inside that method like this.
spinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String result = sp.getSelectedItem().toString();
txt.setText(result);
}
});
You can't get selected Item before setting adapter.Try this below code
public class MainActivity extends AppCompatActivity {
final TextView txt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt=(TextView) findViewById(R.id.txt1) ;
ArrayList<ItemData> list=new ArrayList<>();
list.add(new ItemData("Cash",R.drawable.paymentcashnew));
list.add(new ItemData("Paytm",R.drawable.paymentpaytmnew));
Spinner sp=(Spinner)findViewById(R.id.select_cash);
SpinnerAdapter adapter=new SpinnerAdapter(this,
R.layout.spinner,R.id.txt,list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp.setAdapter(adapter);
sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Object item = parent.getItemAtPosition(position);
String result = sp.getSelectedItem().toString();
txt.setText(result);
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
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?
Hi, Everybody.My goal is when I want to click on a specific item from the ListView, it opens a new activity that I've programmed. For example: when I click on "João", I wish it opens a class called Joao. I've tried using a simple code, but it doesn't work. Nothing happens when I click on João's name from ListView.
This is my code that I'm using:
public class Searchsort extends Activity {
private ListView lv1;
private EditText ed;
private String lv_arr[]={"John","Mary","Carl","Rose","Charlie","Allan", "João"};
private ArrayList<String> arr_sort= new ArrayList<String>();
int textlength=0;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
lv1 = (ListView)findViewById(R.id.ListView01);
ed = (EditText)findViewById(R.id.EditText01);
// By using setAdpater method in listview we an add string array in list.
lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , lv_arr));
ed.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
textlength=ed.getText().length();
arr_sort.clear();
for(int i=0;i<lv_arr.length;i++) {
if(textlength<=lv_arr[i].length()) {
if(ed.getText().toString().equalsIgnoreCase((String) lv_arr[i].subSequence(0, textlength))) {
arr_sort.add(lv_arr[i]);
}
}
}
lv1.setAdapter(new ArrayAdapter<String> (Searchsort.this,android.R.layout.simple_list_item_1 , arr_sort));
}
});
}
public void onListItemClick(ListView parent, View v, int position, long id) {
if ("João".equals(lv_arr[position])) {
Intent myIntent = new Intent(Searchsort.this, Joao.class);
startActivity(myIntent);
}
}
}
lv1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(Searchsort.this, Joao.class);
startActivity(intent);
}
}
});
Use setOnItemClickListener for the ListView and handle activity opening there.
As Niko Suggested use setOnItemClickListener() in the following manner.
public class Searchsort extends Activity {
private ListView lv1;
private EditText ed;
private String lv_arr[]={"John","Mary","Carl","Rose","Charlie","Allan", "João"};
private ArrayList<String> arr_sort= new ArrayList<String>();
int textlength=0;
private String getItem;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
lv1 = (ListView)findViewById(R.id.listView1);
ed = (EditText)findViewById(R.id.EditText01);
// By using setAdpater method in listview we an add string array in list.
lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , lv_arr));
// Here I have set Listener for listview
lv1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
// Here I am getting the selected item of listview.
getItem = (arg0.getItemAtPosition(arg2).toString());
if(getItem.equals("John"))
{
Intent i = new Intent(Searchsort.this,John.class);
startActivity(i);
}
}
});
ed.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
textlength=ed.getText().length();
arr_sort.clear();
for(int i=0;i<lv_arr.length;i++) {
if(textlength<=lv_arr[i].length()) {
if(ed.getText().toString().equalsIgnoreCase((String) lv_arr[i].subSequence(0, textlength))) {
arr_sort.add(lv_arr[i]);
}
}
}
lv1.setAdapter(new ArrayAdapter<String> (Searchsort.this,android.R.layout.simple_list_item_1 , arr_sort));
}
});
}
}
Please check your Androidmanifest.xml.
Add declare like:
<activity android:name=".CLASS_NAME"> </activity>
You must declare all activitIes like that.
try this..
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if(position == 1)
{
Intent myIntent = new Intent(YourActivity.this, SecondActivity.class);
startActivityForResult(myIntent, 0);
}
if(position == 2)
{
Intent myIntent = new Intent(YourActivity.this, ThirdActivity.class);
startActivityForResult(myIntent, 0);
}
}
});
Hope this will help you :-)