我使用BaseAdapter在listview中创建了一个广告列表,我想从列表中删除一个广告。要删除单个广告,我必须使用异步任务请求web api,所以我在onPostExecute方法中添加了notifyDataSetChanged()。广告已成功删除,但notifydatasetchnaged无法正常工作。我自定义BaseAdapter类如下所示,请帮助解决此问题。
public class AdsListAdapter extends BaseAdapter{
private Activity activity;
ArrayList<String> title;
ArrayList<Long> adsIds;
SessionManager sessions;
AlertDialogManager alert = new AlertDialogManager();
private static LayoutInflater inflater=null;
String MYTAG="AdLIst";
long userID=0;
public AdsListAdapter(Activity a,ArrayList<String> titlelist,ArrayList<Long> adsId,long userid) {
activity = a;
title=titlelist;
adsIds=adsId;
userID=userid;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
sessions=new SessionManager(activity.getApplicationContext());
imageLoader = new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
return title.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public static class ViewHolder{
public TextView titles,editicon,delicon;
}
@SuppressLint("NewApi") public View getView(int position, View convertView, ViewGroup parent) {
final int id=position;
View vi=convertView;
ViewHolder holder;
if(convertView==null){
vi = inflater.inflate(R.layout.adscustomlist, null);
holder = new ViewHolder();
holder.titles = (TextView) vi.findViewById(R.id.titles);
holder.editicon= (TextView) vi.findViewById(R.id.editicon);
holder.delicon= (TextView) vi.findViewById(R.id.delicon);
vi.setTag( holder );
}
else
{ holder=(ViewHolder)vi.getTag(); }
holder.titles.setText(title.get(position));
holder.delicon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new DeleteAd(userID,adsIds.get(id)).execute();
}
});
return vi;
}
class DeleteAd extends AsyncTask<String,Void,Void>
{
AlertDialogManager alert = new AlertDialogManager();
private String Error = null,Message=null;
long UserID =0,AdId=0;
boolean Status=false;
private ProgressDialog Dialog = new ProgressDialog(activity);
protected void onPreExecute() {
Dialog.setMessage("Loading ...");
Dialog.setCanceledOnTouchOutside(false);
Dialog.show();
}
public DeleteAd(long UserID,long AdId) {
this.UserID=UserID;
this.AdId=AdId;
}
@Override
protected Void doInBackground(String... arg0) {
JSONObject json;
try {
json = new JSONObject(postDataAdDelete());
Message=json.getString("Message");
Status = json.getBoolean("Status");
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void unused) {
Dialog.dismiss();
alert.showAlertDialog(activity, "Ad Delete", Message, Status);
notifyDataSetChanged();
}
}
}
答案 0 :(得分:0)
您必须删除列表标题中要删除的位置的值。尝试title.remove(位置)
答案 1 :(得分:0)
AdsListAdapter adapter = new AdsListAdapter (Activity ,ArrayList<String> ,ArrayList<Long> ,long );//pass the value
adapter.notifyDataSetChanged();
答案 2 :(得分:0)
尝试像这样调用notifyDataSetChanged()
方法
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
notifyDataSetChanged();
}
});