[英]Can't get custom list adapter to populate listview using asynctask
[英]ListView custom adapter, remove item using AsyncTask
我正在使用自定义适配器来显示我的列表视图。 首先,当从PHP Web服务检索信息时,我将使用AsyncTask检索要显示在列表视图中的信息。 然后,要从列表视图中删除项目,请再次调用Web服务。 成功删除所选项目后, onPostExecute
方法将接收结果并删除该项目。 但是,只有刷新片段后,它才会从我的列表视图中删除。
@Override
public boolean onContextItemSelected(final MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case VIEW_CONTACT:
// other actions
return true;
case EDIT_CONTACT:
return true;
case DELETE_CONTACT:
// Create AlertDialog for confirmation
AlertDialog.Builder builder = new AlertDialog.Builder(context)
.setTitle("Delete Contact List")
.setMessage("Are you sure you want to delete this list?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// continue with delete
new DeleteTask().execute();
dialog.cancel();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
})
.setIcon(android.R.drawable.ic_dialog_alert);
builder.show();
return true;
}
return true;
}
上面的代码显示了上下文菜单,当用户选择删除时,将出现一个警告对话框,提示他们确认操作。 当他们选择确定时,它将执行AsyncTask,如下所示:
public class DeleteTask extends AsyncTask<Void, String ,String>{
@Override
protected void onPreExecute(){
super.onPreExecute();
pDialog = ProgressDialog.show(context, null, "Loading", true);
}
@Override
protected String doInBackground(Void... params){
return DeleteData();
}
@Override
protected void onPostExecute(String result){
super.onPostExecute(result);
pDialog.dismiss();
if (result.equals("1")) {
mAdapter.notifyDataSetChanged(); // this bit didn't work in updating/refreshing the listview
Toast.makeText(context, "List successfully deleted", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context,
"List not deleted", Toast.LENGTH_LONG).show();
}
}
private String DeleteData(){
int success = 0;
try {
List<NameValuePair> paramas = new ArrayList<NameValuePair>();
paramas.add(new BasicNameValuePair("listid", listID));
JSONParser jParserDelete = new JSONParser();
JSONObject jsonObjectA = jParserDelete.makeHttpRequest(Constant.URL
+ "removeList.php", "GET", paramas);
success = jsonObjectA.getInt(Constant.TAG_SUCCESS);
Log.d("contacts", jsonObjectA.toString());
} catch (Exception ex) {
ex.printStackTrace();
}
return Integer.toString(success);
}
}
我尝试使用mAdapter.notifyDataSetChanged();
但这没用。 我知道mAdapter是Activity类中的全局变量。 另外,请注意,我的适配器是自定义类。 我最初想在全局范围内声明一个布尔型deleteSuccess
,然后在onPostExecute
其在result == 1上设置为true,但是onContextItemSelected
不会等待onContextItemSelected
,而是将onContextItemSelected
的值设为`false。
最重要的是,如何删除适配器中的选定项目,从而在删除项目后立即成功“刷新” ListView?
编辑:
这是我的自定义适配器类
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.SectionIndexer;
import android.widget.TextView;
import java.util.ArrayList;
import se.emilsjolander.stickylistheaders.StickyListHeadersAdapter;
public class ContactsAdapter extends BaseAdapter implements StickyListHeadersAdapter, SectionIndexer, Filterable {
private final Context mContext;
private int[] mSectionIndices;
private Character[] mSectionLetters;
private LayoutInflater mInflater;
private ArrayList<String> mArrayList;
// for search function
private ArrayList<Glossary> glossariesList;
private ArrayList<Glossary> glossariesListForSearch;
public ContactsAdapter(Context context, ArrayList<String> arrayList, ArrayList<Glossary> glossaries) {
super();
mContext = context;
mArrayList = arrayList;
mInflater = LayoutInflater.from(context);
mSectionIndices = getSectionIndices();
mSectionLetters = getSectionLetters();
this.glossariesList = glossaries;
glossariesListForSearch = glossaries;
}
private int[] getSectionIndices() {
ArrayList<Integer> sectionIndices = new ArrayList<>();
char lastFirstChar = mArrayList.get(0).charAt(0);
sectionIndices.add(0);
for (int i = 1; i < mArrayList.size(); i++) {
if (mArrayList.get(i).charAt(0) != lastFirstChar) {
lastFirstChar = mArrayList.get(i).charAt(0);
sectionIndices.add(i);
}
}
int[] sections = new int[sectionIndices.size()];
for (int i = 0; i < sectionIndices.size(); i++) {
sections[i] = sectionIndices.get(i);
}
return sections;
}
private Character[] getSectionLetters() {
Character[] letters = new Character[mSectionIndices.length];
for (int i = 0; i < mSectionIndices.length; i++) {
letters[i] = mArrayList.get(mSectionIndices[i]).charAt(0);
}
return letters;
}
@Override
public int getCount() {
return glossariesList.size();
}
@Override
public Object getItem(int position) {
return glossariesList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.index_list_item_layout, parent, false);
holder.text = (TextView) convertView.findViewById(R.id.text);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Glossary glossary = glossariesList.get(position);
holder.text.setText(glossary.getName());
return convertView;
}
@Override
public View getHeaderView(int position, View convertView, ViewGroup parent) {
HeaderViewHolder holder;
if (convertView == null) {
holder = new HeaderViewHolder();
convertView = mInflater.inflate(R.layout.index_header, parent, false);
holder.text = (TextView) convertView.findViewById(R.id.text1);
convertView.setTag(holder);
} else {
holder = (HeaderViewHolder) convertView.getTag();
}
// set header text as first char in name
CharSequence headerChar = glossariesList.get(position).getName().subSequence(0,1);
holder.text.setText(headerChar);
return convertView;
}
@Override
public long getHeaderId(int position) {
// return the first character of the country as ID because this is what
// headers are based upon
return glossariesList.get(position).getName().subSequence(0, 1).charAt(0);
}
@Override
public int getPositionForSection(int section) {
if (mSectionIndices.length == 0) {
return 0;
}
if (section >= mSectionIndices.length) {
section = mSectionIndices.length - 1;
} else if (section < 0) {
section = 0;
}
return mSectionIndices[section];
}
@Override
public int getSectionForPosition(int position) {
for (int i = 0; i < mSectionIndices.length; i++) {
if (position < mSectionIndices[i]) {
return i - 1;
}
}
return mSectionIndices.length - 1;
}
@Override
public Object[] getSections() {
return mSectionLetters;
}
class HeaderViewHolder {
TextView text;
}
class ViewHolder {
TextView text;
}
@Override
public Filter getFilter() {
return myFilter;
}
Filter myFilter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
ArrayList<Glossary> tempGlossaryList = new ArrayList<>();
if (constraint != null && glossariesListForSearch != null) {
int length = glossariesListForSearch.size();
int i = 0;
while (i < length) {
Glossary item = glossariesListForSearch.get(i);
if (item.getName().toLowerCase().contains(constraint.toString().toLowerCase())) {
tempGlossaryList.add(item);
}
i++;
}
filterResults.values = tempGlossaryList;
filterResults.count = tempGlossaryList.size();
}
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
glossariesList = (ArrayList<Glossary>) results.values;
if (results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
};
}
在调用mAdapter.notifyDataSetChanged();
您应该从mAdapter中删除该元素。
您可以使用接口与AsyncTask进行通信
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.