Java – displays a list of strings in Android
I started developing for Android yesterday, so I'm a complete novice
I try to use listview, but I can't even see the virtual view This is what I've tried: activity_ main. In XML:
<ListView
android:id="@+id/wifiScanResList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/wifiScanButton" >
</ListView>
In the 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 now have two questions about my problem
First, do I use the right view to meet my needs? If not, what should I use?
Second, what is the reason why the virtual view does not appear? In addition, how can I simply add strings to the listview?
Solution
First, you need to create an ArrayList that stores all the strings
String[] values = new String[] { "Android","iPhone","WindowsMobile","BlackBerry","WebOS","Ubuntu","Windows7","Max OS X","Linux","OS/2","Android","WindowsMobile" };
final ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < values.length; ++i) {
list.add(values[i]);
}
Then you need to create an adapter from this ArrayList
ArrayAdapter adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list);
Then you need to set this adapter on listview
listview.setAdapter(adapter);
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
