ListView自定义适配器,使用AsyncTask

时间:2016-01-29 02:40:31

标签: java android listview android-asynctask

我正在使用自定义适配器来显示我的列表视图。首先,我使用AsyncTask检索我想要在listview中显示的信息,因为从PHP Web服务检索信息。然后,要从列表视图中删除项目,它再次调用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;
    }

以上代码显示上下文菜单,当用户选择删除时,警告对话框将提示他们确认操作。当他们选择ok时,它执行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上将结果设置为true = = 1,但onContextItemSelected不等待onPostExecute并且只取值deleteSuccess为“false。”。

最重要的是,如何从我的适配器中删除所选项目,从而成功&#34;刷新&#34;我删除项目后立即查看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();
            }
        }
    };
}

2 个答案:

答案 0 :(得分:1)

在致电mAdapter.notifyDataSetChanged();之前,您应该从mAdapter中删除该元素。

答案 1 :(得分:0)

您可以使用界面与AsyncTask进行通信