我从json请求收到的数据元素中设置了这个listvide。现在我不确定的是如何长时间点击这个列表视图中的列表项目。
这是填充列表视图的后台活动
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)
是的所以我不知道如何从列表视图中删除任何项目。
答案 0 :(得分: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
答案 1 :(得分:0)
现在我不确定的是如何从中删除一个listitem 这个列表视图长按一下。
您只需从数据集中删除该项目,然后在notifyDataSetChanged
适配器上调用ListViews
即可。
lv.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int pos, long id){
notifications.remove(pos);
adapter.notifyDataSetChanged();
}