Android:从列表视图中删除使用自定义适配器中的解析填充的项目
Android: Delete item from Listview that was populated using Parse in Custom Adapter
我正在尝试使用列表视图中的按钮删除 parse.com 中的项目。使用自定义适配器 class 填充列表视图,这就是我将执行 onClick 以删除它的地方。任何帮助都会很棒!
更新:基本上我想删除 moduleCode 等于位置的 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;
}
}
如果需要,这里是模块 class:
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;
}
}
设法解决了这个问题。我只是改变了 query.whereEqualTo("objectId", getItem(position));
至
query.whereEqualTo("moduleCode", getItem(position).getModuleCode());
不完全是我想要的,但它有效:)
我正在尝试使用列表视图中的按钮删除 parse.com 中的项目。使用自定义适配器 class 填充列表视图,这就是我将执行 onClick 以删除它的地方。任何帮助都会很棒!
更新:基本上我想删除 moduleCode 等于位置的 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;
}
}
如果需要,这里是模块 class:
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;
}
}
设法解决了这个问题。我只是改变了 query.whereEqualTo("objectId", getItem(position));
至
query.whereEqualTo("moduleCode", getItem(position).getModuleCode());
不完全是我想要的,但它有效:)