テストプロジェクトで使用しようとしていますListViewAnimations library
が、ドラッグアンドドロップ機能をこのリストに実装したいアクティビティがあります
私のListViewTest :
public class ListViewTest extends ActionBarActivity {
DynamicListView list;
DataBaseAdapter dataBaseHelper;
private int mNewItemCount;
public List<Cards> list1;
Cards cards;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view_test);
dataBaseHelper = new DataBaseAdapter(this);
MyAdapter adapter1 = new MyAdapter(this, dataBaseHelper.getData());
list = (DynamicListView) findViewById(R.id.test_list_view);
list.enableDragAndDrop();
list.setDraggableManager(new TouchViewDraggableManager(R.id.linear_layout));
list.setOnItemMovedListener(new MyOnItemMovedListener(adapter1, list1));
list.setOnItemLongClickListener(new MyOnItemLongClickListener(list));
list.setAdapter(adapter1);
}
private static class MyOnItemLongClickListener implements AdapterView.OnItemLongClickListener {
private final DynamicListView mListView;
MyOnItemLongClickListener(final DynamicListView listView) {
mListView = listView;
}
@Override
public boolean onItemLongClick(final AdapterView<?> parent, final View view, final int position, final long id) {
return true;
}
}
private class MyOnDismissCallback implements OnDismissCallback {
private final MyAdapter myAdapter;
@Nullable
private Toast mToast;
MyOnDismissCallback(final MyAdapter adapt) {
myAdapter = adapt;
}
@Override
public void onDismiss(@NonNull final ViewGroup listView, @NonNull final int[] reverseSortedPositions) {
for (int position : reverseSortedPositions) {
myAdapter.remove(position);
}
if (mToast != null) {
mToast.cancel();
}
}
}
private class MyOnItemMovedListener implements OnItemMovedListener {
private final MyAdapter adapter1;
List<Cards> cardsList;
private Toast mToast;
MyOnItemMovedListener(final MyAdapter adapt, List<Cards> list1) {
adapter1 = adapt;
cardsList = list1;
}
@Override
public void onItemMoved(final int originalPosition, final int newPosition) {
}
}
private class MyOnItemClickListener implements AdapterView.OnItemClickListener {
private final DynamicListView mListView;
MyOnItemClickListener(final DynamicListView listView) {
mListView = listView;
}
@Override
public void onItemClick(final AdapterView<?> parent, final View view, final int position, final long id) {
mListView.insert(position, mNewItemCount);
mNewItemCount++;
}
}
}
MyAdapter クラス:
public class MyAdapter extends ArrayAdapter<Cards> {
private Activity mActivity;
private List<Cards> data;
public static LayoutInflater inflater = null;
public Resources resources;
Cards cards = null;
int i = 0;
private final Context mContext;
public MyAdapter(final Context context, List d) {
mContext = context;
this.data = d;
}
public static class ViewHolder {
public TextView text;
public ImageView image;
}
@Override
public View getView(final int position,final View convertView,final ViewGroup parent) {
View vi = convertView;
ViewHolder holder;
if(convertView == null){
vi =(View) LayoutInflater.from(mContext).inflate(R.layout.custom_row, parent, false);
holder = new ViewHolder();
holder.text = (TextView)vi.findViewById(R.id.listText);
holder.image = (ImageView) vi.findViewById(R.id.image_country);
vi.setTag(holder);
}else
holder = (ViewHolder) vi.getTag();
if(data.size()<=0){
}
else {
cards = data.get(position);
holder.text.setText(cards.getName());
}
return vi;
}
@Override
public long getItemId(int position) {
return position;
}
@NonNull
@Override
public List<Cards> getItems() {
return super.getItems();
}
@Override
public int getCount() {
if (data.size() <= 0)
return 1;
return data.size();
}
@Override
public boolean hasStableIds() {
return true;
}
}
カードは私のゲッターおよびセッタークラスであり、リストはprom SQlite dbに取り込まれ、プログラムがすべて正常に起動すると、アイテムをドラッグアンドドロップしようとすると、次のようになりますjava.lang.IndexOutOfBoundsException: Invalid index (here is current item index), size is 0
。どこかでマイリストを発表しOnItemMovedLIstener
なかったと思いますが、それを行う方法とリストを書き換える方法がわかりませんでした
回答ありがとうございます