Where is the layout file in my ListView? - android

I am new to android and i am confused about something. I have created a simple ListView, here is my code:
public class MainActivity extends ListActivity {
ArrayList<String> listItems;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
listItems = new ArrayList<String>();
for(int i=0;i<20;i++){
listItems.add("List Item #"+i);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, listItems);
setListAdapter(adapter);
}
}
This correctly displays the list items on the screen, but here is my question: I have no xml files, no layouts in my project and i do not use setContentView function here. So why is this code working? How can it be displayed even though there is no xml files or layouts?
Thanks

You are using android.R.layout.simple_list_item_1 as the layout, which provides a TextView for you to work with. This is a layout provided by Android for simple lists of text.
If you want to make a more complex layout, you will/can define your own layout.

Android ships with default containers for list items.
As the name suggests, android.R.layout.simple_list_item_1 belongs to the android.R package and not to your app's resources.

Related

To populate a ListView with more than one "list" of information, would I create another adapter or use some other method?

I am just now learning about how listViews work. I currently have a ListView in which each row is an image, with two TextViews to its right. The top TextView is larger and contains a name, the bottom one is smaller and contains a surname. I currently have an ArrayAdapter set up properly to populate the names. However, I can't quite figure out how to also populate the surnames, as doing the same exact thing seems to not work. My code currently looks like this. The k ListView is the one I'm trying to implement for surnames. This is obviously an incorrect approach, though. If I run that code, it will no longer show the names, but only the surnames.
ListView l;
ListView k;
String[] names = {"Andrew","Billy","Charlie","Daniel","Eric","Frank","George","Hal"};
String[] surNames = {"Ainbinder","Brenton","Chen","Donovan","Epstein","Ferris","Gallivan","Higgins"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
l = (ListView) findViewById(R.id.listView1);
k = (ListView) findViewById(R.id.listView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.single_row, R.id.topLine, names);
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this, R.layout.single_row, R.id.secondLine, surNames);
l.setAdapter(adapter);
k.setAdapter(adapter2);
l.setOnItemClickListener(this);
}
The way you are implementing is not correct. You can follow the link below for correct implementation:
Multiple TextViews in the same List Item

Implementing ListView with icons on different items in android application

I am developing an android application in which i have to place 5 items with 5 icons in a list.Has anyone implemented it before?
If Yes,Can he help me how to implement it?
Thanks
tushar
you can try from this
Dynamic ListView in Android app
public class ListViewDemo extends ListActivity {
//LIST OF ARRAY STRINGS WHICH WILL SERVE AS LIST ITEMS
ArrayList<String> listItems=new ArrayList<String>();
//DEFINING STRING ADAPTER WHICH WILL HANDLE DATA OF LISTVIEW
ArrayAdapter<String> adapter;
//RECORDING HOW MUCH TIMES BUTTON WAS CLICKED
int clickCounter=0;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
adapter=new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
listItems);
setListAdapter(adapter);
}
//METHOD WHICH WILL HANDLE DYNAMIC INSERTION
public void addItems(View v) {
listItems.add("Clicked : "+clickCounter++);
adapter.notifyDataSetChanged();
}
}
android.R.layout.simple_list_item_1 is default list items layout supplied by android and >you can use this stock layout for non complex things.
listItems is an array list which holds the data shown in the ListView and all the >insertion and removal should be done on listItems the changes in list should reflect in >the view and thats handled by ArrayAdapter adapter which should be notified using
adapter.notifyDataSetChanged();
Adapter is instantiated with 3 paramters the context which could be your >activity/listactivity the layout of you individual list item and lastly the list which is >the actual data to be displayed in the list.

nullpointer on ListView (not using ListActivity)

