This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
I have an ArrayList adapter wich used to load my listView.
My problem is that I want to change a pictures on the items in the list;
Until now I used a simple String array and didnt have any problem, but I had to change it to ArrayList wich loaded by an ORMlite selection and now my adapter doesent change the pictures...
I toast the text of the item wich is like ..."Asd"...
and in the adapter I use this:
if(textView.getText()=="Asd"){imageView.setImageResource(R.drawable.ic_asd);}
I also tried this:
if(values.get(position).getName()=="Asd")
{
imageView.setImageResource(R.drawable.ic_asd);
}
What is the problem? :S
Well, it's hard to say exactly what the issue is, but the main thing that's sticking out is that you're checking String equality wrong.
To check string equality, you should use Object.equals(Object obj); For example, textView.getText()=="Asd" should be textView.getText().toString().equals("Asd");
yes your problem is that you should use textview.getText().equals("Asd")
Use
if(textView.getText().toString().equals("Asd"))
I'm sure this is the problem ;)
Related
I'm starting learning Android and I want to know if there is some option in Android that let you modify each item or view (I don't know how it's named exactly, I mean each of the items from an ArrayList that you show in a ListView).
Well, I made a ListView that is going to show some books that were located in an ArrayList named "books".
I made a custom adapter that I associate to the ListView to show each item with the corresponding layout in the application. I also have a class "book" for each item that is going to be shown in the ListView.
Further, I made an Intent that I call from MainActivity with startForActivityResult(), that I process in Book class and that I return to MainActivity with all data of a book with the method setResult and got the information with onActivityResult() and the requestCode.
So I don't have any problem to add items to the ListView, just I have the problems if I want to modify some of the items (or views) that are located in the ListView (for example if I have title and author of a book, if I put some wrong information, I want have the option to change it).
I have that, in the same moment that you click on some of the items of the ListView, a new layout will be show to modify the information that it's wrong so I use the method setOnItemClickListener with onItemClick event on the custom adapter that I created before. Here it's where I call the new Intent to modify the wrong information with the method startActivityForResult().
I made the same as before to add a new item but, instead of add a new item with custom_adapter.addBook(title,author) I want to know if there is some option to made something like this: custom_adapter.modify(title,author) or custom_adapter.update(title,author), I mean, when you have modify all the items that were wrong of a book (for example an EditText that were "title") and you have all the information in the MainActivity class (because you returned it with setResult), how to put it again in the same item updating it in the custom_adapter and also in the ArrayList.
I searched it on the Internet but I didn't find anything.
I'm sorry if I have a poor English, but I expect that it can be understand.
Thank you very much!
If I am understanding your problem correctly - you could simply modify the ArrayList of type Book that is backing your ArrayAdapter.
So if you know what Book object you want to modify then you can simply make your changes to the Book object itself. As long as this Book is a reference to the same object that you originally added to the ArrayList you instantiated your ArrayAdapter with then you can then call custom_adapter.notifyDataSetChanged() to tell the adapter to redraw its childviews with the new data.
There are some good code samples on the Internet but you have to understand the code for your purposes. So...here is a start, look at Using an ArrayAdapter with ListView. The code shows the use of ArrayAdapter with getView() method. And I hope it shows how to define the listeners, which you need.
How about that for a start? Have fun...
Thank you very much for all help you gave to me. I'm very pleased with you. :)
Finally, I just send the info with a Bundle when I started the Intent, also with the position. And after, I just used this position to set the new info to my items (in my case, books).
Again, thank you very much ;)
This question already has an answer here:
What does Set<element> mean?
(1 answer)
Closed 9 years ago.
I was looking for some codes to make a Spinner then I noticed that they use an ArrayAdapter like this:
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(context,android.R.layout.simple_spinner_item );
My question is, what does the <> do? I investigate a bit and its not a cast (I think) adn i havent found something yet.
In short, ArrayAdapter<CharSequence> adapter; means ArrayAdapter is of the type CharSequence. You can have adapter of any kind, say, for example, ArrayAdapter<String>, here ArrayAdapter is of type String.
You can define your custom ArrayAdapter too, by creating a class that extends ArrayAdapter and override its methods as you want.
Assuming you know c and c++ we used arrays of data type char to store a stirng
Same way when you want to store array or string you create something like this
String[] myArray = {"abc", "def", "ghi"} ;
Overloading in java helps is using same function defined in a class with different parameters to do different set of instructions which are changed as per the arguments as received via calls.
Same way when ArrayAdapter is called the dataType defines the type of array Adapter
As you might want to think it as
String ArrayAdapter variableName = new String ArrayAdapter () ;
But as u can notice we are trying to define two things i.e. The variable is a string but the variable is also an ArrayAdapter, this creates mess for java jdk developers to change the rules of compiling java files.
So they tried to make something like
ArrayAdapter<String> variableName = new ArrayAdapter<String> () ;
Hope i helped..
In my application i have customListView ,i need to get the first value of list view(i.e. Zero position value) when very first time activity is loaded without using OnItemClickLiseners and it is focusable.How Can i do?,please can any one help me.
Thanking in Advance.
You can either use
listView.getChildAt(0)
or
listView.getChildAt(listView.getFirstVisiblePosition());
This is based on the assumption that you need to pull out values of the item that is at the first (0th) position in the ListView.
Since you are using a Custom ListView, you should also have a custom Adapter (possibly an ArrayAdapter or a BaseAdapter) that you use to show the data etc.
In any case, just check for this in the getView():
if (position == 0) {
// GET WHAT EVER DATA YOU NEED OUT OF THE ITEM.
}
If you can add more info to the OP about how you deal with the data, perhaps this answer can be made a little bit more useful. For example, do you use a POJO class to bind the data to the ListView?
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
how to clear my listview in android
In android, i have used a listadapter, simple adapter and passed an array and i want to clear the listview contents? Instead of clearing , it is appending the records to my listview.
Grab my Custom adapter from here. TestAdapter
Now add a method like this..
public void clearAdapter()
{
deviceNames.clear();
selected.clear();
notifyDataSetChanged();
}
Now call youListAdapter.clearAdapter() from you Activity.
Simply write listView.setAdapter(null)
Use notifydataSetInValidated() on your adapter to notify your listview that the data set is not longer valid. or set your addapter to null : l.setAdapter(null);
How to I send an String array to a listView from the code and have the emulator display it?
That is a very general question... but the basic idea would be to create one of the usual Adapters, ArrayAdapter possibly in this case, initialize it with the String array and then call the ListView's setAdapter() method with that ArrayAdapter.