Michael Nana
Michael Nana

Reputation: 1987

How to delete a listitem from a listview set up like this

I set up this listvide like this from the data elements I received from a json request. Now what I'm not to sure about is how would I delete an listitem from this listview on a long click.

This is the background activity that populates the listview

class readNotifications extends AsyncTask<String, String, String>{
        ;
        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generatNameValuePairb
            List<NameValuePair>param=new ArrayList<NameValuePair>();
            param.add(new BasicNameValuePair("id",SaveSharedPreference.getUserId(Notifications.this))); //SaveSharedPreference.getUserId(Notifications.this)

            JSONObject json=jsonParser.makeHttpRequest(url_notifications, "POST", param);
            Log.d("My Notifications", json.toString());

            try {

                JSONArray array = json.getJSONArray("notifications");
                Log.d("Notifications length",Integer.toString(array.length()));
                //if(array.length()==0)
                    //Log.d("Messages?","No Messages");
                if(array.length()>0){
                for (int i = 0; i < array.length(); i++) {
                   // Log.i("name", array.getJSONObject(i).getString("name"));
                    JSONObject c = array.getJSONObject(i);

                    // Storing each json item in variable
                    String id = c.getString("notification_id");
                    String message = c.getString("message");

                    //Log.i("picture_url", picture);
                    // creating new HashMap
                    HashMap<String, String> map = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    map.put("notification_id", id);
                    map.put("message", message);


                    // adding HashList to ArrayList
                    notifications.add(map);
                }
                }else{
                     runOnUiThread(new Runnable() {
                            public void run() {
                    TextView tx=(TextView)findViewById(R.id.nonotifications);
                    tx.setVisibility(0);
                            }
                     });
                    //Log.d("Notifications length",Integer.toString(array.length()));
                }
            }catch (Exception e) {
                e.printStackTrace();
            }
                return null;
            }
        protected void onPostExecute(String file_url) {
            // dismiss the dialog after getting all products

            // updating UI from Background Thread
            runOnUiThread(new Runnable() {
                public void run() {

                    ListAdapter adapter = new SimpleAdapter(
                            Notifications.this, notifications,
                            R.layout.notifications, new String[] { "notification_id","message"},
                            new int[] { R.id.pid, R.id.notification});
                    // updating listview
                    setListAdapter(adapter);
                }
            });

        }

    }

    class deleteNotification extends AsyncTask<String, String, String>{
        String theirId=((TextView) findViewById(R.id.pid)).getText().toString();
        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generatNameValuePairb
            List<NameValuePair>param=new ArrayList<NameValuePair>();
            param.add(new BasicNameValuePair("id",SaveSharedPreference.getUserId(Notifications.this))); //SaveSharedPreference.getUserId(Notifications.this)
            param.add(new BasicNameValuePair("nid",notiId));
            JSONObject json=jsonParser.makeHttpRequest(url_delete_notification, "POST", param);
            Log.d("Their Id", theirId );
            Log.d("My Notifications", json.toString());


                return null;
            }
        protected void onPostExecute(String file_url) {
            // dismiss the dialog after getting all products

            // updating UI from Background Thread
            runOnUiThread(new Runnable() {
                public void run() {
            //new readNotifications().execute();
                }
            });

        }

    }

This is the start of the longclicklistener I did.

ListView lv= getListView();
        registerForContextMenu(lv);
        lv.setOnItemLongClickListener(new OnItemLongClickListener() {

            public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                    int pos, long id)

yeah so I don't know how I would delete any item from the listview.

Upvotes: 0

Views: 95

Answers (2)

Ahmad
Ahmad

Reputation: 72563

Now what I'm not to sure about is how would I delete an listitem from this listview on a long click.

You just have to remove the item from your dataset and call notifyDataSetChanged on the ListViews adapter.

lv.setOnItemLongClickListener(new OnItemLongClickListener() {

   public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int pos, long id){
          notifications.remove(pos);
          adapter.notifyDataSetChanged();
}

Upvotes: 0

afollestad
afollestad

Reputation: 2934

You remove the item from the list adapter and call notifyDataSetChanged().

The plain ListAdapter doesn't have a remove method, and I don't know what SimpleAdapter extends, but ArrayAdapter has a remove method and you could add your own to a BaseAdapter.

Btw, if you want a simpler way to create a ListAdapter, this may help (make a class that extends SilkAdapter instead of ArrayAdapter or whatever you're using): https://github.com/afollestad/Silk/blob/master/src/com/afollestad/silk/adapters/SilkAdapter.java

Upvotes: 1

Related Questions