João Azevedo
João Azevedo

Reputation: 343

Select and switch items in android

I have a ListView with items of points (latitude and longitude) and i want to select the 3 closest by default and let the user switch any of those 3 points with any other point in the list (the main purpose of 3 points is for spacial interpolation). Is this possible or how can i make something similar?

Example:

The user is in position (lat, lon). The ListView have the items (x1, y1), (x2,y2), (x3,y3), (x4,y4), ...

The app select the 3 closest items of user position by default, for example the 3 first items. And now the user must be able to switch for example (x1,y1) with (x4,y4).

Thanks.

Upvotes: 1

Views: 736

Answers (1)

GIGAMOLE
GIGAMOLE

Reputation: 1266

Check my solution. I`ve tried to create the answer of what you need. This is simple and easy.

Here is the code:

Adapter

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);
        }
    }
}

Initialize

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);
            }
        });

Upvotes: 1

Related Questions