Bonjour à tous
j'utilise une listview pour lire un fichier .csv avec un onclick listener qui sélectionne la view activée.
Or quand on scrolle la listview la sélection disparait puisqu'on recharge la view.
Du coup j'ai commencé à écrire un singleton pour implémenter un array de positions sélectionnées mais je bute sur la façon de le recharger quand je reviens sur mon activité contenant ma listview.
En réfléchissant je me dis que ça serait bizarre quand même qu'il n'y ait pas une fonction pour le faire vu que c'est une fonction quand même basique de sélectionner une view.
non????
Heeeeeelp
voilà mon code :
ma classe adapter
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package com.listfiles;
 
import android.content.Context;
import android.content.Intent;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.RelativeSizeSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.Toast;
 
import java.util.List;
 
public class MyListAdapter extends ArrayAdapter<String[]>{
 
    int groupid;
    List<String[]> items;
    Context context;
    String test;
    String test2;
    String test3;
    String test4;
 
 
 
    Boolean fin = false;
    Boolean debut = false;
 
    public MyListAdapter(Context context, int vg, int id, List<String[]> items){
        super(context,vg, id, items);
        this.context=context;
        groupid=vg;
        this.items=items;
 
    }
    static class ViewHolder {
        public TextView textid;
 
    }
    public View getView(final int position, View convertView, ViewGroup parent) {
 
        View rowView = convertView;
        Toast.makeText(getContext(),Singleton.getInstance().getList().toString(), Toast.LENGTH_LONG).show();
 
 
        if(rowView==null){
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            rowView= inflater.inflate(groupid, parent, false);
            ViewHolder viewHolder = new ViewHolder();
            viewHolder.textid = (TextView) rowView.findViewById(R.id.txtid);
 
            rowView.setTag(viewHolder);
        }
 
        // Fill data
        ViewHolder holder = (ViewHolder) rowView.getTag();
        String[] row=items.get(position);
 
 
        test = row[0].toString();
        test2 = row[1].toString();
        test3 = row[2].toString();
        test4 = row[3].toString();
        SpannableString pouf = new SpannableString(test);
        pouf.setSpan(new RelativeSizeSpan(1.2f), 0, pouf.length(), 0);
        SpannableString pouf2 = new SpannableString(test2);
        pouf2.setSpan(new RelativeSizeSpan(1.2f), 0, pouf2.length(), 0);
        SpannableString pouf3 = new SpannableString(test3);
        pouf3.setSpan(new RelativeSizeSpan(1f), 0, pouf3.length(), 0);
        SpannableString pouf4 = new SpannableString(test4);
        pouf4.setSpan(new RelativeSizeSpan(1f), 0, pouf4.length(), 0);
 
 
        holder.textid.setText(TextUtils.concat(pouf," : ",pouf2,"\n",pouf3, "\n",pouf4));
 
        rowView.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
 
 
                Singleton.getInstance().setString("youpi");
                String[] record = items.get(position);
                //v.setBackgroundColor(Color.RED);
 
 
                v.setSelected(true);
                if(position == 0){
                    debut =true;
                    fin = false;
                    int place = position;
                    //Toast.makeText(getContext(),String.valueOf(position),Toast.LENGTH_LONG).show();
                    Intent intent=new Intent(context, Main3Activity.class);
                    intent.putExtra("numéro", record[1]);
                    intent.putExtra("localite", record[2]);
                    intent.putExtra("adresse", record[3]);
                    intent.putExtra("dernier", fin);
                    intent.putExtra("premier",debut);
                    intent.putExtra("pos",position);
 
                    context.startActivity(intent);
                }
                else if(position==getCount()-1){
                    //Toast.makeText(getContext(), "je suis le dernier",Toast.LENGTH_LONG).show();
                    fin = true;
                    debut=false;
                    Intent intent=new Intent(context, Main3Activity.class);
                    intent.putExtra("numéro", record[1]);
                    intent.putExtra("localite", record[2]);
                    intent.putExtra("adresse", record[3]);
                    intent.putExtra("dernier", fin);
                    intent.putExtra("premier",debut);
                    intent.putExtra("pos",position);
                    context.startActivity(intent);
                }
                else{
                    fin = false;
                    debut=false;
                    //Toast.makeText(getContext(),String.valueOf(position),Toast.LENGTH_LONG).show();
                Intent intent=new Intent(context, Main3Activity.class);
                intent.putExtra("numéro", record[1]);
                intent.putExtra("localite", record[2]);
                intent.putExtra("adresse", record[3]);
                    intent.putExtra("dernier", fin);
                    intent.putExtra("premier",debut);
                    intent.putExtra("pos",position);
                context.startActivity(intent);
            }}
 
 
        });
 
        return rowView;
 
 
 
    }
 
}