0

我根据从 json 请求中收到的数据元素来设置这个列表视频。现在我不确定的是如何通过长按从这个列表视图中删除一个列表项。

这是填充列表视图的后台活动

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();
                }
            });

        }

    }

这是我做的 longclicklistener 的开始。

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

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

是的,所以我不知道如何从列表视图中删除任何项目。

4

2 回答 2

1

您从列表适配器中删除该项目并调用 notifyDataSetChanged()。

普通的 ListAdapter 没有 remove 方法,我不知道 SimpleAdapter 扩展了什么,但是 ArrayAdapter 有一个 remove 方法,您可以将自己的添加到 BaseAdapter。

顺便说一句,如果您想要一种更简单的方法来创建 ListAdapter,这可能会有所帮助(创建一个扩展 SilkAdapter 而不是 ArrayAdapter 或您正在使用的任何东西的类):https ://github.com/afollestad/Silk/blob/master/ src/com/afollestad/silk/adapters/SilkAdapter.java

于 2013-07-22T20:18:36.190 回答
0

现在我不确定的是如何通过长按从这个列表视图中删除一个列表项。

您只需从数据集中删除该项目并调用notifyDataSetChanged适配器ListViews

lv.setOnItemLongClickListener(new OnItemLongClickListener() {

   public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int pos, long id){
          notifications.remove(pos);
          adapter.notifyDataSetChanged();
}
于 2013-07-22T20:19:44.023 回答