title: How to update list-view data at run time in android
body: <p>I have list-view with check-box which loads data from database. And i have a button too, when i press a button spinner is popping up with 2 messages "EDIT" and "DELETE". Delete is working perfectly fine. and if i select Edit option and press OK then Edit-Text is coming in alert window with two button Ok and Cancel.</p>
  
  <p>Now i want that Whatever i enter in edit-text and press Ok button a list-view data which is selected (Checked) should update with new text whatever i have entered in edit-text. So how to achieve this. Please help me. Any help would be appreciated.  Thanks in advance.</p>
  
  <p>My code for creating Database:</p>
  
  <pre><code>public class DataManipulatorClass {
  
  public static final String KEY_ROWID = "id";
  private static final String DATABASE_NAME = "mydatabaseclass.db";
  private static final int DATABASE_VERSION = 1;
  static final String TABLE_NAME = "newtableclass";
  private static Context context;
  static SQLiteDatabase db;
  OpenHelper openHelper = null;
  
  private SQLiteStatement insertStmt;
  
  private static final String INSERT = "insert into " + TABLE_NAME
          + "(classname) values (?)";
  
  public DataManipulatorClass(Context context) {
      DataManipulatorClass.context = context;
      OpenHelper openHelper = new OpenHelper(DataManipulatorClass.context);
      DataManipulatorClass.db = openHelper.getWritableDatabase();
      this.insertStmt = DataManipulatorClass.db.compileStatement(INSERT);
      //this.db = openHelper.getWritableDatabase();
  }
  
  public long insert(String classname) {
      this.insertStmt.bindString(1, classname);
      return this.insertStmt.executeInsert();
  }
  
  public void close() {
      if (openHelper != null) {
          openHelper.close();
      }
  }
  
  public void deleteAll() {
      db.delete(TABLE_NAME, null, null);
  }
  
  public List&lt;String[]&gt; selectAll() {
      List&lt;String[]&gt; list = new ArrayList&lt;String[]&gt;();
      Cursor cursor = db.query(TABLE_NAME,
              new String[] { "id", "classname" }, null, null, null, null,
              "classname asc");
      int x = 0;
      if (cursor.moveToFirst()) {
          do {
              String[] b1 = new String[] { cursor.getString(0),
                      cursor.getString(1) };
              list.add(b1);
              x = x + 1;
          } while (cursor.moveToNext());
      }
      if (cursor != null &amp;&amp; !cursor.isClosed()) {
          cursor.close();
      }
      cursor.close();
      return list;
  }
  
  public boolean delete(long rowId) {
      /** this method deletes by id, the first column in your database */
      return db.delete(TABLE_NAME, KEY_ROWID + "=" + rowId, null) &gt; 0;
  }
  
  private static class OpenHelper extends SQLiteOpenHelper {
      OpenHelper(Context context) {
          super(context, DATABASE_NAME, null, DATABASE_VERSION);
      }
  
      @Override
      public void onCreate(SQLiteDatabase db) {
          db.execSQL("CREATE TABLE " + TABLE_NAME
                  + " (id INTEGER PRIMARY KEY, classname TEXT)");
      }
  
      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
          db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
          onCreate(db);
      }
  }
  }
  </code></pre>
  
  <p>My Activity class:</p>
  
  <pre><code>public class Classes extends Activity implements OnClickListener {
  
  ImageView imageViewNewClass, imageViewDelete;
  ListView mListView;
  String[] stg1;
  List&lt;String[]&gt; names2 = null;
  DataManipulatorClass dataManipulator;
  ArrayAdapter&lt;CharSequence&gt; adapterSpinner;
  ArrayAdapter&lt;String&gt; adapter;
  
  /** Sliding Menu */
  boolean alreadyShowing = false;
  private int windowWidth;
  private Animation animationClasses;
  private RelativeLayout classesSlider;
  LayoutInflater layoutInflaterClasses;
  
  @SuppressWarnings("deprecation")
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.classes);
  
      imageViewNewClass = (ImageView) findViewById(R.id.newclass);
      imageViewDelete = (ImageView) findViewById(R.id.deletemenu);
      mListView = (ListView) findViewById(R.id.displaydata);
  
      Display display = getWindowManager().getDefaultDisplay();
      windowWidth = display.getWidth();
      display.getHeight();
      layoutInflaterClasses = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  
      imageViewDelete.setOnClickListener(new OnClickListener() {
          @Override
          public void onClick(View v) {
              deleteMenuSpinner();
          }
  
          private void deleteMenuSpinner() {
              AlertDialog.Builder alertDialog = new AlertDialog.Builder(
                      Classes.this);
  
              final Spinner spinnerDelete = new Spinner(Classes.this);
              alertDialog.setView(spinnerDelete);
  
              adapterSpinner = ArrayAdapter.createFromResource(Classes.this,
                      R.array.delete_menu,
                      android.R.layout.simple_spinner_item);
              adapterSpinner
                      .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
              spinnerDelete.setAdapter(adapterSpinner);
  
              alertDialog.setPositiveButton("Ok",
                      new DialogInterface.OnClickListener() {
                          public void onClick(DialogInterface dialog,
                                  int which) {
  
                                      Delete operation code.....
                                  }
  
                              } else {
                                  AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                                          Classes.this);
                                  final EditText updateClass = new EditText(
                                          Classes.this);
                                  LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                                          LinearLayout.LayoutParams.MATCH_PARENT,
                                          LinearLayout.LayoutParams.MATCH_PARENT);
                                  updateClass.setLayoutParams(lp);
                                  alertDialogBuilder.setView(updateClass);
                                  alertDialogBuilder
                                          .setTitle("Edit Operation");
                                  alertDialogBuilder
                                          .setMessage("Enter New Class Name")
                                          .setCancelable(false)
                                          .setPositiveButton(
                                                  "Yes",
                                                  new DialogInterface.OnClickListener() {
                                                      public void onClick(
                                                              DialogInterface dialog,
                                                              int id) {
                                                      }
                                                  })
                                          .setNegativeButton(
                                                  "No",
                                                  new DialogInterface.OnClickListener() {
                                                      public void onClick(
                                                              DialogInterface dialog,
                                                              int id) {
                                                          dialog.cancel();
                                                      }
                                                  });
                                  AlertDialog alertDialog = alertDialogBuilder
                                          .create();
                                  alertDialog.show();
                              }
                          }
                      });
  
              alertDialog.setNegativeButton("Cancel",
                      new DialogInterface.OnClickListener() {
                          public void onClick(DialogInterface dialog,
                                  int which) {
                              dialog.cancel();
                          }
                      });
              alertDialog.show();
          }
      });
  
      findViewById(R.id.skirrmenu).setOnClickListener(new OnClickListener() {
          @Override
          public void onClick(View v) {
              if (!alreadyShowing) {
                  alreadyShowing = true;
                  openSlidingMenu();
              }
          }
      });
  
      imageViewNewClass.setOnClickListener(new View.OnClickListener() {
          public void onClick(View v) {
              Intent intent = new Intent(Classes.this, Class_Create.class);
              startActivity(intent);
          }
      });
  
      mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
          public void onItemClick(AdapterView&lt;?&gt; parent, View item,
                  int position, long id) {
              // Toast.makeText(getApplicationContext(),
              // "Listview item clicked", Toast.LENGTH_LONG).show();
  
              SparseBooleanArray sp = mListView.getCheckedItemPositions();
              StringBuffer str = new StringBuffer();
              for (int i = 0; i &lt; sp.size(); i++) {
                  if (sp.valueAt(i) == true) {
                      String s = ((TextView) mListView.getChildAt(i))
                              .getText().toString();
                      str = str.append(" " + s);
                  }
              }
              Toast.makeText(Classes.this,
                      "Selected items are " + str.toString(),
                      Toast.LENGTH_LONG).show();
  
              // Intent intent = new Intent(Classes.this, PlayAudio.class);
              // startActivity(intent);
          }
      });
  
      dataManipulator = new DataManipulatorClass(this);
  }
  
  @Override
  protected void onResume() {
      // TODO Auto-generated method stub
      super.onResume();
  
      names2 = dataManipulator.selectAll();
  
      stg1 = new String[names2.size()];
      int x = 0;
      String stg;
  
      for (String[] name : names2) {
          stg = "Class Name : " + name[1];
          stg1[x] = stg;
          x++;
      }
  
      adapter = new ArrayAdapter&lt;String&gt;(this,
              R.layout.custom_list_item_multiple_choice, stg1);
      mListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
      mListView.setBackgroundResource(R.drawable.assignmentheader);
      mListView.setCacheColorHint(Color.TRANSPARENT);
      mListView.setAdapter(adapter);
      adapter.notifyDataSetChanged();
  }
  
  @Override
  protected void onDestroy() {
      // TODO Auto-generated method stub
      super.onDestroy();
      if (dataManipulator != null)
          dataManipulator.close();
  }
  }
  </code></pre>