我编写了一个代码来删除列表视图中的行,但该行仍在列表中可见,我正在使用notifyDataSetChanged()
,这是我使用的:
public class MyAdapter extends ArrayAdapter<Transport> {
Context context;
ArrayList<Transport> arrayList;
LayoutInflater layoutInflater;
int resource;
ViewHolder viewHolder;
View view;
Helper helper;
List<NameValuePair> params;
JSONObject jsonObject;
String strName = null, strAlwaysZero = null, strPosition = null;
Integer taggedPosition;
public MyAdapter(Context context, int resource, ArrayList<Transport> arrayList) {
super(context, resource, arrayList);
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.context = context;
this.resource = resource;
this.arrayList = arrayList;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// convert view = design
view = convertView;
if (view == null) {
viewHolder = new ViewHolder();
view = layoutInflater.inflate(resource, null);
viewHolder.textName = (TextView) view.findViewById(R.id.tvName);
viewHolder.btnDelete = (ImageButton) view.findViewById(R.id.btnDelete);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
viewHolder.textName.setText(arrayList.get(position).getName());
viewHolder.btnDelete.setTag(position);
viewHolder.btnDelete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(final View v) {
taggedPosition = (Integer) v.getTag();
Log.d("Position::-", Integer.toString(taggedPosition));
new DeleteRecord().execute();
}
});
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
});
return view;
}
class DeleteRecord extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... args) {
strUrlUpdate = "http://somerurl.tld/result.php";
helper = new Helper();
params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("sName", strName));
params.add(new BasicNameValuePair("sKey", strAlwaysZero));
strServerResult = helper.getHttpPost(strUrlUpdate ,params);
strStatusId = "0";
strMessage = "Unknow Status!";
try {
jsonObject = new JSONObject(strServerResult);
strStatusId = jsonObject.getString("StatusID");
if (!strStatusId.equals("0")) {
Log.d("background-Position::-", Integer.toString(taggedPosition));
arrayList.remove(taggedPosition);
}
strMessage = jsonObject.getString("Message");
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// Prepare Save Data
if (strStatusId.equals("0")) {
Toast.makeText(context, "Unable to remove record", Toast.LENGTH_SHORT).show();
} else {
notifyDataSetChanged();
Toast.makeText(context, "Record removed successfully", Toast.LENGTH_SHORT).show();
}
}
}
答案 0 :(得分:0)
将arrayList.remove(taggedPosition)
放入doinBackground()
方法。
class DeleteRecord extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... args) {
strUrlUpdate = "http://somerurl.tld/result.php";
helper = new Helper();
params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("sName", strName));
params.add(new BasicNameValuePair("sKey", strAlwaysZero));
strServerResult = helper.getHttpPost(strUrlUpdate ,params);
strStatusId = "0";
strMessage = "Unknow Status!";
try {
jsonObject = new JSONObject(strServerResult);
strStatusId = jsonObject.getString("StatusID");
if (!strStatusId.equals("0")) {
arrayList.remove(taggedPosition);
}
strMessage = jsonObject.getString("Message");
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// Prepare Save Data
if (strStatusId.equals("0")) {
Toast.makeText(context, "Unable to remove record", Toast.LENGTH_SHORT).show();
} else {
notifyDataSetChanged();
Toast.makeText(context, "Record removed successfully", Toast.LENGTH_SHORT).show();
}
}
}
答案 1 :(得分:-1)
使用适配器名称,例如adaptername.notifyDataSetChanged();