I've created a layout in XML and it includes a ListView. I can use the ListView just find inside my AsyncTask. In onCreate, I did: listView = (ListView)findViewById(R.id.listView1);
And in onPostExecute of my AsyncTask, I did: listView.setAdapter(new OfferAdapter(Main.this, offers)); THIS WORKS JUST FINE.
But if I try listView.setDivider(null) in onCreate, then the app crashes with a nullpointer there.
How am I supposed to grab hold of my ListView when I'm not using ListActivity?
If findViewById is returning null, I'd make sure you are calling it after setContentView, and that you are using the correct ID.
One option you have is to set the divider in xml, ie:
android:divider="#drawable/icon".
If you want more control, verify that you are following this syntax in your activity:
public class DividerExampleActivity extends Activity {
ListView listView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView = (ListView) findViewById(R.id.listView1);
listView.setDivider(null);
}
}
I've examined the ListView.java source code, and setting it to null should be fine.
Another option might be to make yourself a very thin, transparent divider in xml (say 0.5dp).

Android listview adapter question

In my activity there is a listview containing lines of a text file. To populate this listview i created an arrayadapter in the onCreate(Bundle savedInstanceState) method. The listview is populated with items if there are lines in the file. The arrayadapter part looks like this:
final ListView lv1 = (ListView) findViewById(R.id.ListView01);
ArrayAdapter<String> adapter1;
adapter1 = new ArrayAdapter<String>(Main.this,R.layout.list_black_text,R.id.list_content, assignArr0);
lv1.setAdapter(adapter1);
adapter1.notifyDataSetChanged();
Then by clicking on Menu item1 (so now we are outside the onCreate(Bundle savedInstanceStat) method i write one line in the text file. For now everything works fine. If i exit and reopen the app the new line appears in the listview. But the listview is not refreshed after adding the new line, so at the end of the " case R.id.Menu1:" part.
So after adding one line to the file I want to refresh the listview by reading the lines from the file to an array then populating the listview with that array. Problem is that i cannot do that, even if i put the ArrayAdapter<String> adapter1; line outside the onCreate() method where the other arrays and variables are declared.
It does not see the listview (lv1 cannot be resolved).
If i define the ListView where the other variables are (ListView lv1;) this whole part of the program is not working.
I also need to empty the listview (the adapter?) by adapter1.clear(); or sg before populating it.
Any ideas are welcome.
If you need those variables to be global, you should declare them as
private final ArrayList<String> assignArr0 = new ArrayList<String>();
private ListView lv1;
private ArrayAdapter<String> adapter1;
#Override
public void onCreate(Bundle icicle)
{
lv1 = (ListView) findViewById(R.id.ListView01);
adapter1 = new ArrayAdapter<String>(Main.this,R.layout.list_black_text,
R.id.list_content, assignArr0);
lv1.setAdapter(adapter1);
}
You should set the adapter to the ListView only one time, inside the onCreate method of your activity, and the notifiyDatasetChanged should be called after you made changes to the list (the array used in your ListAdapter).
Say you have a method to add the recently read lines not yet included in your list. After adding the lines to the ArrayList used in the ListAdapter, you should call the notifyDatasetChanged method, to visualize the new items in the ListView:
private void addLinesToList(final String... newLines)
{
assignArr0.addAll(Arrays.asList(newLines));
adapter1.notifyDataSetChanged();
}
If you are reading the whole file every time it changes, and populate it from top (not just inserting the new lines), you have to first remove all the elements of the list, so the first line of the addLinesToList method should be:
assignArr0.clear();

Selectable width of list items in ListActivity

In an Android project I am working on I have a ListActivity that is set up like this:
public class myClass extends ListActivity {
public void onCreate(Bundle savedInstanceState) {
...
String[] menuItems = new String[] { "View", "Share", "Delete" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, menuItems);
setListAdapter(adapter);
...
}
}
The 3 menu items show up fine, but only the text portion of the item can be "clicked". How can I make it so that the entire row is "clickable"?
The width of "clickable" area is determined by the layout of the list and of it's items.
By layout I mean those XML files in res/layout.
You have to set android:layout_width="fill_parent" for ListView and the items.
I see that you are using a ListView, if you are not loading a custom layout for the list, then simple_list_item_1.xml should be the problem.
Please post that file if the problem persists.

Categories

Resources