Добавить ImageView в ArrayAdapter - PullRequest
0 голосов
/ 30 марта 2020

Я работаю над проектом Android для учебы и создаю игру Uno (карточная игра), но у меня есть проблема, я использую карты стандартных игроков в ArrayAdapter, он работал со String, но когда я попробуйте поместить ImageView в мой ArrayAdapter, он выглядит следующим образом: введите описание изображения здесь

Это мой MainActivity:

public class MainActivity extends AppCompatActivity {
//public static TextView text;
public static TextView textTJ;
public static TextView textTP;
public static TextView textLC;
public static GridView gridCartes;
private Button button;
private ImageButton ib;


@SuppressLint("WrongViewCast")
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
   // text = findViewById(R.id.textPrincipal);
    gridCartes  = findViewById(R.id.gridCartes);
    textTJ = findViewById(R.id.textTourJoueur);
    textTP = findViewById(R.id.textTaillePioche);
    textLC = findViewById(R.id.textLastCard);
    //ib = findViewById(R.id.imageButton3);
    //Context context = ib.getContext();
    //int id = context.getResources().getIdentifier("unoprojetcarteverso1v", "drawable", context.getPackageName());
   // ib.setImageResource(id);

    ArrayAdapter arrayAdapter = new ArrayAdapter<>(this,android.R.layout.simple_expandable_list_item_1);
    gridCartes.setAdapter(arrayAdapter);

    this.button = findViewById(R.id.buttonPartie);

    this.button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            Toast.makeText(getApplicationContext() , "Bouton pressé", Toast.LENGTH_SHORT).show();
            ArrayList<Joueur> j = new ArrayList<>();
            j.add(new Joueur(1, "Cesar"));
            j.add(new Joueur(2, "Enzo"));
            j.add(new Joueur(3, "Matthias\t"));
            j.add(new Joueur(4, "Paul\t\t"));
            Partie p = new Partie(j, getApplicationContext());


        }
    });

}

И это часть моего Класс игры, где я помещаю Imageview в ArrayAdapter:

public void tourJoueur(final Joueur j) {
    ImageView iv =new ImageView(context);
    if (!j.getJeu().isEmpty()) {
        MainActivity.textLC.setText("Premiere carte placee : " + last.getId());
        MainActivity.textTJ.setText("=========== Au tour de " + j.getNom() + " ! ===========");
        MainActivity.textTP.setText("Taille de la pioche : " + this.tas.size());
        System.out.print("Ton jeu :  ");
        ArrayAdapter aa = (ArrayAdapter) MainActivity.gridCartes.getAdapter();
        aa.clear();
        for (Carte c : j.getJeu()) {
            iv.setImageResource(R.drawable.unoprojetcarteverso1v);
            aa.add(iv);
        }
        if(!apioche)
            aa.add("Piocher");
        else {
            apioche=false;
            aa.add("Passer");
        }
        final Carte[] cs = {null};
        MainActivity.gridCartes.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.O)
            @Override
            public void onItemClick(AdapterView<?> a, View v, int position, long id) {
                Object o = MainActivity.gridCartes.getItemAtPosition(position);
                try {
                    Toast.makeText(context,o+"",Toast.LENGTH_SHORT).show();
                    cs[0] = (Carte) o;
                    boolean b = placeCarte(cs[0], j);
                    if (b) {
                        Toast.makeText(context, "Tu ne peux pas jouer cette carte", Toast.LENGTH_SHORT).show();
                    } else {
                        if(j.aGagne()){
                            Log.d(TAG, "tourJoueur: " + j.getNom() + " n'a plus de cartes");
                            partieFinie();
                        }
                        tourJoueur(joueurSuivant(j));
                    }
                } catch (ClassCastException e){
                    try {
                        String cont = (String) o;
                        if(cont.equalsIgnoreCase("piocher")) {
                            piocher(j);
                            apioche = true;
                            tourJoueur(j);
                        } else if(cont.equalsIgnoreCase("passer")){
                            tourJoueur(joueurSuivant(j));
                        }
                    } catch (ClassCastException ee){
                        e.printStackTrace();
                    }
                }
            }
        });
    }
}
...