Welcome
Login / Register

Basic ListView Demo: Android Programming

Thanks! Share it with your friends!

URL

You disliked this video. Thanks for the feedback!

Sorry, only registred users can create playlists.
URL


Added by taekwonv in Demo
3 Views

Description

Demonstration on how to create a simple list view and populate it with text elements. Full Java code below.\r
\r
package ca.demo.demolistview;\r
\r
import android.app.Activity;\r
import android.os.Bundle;\r
import android.view.View;\r
import android.widget.AdapterView;\r
import android.widget.ArrayAdapter;\r
import android.widget.ListView;\r
import android.widget.TextView;\r
import android.widget.Toast;\r
\r
// Array of options --} ArrayAdapter --} ListView\r
// List view: {views: da_items.xml}\r
\r
public class MainActivity extends Activity {\r
\r
@Override\r
protected void onCreate(Bundle savedInstanceState) {\r
super.onCreate(savedInstanceState);\r
setContentView(R.layout.ivity_main); populateListView();\r
registerClickCallback();\r
}\r
\r
\r
private void populateListView() {\r
// Create list of items\r
String[] myItems = {Blue, Green, Purple, Red}; // Build Adapter\r
// TODO: CHANGE THE [[ to a less than, ]] to greater than.\r
ArrayAdapter[[String]] adapter = new ArrayAdapter[[String]]( this, // Context for the ivity. R.layout.da_item, // Layout to use (create) myItems); // Items to be displayed // Configure the list view.\r
ListView list = (ListView) findViewById(R.id.listViewMain);\r
list.setAdapter(adapter);\r
}\r
\r
private void registerClickCallback() {\r
ListView list = (ListView) findViewById(R.id.listViewMain);\r
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {\r
@Override\r
// TODO: CHANGE THE [[ to a less than, ]] to greater than.\r
public void onItemClick(AdapterView[[?]] paret, View viewClicked, int position, long id) { TextView textView = (TextView) viewClicked; String message = You clicked #+ position + , which is string:+ textView.getText().toString(); Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();\r
}\r
}); \r
}\r
}

Post your comment

RSS