我有一个包含多个项目的列表字段。我想从列表中选择一项并拖放到列表中的另一项。将项目放在另一个项目上后,项目的位置应互换。(示例:- 如果我从列表中选择第一项并在拖放后将该项目拖放到列表中的第四项,则应更改这两项的项目位置应更改为第 4 位的第 1 项和第 1 位的第 4 项)
我不知道该怎么做。请帮我完成这个任务。
public class ListScreen extends MainScreen implements ListFieldCallback {
// private ObjectChoiceField choices;
private int x, y;
int start = 0 , end=0;
private int startPos;
// private int pos;
private ObjectListField list;
private Object[] listData = new Object[] { "Ram", "Sunny", "Ramit",
"Vishal","Ravinder","Manoj","Ravi","Vikas","Santosh","Ravi","Deepak","Sharma","Nvn" };
public ListScreen() {
init();
}
private void init() {
list = new ObjectListField(FOCUSABLE);
list.setRowHeight(45);
list.setCallback(this);
list.set(listData);
add(list);
}
public void drawListRow(ListField listField, Graphics graphics, int index,
int y, int width) {
graphics.drawText(listData[index].toString(), 25, y + 10);
}
public Object get(ListField listField, int index) {
return listData[index];
}
public int getPreferredWidth(ListField listField) {
return getPreferredWidth();
}
public int indexOfList(ListField listField, String prefix, int start) {
// TODO Auto-generated method stub
return 0;
}
// protected boolean navigationMovement(int dx, int dy, int status, int
// time) {
//
// return super.navigationMovement(dx, dy, status, time);
// }
protected boolean touchEvent(TouchEvent message) {
int eventCode = message.getEvent();
// Get the screen coordinates of the touch event
int touchX = message.getX(1);
int touchY = message.getY(1);
if (eventCode == TouchEvent.CLICK) {
calculateStart(touchY);
// Dialog.alert("You clicked at (" + touchX + "," + touchY + ")");
}
if (eventCode == TouchEvent.MOVE) {
x = touchX;
y = touchY;
invalidate();
}
if (eventCode == TouchEvent.UNCLICK) {
int unClickx = message.getX(1);
int unClicky = message.getY(1);
System.out.println("unclick at:" + unClickx + "::::" + unClicky);
calculatePosition(unClicky);
}
return true;
}
private void calculatePosition(int dy) {
if (dy != 0) {
end = dy / 45;
System.out.println(start + ":::" + end);
Object temp = listData[start];
listData[start] = listData[end];
listData[end] = temp;
invalidate();
}
}
private void calculateStart(int dy){
if(dy!=0)
start = dy/45;
}
}