我根据从 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)
是的,所以我不知道如何从列表视图中删除任何项目。