Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying list of strings in Android

I started developing for Android yesterday, so I am a complete newbie. I am using ADT bundle that works with Eclipse. What I want to do is I have got an array of strings. I would like to display them in a scroll supported view. I found ListView can do that but all tutorials that I found are extremely complex and explaining nested views.

I tried using ListView but I cannot even see the dummy view. Here is what I have tried: activity_main.xml:

<ListView
        android:id="@+id/wifiScanResList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/wifiScanButton" >
</ListView>

in MainActivity Class:

private ListView wifiListView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.

    getMenuInflater().inflate(R.menu.main, menu);
    wifiListView = (ListView) findViewById(R.id.wifiScanResList);
    wifiListView.setEnabled(true);

    return true;
}

I have got two questions in regards to my problem now.

First one is, am I using the right view for my needs? If not what should I use?

Second one is, what is the reason that dummy view does not appear? Also how can I simply add strings into ListView?

like image 576
Sarp Kaya Avatar asked Dec 23 '13 19:12

Sarp Kaya


People also ask

What is a list view in android?

A list view is an adapter view that does not know the details, such as type and contents, of the views it contains. Instead list view requests views on demand from a ListAdapter as needed, such as to display new views as the user scrolls up or down. In order to display items in the list, call setAdapter(android.


1 Answers

first you need to create a ArrayList that would store all the strings.

String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
    "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
    "Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux",
    "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2",
    "Android", "iPhone", "WindowsMobile" };

final ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < values.length; ++i) {
  list.add(values[i]);
}

then you would need to create an adapter from this arraylist

ArrayAdapter adapter = new ArrayAdapter<String>(this,  
      android.R.layout.simple_list_item_1, 
      list);

then you would need to set this adapter on the listview

listview.setAdapter(adapter);

please see this link for a simple list view implementation:

http://androidexample.com/Create_A_Simple_Listview_-_Android_Example/index.php?view=article_discription&aid=65&aaid=90

like image 68
gaurav5430 Avatar answered Oct 05 '22 02:10

gaurav5430