Android:从Listview中删除使用自定义适配器中的Parse填充的项目

时间:2015-04-09 19:36:37

标签: android android-listview parse-platform delete-row parseexception

我正在尝试使用listview中的按钮删除parse.com中的项目。列表视图使用自定义适配器类进行填充,这是我将执行onClick以删除它的位置。任何帮助都会很棒!

更新:基本上我想删除moduleCode等于位置模块的模块

这是我尝试使用的适配器的一部分,但是我从ParseException e返回的错误是:04-09 19:33:05.408 10425-10425/com.yupo.dominic.yupo E/Error﹕ java.lang.IllegalArgumentException: invalid type for ParseObject: class com.yupo.dominic.yupo.Module

Button moduleDeleteButton = (Button)view.findViewById(R.id.moduleDeleteButton);

    moduleDeleteButton.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {

            ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Module");
            query.whereEqualTo("objectId", getItem(position));
            query.findInBackground(new FindCallback<ParseObject>() {
                public void done(List<ParseObject> module, ParseException e) {
                    if (e == null) {
                        // iterate over all messages and delete them
                        for(ParseObject delete : module)
                        {
                            delete.deleteInBackground();
                        }
                    } else {
                        Log.e("Error", e.getMessage());
                    }
                }
            });
            Toast.makeText(v.getContext(), "Deleted", Toast.LENGTH_LONG).show();
        }
    });

这是完整的适配器:

package com.yupo.dominic.yupo;

import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;

public class CustomListViewAdapter extends BaseAdapter {

    // Declare Variables
    Context mContext;
    LayoutInflater inflater;
    private List<Module> moduleList = null;
    private ArrayList<Module> arraylist;

    public CustomListViewAdapter(Context context,List<Module> moduleList) {
        mContext = context;
        this.moduleList = moduleList;
        inflater = LayoutInflater.from(mContext);
        this.arraylist = new ArrayList<Module>();
        this.arraylist.addAll(moduleList);
    }

    public class ViewHolder {
        TextView moduleCode;
        TextView moduleName;
        TextView moduleUni;
    }

    @Override
    public int getCount() {
        return moduleList.size();
    }

    @Override
    public Module getItem(int position) {
        return moduleList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public View getView(final int position, View view, ViewGroup parent) {
        final ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            view = inflater.inflate(R.layout.module_list_item, null);
            // Locate the TextViews in listview_item.xml
            holder.moduleCode = (TextView) view.findViewById(R.id.moduleCodeTextView);
            holder.moduleName = (TextView) view.findViewById(R.id.moduleNameTextView);
            holder.moduleUni = (TextView) view.findViewById(R.id.moduleUniTextView);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        // Set the results into TextViews
        holder.moduleCode.setText(moduleList.get(position).getModuleCode());
        holder.moduleName.setText(moduleList.get(position).getModuleName());
        holder.moduleUni.setText(moduleList.get(position).getModuleUni());

        // Listen for ListView Item Click
        Button moduleDeleteButton = (Button)view.findViewById(R.id.moduleDeleteButton);

        moduleDeleteButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {

                ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Module");
                query.whereEqualTo("objectId", getItem(position));
                query.findInBackground(new FindCallback<ParseObject>() {
                    public void done(List<ParseObject> module, ParseException e) {
                        if (e == null) {
                            // iterate over all messages and delete them
                            for(ParseObject delete : module)
                            {
                                delete.deleteInBackground();
                            }
                        } else {
                            Log.e("Error", e.getMessage());
                        }
                    }
                });
                Toast.makeText(v.getContext(), "Deleted", Toast.LENGTH_LONG).show();
            }
        });

        return view;
    }
}

如果需要,这是Module类:

package com.yupo.dominic.yupo;

public class Module {
    private String moduleName;
    private String moduleCode;
    private String moduleUni;

    public String getModuleName() {
        return moduleName;
    }

    public void setModuleName(String moduleName) {
        this.moduleName = moduleName;
    }

    public String getModuleCode() {
        return moduleCode;
    }

    public void setModuleCode(String moduleCode) {
        this.moduleCode = moduleCode;
    }

    public String getModuleUni() {
        return moduleUni;
    }

    public void setModuleUni(String moduleUni) {
        this.moduleUni = moduleUni;
    }
}

1 个答案:

答案 0 :(得分:0)

管理好解决这个问题。我只是改变了query.whereEqualTo("objectId", getItem(position));

query.whereEqualTo("moduleCode", getItem(position).getModuleCode());

不完全是我想要的但它有效:)