0

I am making an android application that needs to use a ListView. Once a user presses a menubutton, it pops up a popupwindow containing a TextView, EditText and two Buttons, "Ok" and "Cancel". Once the user presses "Ok", the text inside the EditText should be added to the ListView. And the cancel Button is obvious. I also want to be able to long press on a ListView item to open a popupwindow containing a delete Button. How can I make this possible? I am using this code so far:

public class NotesActivity extends ListActivity {
/** Called when the activity is first created. */
Button AddItemToListView;
  static final String[] COUNTRIES = new String[] {
      "Matte på A1 med Ole", "Engelsk på klasserommet", "Film på A1 etter friminuttet"
      };
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.notes);
    setListAdapter((ListAdapter) new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES));

    ListView lv = getListView();
    lv.setTextFilterEnabled(true);

    lv.setOnItemClickListener(new OnItemClickListener() {
      public void onItemClick(AdapterView<?> parent, View view,
          int position, long id) {
        // When clicked, show a toast with the TextView text
        Toast.makeText(getApplicationContext(), "Note: " + ((TextView) view).getText(),
            Toast.LENGTH_SHORT).show();
  }
    });
  }
@Override
public boolean onCreateOptionsMenu(Menu meny) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.listviewmenubuttons, meny);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()) {
    case R.id.AddItemToListView:
        Toast.makeText(NotesActivity.this,
                "Add note button pressed", Toast.LENGTH_SHORT)
                .show();
        break;
        }
    return true;
}
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user1183066
  • 115
  • 2
  • 6
  • 20

2 Answers2

0

Try doing some research on dialogs if you want to add feature after accessing listview.setonlongclick(). Here is a link on android dialog developers.

wesdfgfgd
  • 683
  • 1
  • 13
  • 26
  • I am certain this is what you are looking for. For more examples I am pretty sure google will help you out. – wesdfgfgd Feb 10 '12 at 16:24
0

Since you are using an ArrayAdapter, when the user taps on add, you must add the new item to your array (change from an array to an List to make it easier). Then you should call notifyDataSetChanged() from the ArrayAdapter.

For deleting is the same, but you remove the item from the List. The call to notifyDataSetChanged() is to tell the ListView that it needs to redraw itself.

Marcio Covre
  • 4,556
  • 2
  • 22
  • 24