Android Open Source - 2014-Droid-code Simple L V






From Project

Back to project page 2014-Droid-code.

License

The source code is released under:

GNU General Public License

If you think the Android project 2014-Droid-code listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package cs534.sample.simplelistview;
//w  w  w  .  j a  va  2s.  co  m
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

/*
 * Example 2 for ListView
 * ListView is an AbsListView, AbsListView is an AdapterView,  AdapterView is a ViewGroup, ViewGroup is a View
 * ArrayAdapter is a BaseAdapter
 * 
 * Second  example of a ListView, populated by an array created in line,
 * the ListView responds to item click and long click events, we dynamically modify the data  
 */
public class SimpleLV extends Activity {
  private List<String> data;
  private int counter = 0;
  private ListView lv;
  private Button addbutton1, addbutton2;
  private ArrayAdapter<String> aa;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    /*
     * setContentView(): inflate the main layout (UI) from activity_main.xml
     * inflation: xml -> ojbect(s)
     */
    setContentView(R.layout.activity_simple_lv);

    // create and populate our array
    data = new ArrayList<String>();
    data.add("ONE");
    data.add("two");
    data.add("three");
    data.add("biscuit");

    // get a reference to the listview object
    lv = (ListView) findViewById(R.id.list);

    // set the item click Listener for the ListView
    lv.setOnItemClickListener(delOnClick);

    // set the item long click listener for the ListView
    lv.setOnItemLongClickListener(longClick);

    /*
     * ListView, GridView, GalleryView containers are types of ArrayAdapter
     * controls ArrayAdapter is a BaseAdapter, Adapters bind data to View
     * objects
     * 
     * the layout xml template is builtin: R.Layout.simple_list_item_1
     * 
     * documentation:
     * http://developer.android.com/reference/android/R.layout.html
     * 
     * source:
     * https://github.com/android/platform_frameworks_base/blob/master
     * /core/res/layout/simple_list_item_1.xml
     * 
     * the data in this case is a String array that we just created
     */
    aa = new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_1, data);
    /*
     * setAdapter() sets the data & layout behind the ListView
     */
    lv.setAdapter(aa);

    // add listeners for our two buttons
    addbutton1 = (Button) findViewById(R.id.addUsingAdapter);
    addbutton1.setOnClickListener(addUsingAdapter);

    addbutton2 = (Button) findViewById(R.id.addUsingList);
    addbutton2.setOnClickListener(addUsingList);

  } // onCreate()

  /*
   * addUsingAdapter() this listener is attached to a button, we add data to
   * the ListView using the adapter.
   */
  private OnClickListener addUsingAdapter = new OnClickListener() {

    public void onClick(View v) {
      counter++;
      /*
       * using the adapter we add an element we could also clear the list
       * ArrayAdapter.clear() set it to a new Collecton
       * ArrayAdapter.addAll(Collection)
       */
      aa.add("via adapter " + counter);
      /*
       * ArrayAdapter.notifyDataSetChanged() refreshes the data by telling
       * the observer of the adapter that the contents have changed, so
       * that the observer (ListView in this case) can decide what to
       * refresh.
       * 
       * call after ArrayAdapter.add() .insert() .remove() .clear()
       * 
       * Here we explicitly call notifyDataSetChanged() we could also have
       * the adapter automatically call it
       * ArrayAdapter.setNotifyOnChange()
       */
      aa.notifyDataSetChanged();
      Toast.makeText(getApplicationContext(),
          "List size post adapter add " + data.size(),
          Toast.LENGTH_SHORT).show();
    }
  }; // addUsingAdapter()
  /*
   * addUsingAdapter() this listener is attached to a button, we add data to
   * the ListView using the list
   */
  private OnClickListener addUsingList = new OnClickListener() {

    public void onClick(View v) {
      counter++;
      // using the list we add an element
      data.add("via list " + counter);
      /*
       * ListView.invalidateViews() rebuilds and redraws all child item
       * views, whether or not they have changed, so it is more expensive
       * note you can have 100 items in your list but fit only 10 views on
       * your display.
       */
      lv.invalidateViews();
      Toast.makeText(getApplicationContext(),
          "List size post list  add " + data.size(),
          Toast.LENGTH_SHORT).show();
    }
  }; // addUsingList

  /*
   * delOnClick() this listener is attached to the ListView when an item is
   * clicked we delete the item in the list
   */
  private OnItemClickListener delOnClick = new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position,
        long id) {
      /*
       * delete the list item, we could also use ArrayAdapter.remove(T
       * object);
       */
      data.remove(position);
      // notify the observer
      aa.notifyDataSetChanged();
    }
  }; // delOnClick
  /*
   * longClick() this listener is attached to the ListView when an item is
   * held on long click we do not modify the data we Toast the position
   */

  private OnItemLongClickListener longClick = new OnItemLongClickListener() {
    public boolean onItemLongClick(AdapterView<?> parent, View view,
        int position, long id) {
      Toast.makeText(getApplicationContext(),
          "Long Click position " + position, Toast.LENGTH_SHORT)
          .show();
      /*
       * data has not changed so no need for lv.invalidateViews(); or
       * aa.notifyDataSetChanged();
       */
      // Return true to consume the click event.
      return true;
    }
  }; // longClick
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_simple_lv, menu);
    return true;
  }
  // you may swap to  the COUNTRIES array defined below it is a bigger array
  // than countries_array

  public boolean onOptionsItemSelected(MenuItem item)
  {
    switch (item.getItemId())
    {
    case R.id.help:
      createShowDialogue();
      return true;
    default:
      return false;
    }
  } // onOptionsItemSelected
  /*
   * Create and show an alert dialog for help information
   */
  private void createShowDialogue() {

    // 1. Instantiate an AlertDialog.Builder with its constructor
    AlertDialog.Builder builder = new AlertDialog.Builder(this);

    // 2. Chain together various setter methods to set the dialog characteristics
    builder.setMessage(R.string.dialog_message)
    .setTitle(R.string.help)
    .setCancelable(true);
    // 3. Get the AlertDialog from create()
    AlertDialog dialog = builder.create();
    // 4.  set Buttons, not used here
    // 5.  Display the dialog
    dialog.show();
  } // createDialogue
} // SimpleLV class





