我有一个ListView
项目的点数(纬度和经度),我想默认选择最接近的3个,并让用户用列表中的任何其他点切换这3个点中的任何一个点(主要的)用于空间插值的3点的目的。
这是可能的,或者我怎样才能做出类似的东西?
示例:
用户处于适当位置(lat,lon)。
ListView
包含项目(x1,y1),(x2,y2),(x3,y3),(x4,y4),......
应用程序默认选择3个最接近的用户位置项,例如3个第一项。现在用户必须能够用(x4,y4)切换例如(x1,y1)。
感谢。
答案 0 :(得分:1)
检查我的解决方案。我试图创造你需要的答案。这很简单。
以下是代码:
public class Adapter extends BaseAdapter {
private final LayoutInflater layoutInflater;
private final Context context;
public final ArrayList<PointItem> pointItems;
private int firstActivePointPosition;
private int secondActivePointPosition;
private int thirdActivePointPosition;
private int clickCounter;
public Adapter(Context context, ArrayList<PointItem> pointItems) {
this.context = context;
this.layoutInflater = LayoutInflater.from(context);
this.pointItems = pointItems;
//Default points
this.pointItems.get(0).isChecked = true;
this.pointItems.get(1).isChecked = true;
this.pointItems.get(2).isChecked = true;
this.firstActivePointPosition = 0;
this.secondActivePointPosition = 1;
this.thirdActivePointPosition = 2;
}
@Override
public int getCount() {
return pointItems.size();
}
@Override
public Object getItem(int position) {
return pointItems.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
public void checkActivePosition(int position) {
if (!this.pointItems.get(position).isChecked) {
this.pointItems.get(position).isChecked = true;
if (clickCounter == 0) {
this.pointItems.get(this.firstActivePointPosition).isChecked = false;
this.firstActivePointPosition = position;
} else if (clickCounter == 1) {
this.pointItems.get(this.secondActivePointPosition).isChecked = false;
this.secondActivePointPosition = position;
} else if (clickCounter == 2) {
this.pointItems.get(this.thirdActivePointPosition).isChecked = false;
this.thirdActivePointPosition = position;
this.clickCounter = -1;
}
this.clickCounter++;
notifyDataSetChanged();
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
PointItem pointItem = (PointItem) getItem(position);
ViewHolder viewHolder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.point_item_layout, null);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.title.setText(pointItem.title);
if (pointItem.isChecked) {
viewHolder.title.setBackgroundColor(Color.YELLOW);
} else {
viewHolder.title.setBackgroundColor(Color.TRANSPARENT);
}
return convertView;
}
public static class ViewHolder {
public TextView title;
public ViewHolder(View view) {
this.title = (TextView) view.findViewById(R.id.txt_title);
}
}
public static class PointItem {
public int x;
public int y;
public String title;
public boolean isChecked;
public PointItem(int x, int y) {
this.x = x;
this.y = y;
this.title = String.format("(%d, %d)", this.x, this.y);
}
}
}
ArrayList<Adapter.PointItem> pointItems = new ArrayList<>();
pointItems.add(new Adapter.PointItem(0, 0));
pointItems.add(new Adapter.PointItem(1, 1));
pointItems.add(new Adapter.PointItem(2, 2));
pointItems.add(new Adapter.PointItem(3, 3));
pointItems.add(new Adapter.PointItem(4, 4));
pointItems.add(new Adapter.PointItem(5, 5));
pointItems.add(new Adapter.PointItem(6, 6));
pointItems.add(new Adapter.PointItem(7, 7));
pointItems.add(new Adapter.PointItem(8, 8));
pointItems.add(new Adapter.PointItem(9, 9));
Adapter adapter = new Adapter(getBaseContext(), pointItems);
ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Adapter adapter = (Adapter) parent.getAdapter();
adapter.checkActivePosition(position);
}
});