aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/cgMapfileListAdapter.java
blob: 5294ff2494bb3ccccfd68d49605b160d932d924a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package cgeo.geocaching;

import android.app.Activity;
import android.graphics.Typeface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.io.File;
import java.util.List;

public class cgMapfileListAdapter extends ArrayAdapter<File> {

    private cgSelectMapfile parentView;
    private LayoutInflater inflater;
    private MapfileView holder;

    public cgMapfileListAdapter(cgSelectMapfile parentIn, List<File> listIn) {
        super(parentIn, 0, listIn);

        parentView = parentIn;
    }

    @Override
    public View getView(final int position, final View rowView, final ViewGroup parent) {
        if (inflater == null) {
            inflater = ((Activity) getContext()).getLayoutInflater();
        }

        if (position > getCount()) {
            Log.w(Settings.tag, "cgGPXListAdapter.getView: Attempt to access missing item #" + position);
            return null;
        }

        File file = getItem(position);

        View v = rowView;

        if (v == null) {
            v = inflater.inflate(R.layout.mapfile_item, null);

            holder = new MapfileView();
            holder.filepath = (TextView) v.findViewById(R.id.mapfilepath);
            holder.filename = (TextView) v.findViewById(R.id.mapfilename);

            v.setTag(holder);
        } else {
            holder = (MapfileView) v.getTag();
        }

        File current = new File(parentView.getCurrentMapfile());

        if (file.equals(current)) {
            holder.filename.setTypeface(holder.filename.getTypeface(), Typeface.BOLD);
        } else {
            holder.filename.setTypeface(holder.filename.getTypeface(), Typeface.NORMAL);
        }

        final touchListener touchLst = new touchListener(file);
        v.setOnClickListener(touchLst);

        holder.filepath.setText(file.getParent());
        holder.filename.setText(file.getName());

        return v;
    }

    private class touchListener implements View.OnClickListener {
        private File file = null;

        public touchListener(File fileIn) {
            file = fileIn;
        }

        // tap on item
        public void onClick(View view) {
            parentView.setMapfile(file.toString());
            parentView.close();
        }
    }

    private static class MapfileView {
        public TextView filepath;
        public TextView filename;
    }
}