Java 如何从如下设置的listview中删除listitem

Java 如何从如下设置的listview中删除listitem,java,android,android-listview,Java,Android,Android Listview,我从一个json请求中收到的数据元素中设置了这个listvide。现在我不确定的是,如何在长时间单击后从这个listview中删除listitem 这是填充listview的后台活动 class readNotifications extends AsyncTask<String, String, String>{ ; @Override protected String doInBackground(String... params)

我从一个json请求中收到的数据元素中设置了这个listvide。现在我不确定的是,如何在长时间单击后从这个listview中删除listitem

这是填充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();
                }
            });

        }

    }
类readNotifications扩展异步任务{
;
@凌驾
受保护的字符串doInBackground(字符串…参数){
//TODO自动生成NameValuePairb
Listparam=newarraylist();
param.add(新的BasicNameValuePair(“id”,saveSharedReference.getUserId(Notifications.this));//saveSharedReference.getUserId(Notifications.this)
JSONObject json=jsonParser.makeHttpRequest(url_通知,“POST”,参数);
Log.d(“我的通知”,json.toString());
试一试{
JSONArray数组=json.getJSONArray(“通知”);
Log.d(“通知长度”,Integer.toString(array.length());
//if(array.length()==0)
//Log.d(“消息?”,“无消息”);
if(array.length()>0){
对于(int i=0;ivalue
地图放置(“通知id”,id);
map.put(“message”,message);
//将哈希列表添加到ArrayList
通知。添加(地图);
}
}否则{
runOnUiThread(新的Runnable(){
公开募捐{
TextView tx=(TextView)findViewById(R.id.nonotifications);
tx.setVisibility(0);
}
});
//Log.d(“通知长度”,Integer.toString(array.length());
}
}捕获(例外e){
e、 printStackTrace();
}
返回null;
}
受保护的void onPostExecute(字符串文件\u url){
//获取所有产品后关闭对话框
//从后台线程更新UI
runOnUiThread(新的Runnable(){
公开募捐{
ListAdapter=新的SimpleAdapter(
通知,这个通知,,
R.layout.notifications,新字符串[]{“notification_id”,“message”},
新的int[]{R.id.pid,R.id.notification});
//更新列表视图
setListAdapter(适配器);
}
});
}
}
类deleteNotification扩展了异步任务{
字符串theirId=((TextView)findViewById(R.id.pid)).getText().toString();
@凌驾
受保护的字符串doInBackground(字符串…参数){
//TODO自动生成NameValuePairb
Listparam=newarraylist();
param.add(新的BasicNameValuePair(“id”,saveSharedReference.getUserId(Notifications.this));//saveSharedReference.getUserId(Notifications.this)
参数add(新的BasicNameValuePair(“nid”,notiId));
JSONObject json=jsonParser.makeHttpRequest(url_delete_通知,“POST”,参数);
Log.d(“他们的Id”,theirId);
Log.d(“我的通知”,json.toString());
返回null;
}
受保护的void onPostExecute(字符串文件\u url){
//获取所有产品后关闭对话框
//从后台线程更新UI
runOnUiThread(新的Runnable(){
公开募捐{
//新建readNotifications().execute();
}
});
}
}
这是我做的longclicklistener的开始

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

            public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                    int pos, long id)
ListView lv=getListView();
registerForContextMenu(lv);
lv.setOnItemLongClickListener(新的OnItemLongClickListener(){
长单击(AdapterView arg0、视图arg1、,
内部位置,长id)

是的,所以我不知道如何从listview中删除任何项。

从列表适配器中删除该项,然后调用notifyDataSetChanged()

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

顺便说一句,如果您想要一种更简单的方法来创建ListAdapter,这可能会有所帮助(创建一个扩展SilkAdapter的类,而不是ArrayAdapter或任何您正在使用的类):

现在我不确定的是我如何从中删除listitem 长时间单击此列表视图

您只需从数据集中删除该项,然后在
ListViews
适配器上调用
notifyDataSetChanged

lv.setOnItemLongClickListener(new OnItemLongClickListener() {

   public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int pos, long id){
          notifications.remove(pos);
          adapter.notifyDataSetChanged();
}
lv.setOnItemLongClickListener(新的OnItemLongClickListener(){
长单击公共布尔值(AdapterView arg0、视图arg1、int pos、长id){
通知。删除(pos);
adapter.notifyDataSetChanged();
}

从“通知”中删除不会从适配器中删除。@afollestad这就是您调用notifyDataSetChanged的原因。@Ahman“通知”未连接到适配器,它是一个ArrayList,因此适配器中的任何内容都不会被更改。@afollestad
notification
是包含HashMap元素的ArrayList:)@afollestad我们不能说
通知
没有连接到适配器