我想在Custom Adapter中创建一个可以从列表中删除项目的函数,然后想要在Main_Activity中实现Custom Adapter类 并执行操作。
我的CustomAdapterClass
public class CustomAdapter extends BaseAdapter {
String[] result;
Context context;
int[] imageId;
TextView tv;
ImageView img;
private static LayoutInflater inflater = null;
public CustomAdapter(MainActivity mainActivity, String[] prgmNameList,
int[] prgmImages) {
// TODO Auto-generated constructor stub
result = prgmNameList;
context = mainActivity;
imageId = prgmImages;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return result.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public class Holder {
TextView tv;
ImageView img;
}
@SuppressLint({ "ViewHolder", "InflateParams" })
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Holder holder = new Holder();
View rowView;
rowView = inflater.inflate(R.layout.listitem, null);
holder.tv = (TextView) rowView.findViewById(R.id.textView1);
holder.img = (ImageView) rowView.findViewById(R.id.imageView1);
// holder.tv.setText(result[position]);
holder.img.setImageResource(imageId[position]);
return rowView;
}
public void remove(Object item) {
// TODO Auto-generated method stub
}
}
和MainActivityClass
public class MainActivity extends ListActivity {
CustomAdapter mAdapter;
ListView listView;
Context context;
TextView Tv;
// ArrayList<String> prgmName;
public static int[] prgmImages = { R.drawable.p1, R.drawable.p2,
R.drawable.p3, R.drawable.p4 };
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Tv = (TextView) findViewById(R.id.textview2);
mAdapter = new CustomAdapter(this, null, prgmImages);
listView = (ListView) findViewById(R.id.list);
String[] items = new String[2];
for (int i = 0; i < items.length; i++) {
items[i] = "Item " + (i + 1);
}
context = MainActivity.this;
listView.setAdapter(mAdapter);
// Create a ListView-specific touch listener. ListViews are given
// special treatment because
// by default they handle touches for their list items... i.e. they're
// in charge of drawing
// the pressed state (the list selector), handling list item clicks,
// etc.
SwipeDismissListViewTouchListener touchListener = new SwipeDismissListViewTouchListener(
listView,
new SwipeDismissListViewTouchListener.DismissCallbacks() {
@Override
public boolean canDismiss(int position) {
return true;
}
@Override
public void onDismiss(ListView listView,
int[] reverseSortedPositions) {
for (int position : reverseSortedPositions) {
mAdapter.remove(mAdapter.getItem(position));
}
mAdapter.notifyDataSetChanged();
}
});
listView.setOnTouchListener(touchListener);
// Setting this scroll listener is required to ensure that during
// ListView scrolling,
// we don't look for swipes.
listView.setOnScrollListener(touchListener.makeScrollListener());
// Set up normal ViewGroup example
final ViewGroup dismissableContainer = (ViewGroup) findViewById(R.id.dismissable_container);
for (int i = 0; i < items.length; i++) {
final Button dismissableButton = new Button(this);
dismissableButton.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
dismissableButton.setText("Button " + (i + 1));
dismissableButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this,
"Clicked " + ((Button) view).getText(),
Toast.LENGTH_SHORT).show();
}
});
// Create a generic swipe-to-dismiss touch listener.
dismissableButton.setOnTouchListener(new SwipeDismissTouchListener(
dismissableButton, null,
new SwipeDismissTouchListener.DismissCallbacks() {
@Override
public boolean canDismiss(Object token) {
return true;
}
@Override
public void onDismiss(View view, Object token) {
dismissableContainer.removeView(dismissableButton);
}
}));
dismissableContainer.addView(dismissableButton);
}
}
@Override
protected void onListItemClick(ListView listView, View view, int position,
long id) {
Toast.makeText(this,
"Clicked " + getListAdapter().getItem(position).toString(),
Toast.LENGTH_SHORT).show();
}
}
我在mAdapter.remove(mAdapter.getItem(position));
收到错误
我该如何解决呢?