ListView add item click item basic Android example code

Definition of ListView in layout main.xml file Android example

 
<ListView android:id="@+id/idListView"
        android:background="#7700CC00"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent" />

Code in Main.java ListView example source code Java Android

public class Main extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
 
        ListView mlistView = (ListView) findViewById(R.id.idListView);
        mlistView.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, 
                new String[] {"Game", "Help", "Home site"}));
 
        mlistView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
              // When clicked, show a toast with the TextView text Game, Help, Home
              Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
                  Toast.LENGTH_SHORT).show();
            }
          });
     }
}
This entry was posted in Android Examples Code and tagged , , , . Bookmark the permalink.


Comments are closed.



8,628 views