ListView listView = (ListView) findViewById(R.id.listview);
It would be understandable for me if it was something like
ListView listView = new ListView()
But I don't understand what the RHS of ListView listView = (ListView) findViewById(R.id.listview) means; I know that LHS creates a reference variable named listView which will contain the reference to an object of ListView.
To the best of my understanding, is it retrieving a view by findViewById() and parsing to a ListView object (how can an object of one type be even parsed into an object of another type) , and then assigning a reference to that ListView object in listView reference variable?
Thank you in advance.
R.id.listview
here in one of your xml layouts you name a list as "listview"
android assigns id to every name you allot. id are stored in R java file
it would be like
public static final int listview=0x7f050002;
even you could directly use this int value in place of R.id.listview
findViewById(R.id.listview);
this will tell your activity to find a view (whose id is stored as R.id.listview)
(listview)
you are casting your view as a LISTVIEW object
and assigning it to the
listView
object of class ListView
Related
Is it possible to show a thumbnail with a Array adapter in a list? The thumbnail is always the same.
final ArrayAdapter myAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1 );
ListView myList = (ListView) findViewById(R.id.list);
myList.setAdapter(myAdapter);
I populate my list with volley.
ArrayAdapter is usually used for populating a ListView widget from array or list of primitive values like String. When you are creating an ArrayAdapter with ArrayAdapter myAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1 ); as the second parameter you have R.layout.simple_list_item_1 which is a layout with only one TextView, so if you need a ImageView alongside a TextView, you will need to create new view that will contain TextView and ImageView widgets and create custom class that will extend Adapter where you will use the newly created view and populate it's views with your content.
I have a normal ListView:
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:dividerHeight="10dp" />
And I populate that listview with an ArrayList of Alert objects:
// Get ListView object from XML
listView = (ListView) findViewById(android.R.id.list);
// Create a new Adapter
ArrayAdapter<Alert> adapter = new ArrayAdapter<Alert>(this, android.R.layout.activity_list_item, android.R.id.text1, DataSourceHandler.getAlertsList());
// Assign adapter to ListView
listView.setAdapter(adapter);
DataSourceHandler.getAlertsList() returns to me the ArrayList of objects and in my ListView I now see two items like this:
path.to.object#41a92a48
path.to.object#41a92cc8
Those two Alert objects has a method called getName() which returns a String that I want to use as the displaying name for this item in the list instead of what I get above.
How can I do this?
As was already mentioned in the comments, the standard ArrayAdapter implementation calls toString() on every object in your list and populates the view using the resulting String. The simplest solution will be to override toString() by calling your getName() method inside it. If it doesn't suit your needs then you should create a custom adapter.
Yes,override toString() is the solution, example of doing that:
Example
I have a listview with a lot of variables (about 1000). How can I get the position of a particular string variable (eg "Washington")?
That is, I want to say how knowing the name of the variable (name of city) to get its position in the listview.
Depends on the ListAdapter that you're using. If you have an ArrayAdapter and a simple array of strings the code would be something like:
ArrayAdapter arrayAdapter = (ArrayAdapter) listView.getAdapter();
int position = arrayAdapter.getPosition("Washington");
I asked a question before about splitting string but maybe it wasn't clear enough.
I made a simple activity which has an example to what my problem is.
I have a message and it's a long one coming from a server.
I need to split this message and put it inside a listview, I'll show you my code.
public class Page1 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity6);
String message = "0---12,,,2013-02-12 08:04,,,this is a test,,,0---11,,,2013-02-12 08:05,,,and this is why it is damaged,,,0---10,,,2013-02-12 08:06,,,what comes from select data randomly";
String[] variables = message.split(",");
ListView listView1 = (ListView) findViewById(R.id.listView12);
String[] items = { variables.toString() };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, items);
listView1.setAdapter(adapter);
}
}
Now let's say that the split is commas ", " so it will be
0---12 ------->ID1
2013-02-12 08:04 ------------>date1
this is a test ----------->subject1
0---11 ------->ID2
2013-02-12 -8:05 ------------>date2
and this is why it is damaged ----------->subject2
And so on, now what I can't do is that I want to put these strings in a loop and write them to a listview such that the subject1 should be in item1 and date1 should be in subitem1 like this
Subject1
Date1
------
Subject2
Date2
------
This is how the listview should look like
Can anyone help me with this please?
You would need to create a custom ArrayAdapter to populate a ListView from your objects the way you want.
The advantage of this technic is that you gain a Views recycle mechanism that will recycle the Views inside you ListView in order to spend less memory.
In Short you would have to:
1. Create an object that represents your data for a single row.
2. Create an ArrayList of those objects.
3. Create a layout that contains a ListView or add a ListView to you main layout using code.
4. Create a layout of a single row.
5. Create a ViewHolder that will represent the visual aspect of you data row from the stand point of Views.
6. Create a custom ArrayAdapter that will populate the rows according to you needs, in it you will override the getView method and use the position parameter you receive for the corrent row View to indicate the row index.
7. Finally assign this ArrayAdapter to your ListView in onCreate.
You can get an idea of how to implement this by reading this blog post I wrote:
Create a Custom ArrayAdapter
Please note that ArrayAdaper is designed for items containing only one single TextView. From the docs:
A concrete BaseAdapter that is backed by an array of arbitrary objects. By default this class expects that the provided resource id references a single TextView
Consider subclassing ArrayAdapter (docs) and override its getView method.
I have a list of ListItems's in my listview. The ListItem has a name instance variable. I am trying to get the text of each of the items just to be the name.
How can I do this?
public class List extends ListActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<ListItem>(this, R.layout.list_item, Previousclass.LinkedList-from-previous-class));
Right now it is just displaying the memory location of each item i.e. ListItem#47234c10
You have to extend ArrayAdapter to implement getView(). The ArrayAdapter implementation is returning the toString() value of your ListItem object.
Or alternatively stringify your data before adding it to the adapter.
What is Previousclass.LinkedList-from-previous-class ? Is it a list of your items?
If yes,
Your list of items should be a list of names of items. Your resulting screen should have listview in it and the adapter you created should be used this way
result.setListAdapter(Youradapter)
I am just giving you sample based on what code you have posted. If you need details, please elaborate your code.
result.setListAdapter(new ArrayAdapter<ListItem>(this, R.layout.list_item, Previousclass.LinkedList-from-previous-class))