Java Source Code List

ca.campbell.httpexample.HttpExample.java
ca.campbell.httpexamplepost.HttpsExamplePOST.java
ca.campbell.layoutprogrammatically.MainActivity.java
ca.campbell.networkcheckstatus.MainActivity.java
ca.campbell.optionsmenu.Activity2.java
ca.campbell.optionsmenu.Activity2.java
ca.campbell.optionsmenu.Activity3.java
ca.campbell.optionsmenu.MainActivity.java
ca.campbell.optionsmenu.MainActivity.java
ca.campbell.simplegridview.MainActivity.java
ca.campbell.week2_rw_views.Activity2.java
ca.campbell.week2_rw_views.MainActivity.java
com.androidbook.simpleasync.ChoiceActivity.java
com.androidbook.simpleasync.SimpleAsyncActivity.java
com.androidbook.simpleasync.SimpleNoBGThread.java
com.androidbook.simpleasync.SimpleThreadActivity.java
com.cookbook.internet.search.GoogleSearch.java
com.introtoandroid.simplefragments.FieldNoteListFragment.java
com.introtoandroid.simplefragments.FieldNoteViewActivity.java
com.introtoandroid.simplefragments.FieldNoteWebViewFragment.java
com.introtoandroid.simplefragments.SimpleFragmentsActivity.java
com.introtoandroid.simplelayout.FrameLayoutActivity.java
com.introtoandroid.simplelayout.GridLayoutActivity.java
com.introtoandroid.simplelayout.LinearLayoutActivity.java
com.introtoandroid.simplelayout.MenuActivity.java
com.introtoandroid.simplelayout.MultipleLayoutActivity.java
com.introtoandroid.simplelayout.RelativeLayoutActivity.java
com.introtoandroid.simplelayout.SimpleLayoutActivity.java
com.introtoandroid.simplelayout.TableLayoutActivity.java
com.introtoandroid.viewsamples.ButtonsActivity.java
com.introtoandroid.viewsamples.ContainersActivity.java
com.introtoandroid.viewsamples.EventsActivity.java
com.introtoandroid.viewsamples.FormsActivity.java
com.introtoandroid.viewsamples.IndicatorsActivity.java
com.introtoandroid.viewsamples.MenuActivity.java
com.introtoandroid.viewsamples.PickersActivity.java
com.introtoandroid.viewsamples.TextDisplayActivity.java
com.introtoandroid.viewsamples.TextInputActivity.java
com.introtoandroid.viewsamples.ViewSampleActivity.java
cs518.sample.activityLifecycle.Activity2.java
cs518.sample.activityLifecycle.MyActivityLifeCycleActivity.java
cs518.sample.database.AddStudent.java
cs518.sample.database.DBHelper.java
cs518.sample.database.DatabaseActivity.java
cs518.sample.database.Thirty.java
cs518.sample.dbcursoradapter.AddStudent.java
cs518.sample.dbcursoradapter.DBHelper.java
cs518.sample.dbcursoradapter.DatabaseActivity.java
cs518.sample.dbcursoradapter.Thirty.java
cs518.sample.localisation.MainActivity.java
cs518.sample.multiactivity.Activity1.java
cs518.sample.multiactivity.Activity2.java
cs518.sample.multiactivity.Activity3.java
cs518.sample.multiactivity.Activity4.java
cs518.sample.multiactivity.Activity5.java
cs518.sample.multiactivity.Activity6.java
cs518.sample.multiactivity.Constants.java
cs518.sample.usecalendarcontentprovider.MainActivity.java
cs518.sample.usecontactcontentprovider.MainActivity.java
cs518.sample.usecontactcontentprovidercursorloader2.MainActivity.java
cs518.sample.usemediastorecontentprovider.MainActivity.java
cs518.samples.imageswap.MainActivity.java
cs518.samples.sharedpreferences.MainActivity.java
cs534.sample.dbAsyncTask.AddStudent.java
cs534.sample.dbAsyncTask.DBHelper.java
cs534.sample.dbAsyncTask.DatabaseActivity.java
cs534.sample.dbAsyncTask.Thirty.java
cs534.sample.implicitintents.MainActivity.java
cs534.sample.multithread.MultiThread.java
cs534.sample.simplelistview.SimpleLV.java
cs534.samples.simplestlv.MainActivity.java