Add determined image to gridview

0

I'm doing a horoscope app, I did it with an adapter and I need to assign each image to the sign so that it remains in the grid.

Can I add the image in the arraylist? or where and with what parameters?

attached main activity and the adapter

    import android.content.Intent;
    import android.os.Bundle;
    import android.os.Handler;
    import android.support.design.widget.CoordinatorLayout;
    import android.support.design.widget.FloatingActionButton;
    import android.support.design.widget.Snackbar;
    import android.support.v4.widget.SwipeRefreshLayout;
    import android.text.Layout;
      import android.util.Log;
     import android.view.KeyEvent;
       import android.view.View;
      import android.support.design.widget.NavigationView;
       import android.support.v4.view.GravityCompat;
        import android.support.v4.widget.DrawerLayout;
         import android.support.v7.app.ActionBarDrawerToggle;
       import android.support.v7.app.AppCompatActivity;
       import android.support.v7.widget.Toolbar;
        import android.view.Menu;
        import android.view.MenuItem;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
    import android.webkit.WebViewClient;
      import android.widget.AdapterView;
      import android.widget.Toast;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.GridView;

import java.util.ArrayList;

       public class MainActivity extends AppCompatActivity {

        private GridView gridView;
         private GridAdapter adapter;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add("Aries");
        arrayList.add("Tauro");
        arrayList.add("Geminis");
        arrayList.add("Cancer");
        arrayList.add("Leo");
        arrayList.add("Virgo");
        arrayList.add("Libra");
        arrayList.add("Escorpio");
        arrayList.add("Sagitario");
        arrayList.add("Aries");
        arrayList.add("Acuario");
        arrayList.add("Piscis");


        gridView = (GridView) findViewById(R.id.grid);
        adapter = new GridAdapter(this,arrayList );
        gridView.setAdapter(adapter);

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent intent = new Intent(MainActivity.this, Descripcion.class);
                intent.putExtra("Nombre" , adapter.getItem(position).toString() ) ;
                startActivity(intent);

            }
        });


    }


   }
 import android.content.Context;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;

public class GridAdapter   extends BaseAdapter{

    private Context context;
    private ArrayList <String> arrayList;

    public GridAdapter (Context context, ArrayList <String> arrayList) {

        this.context = context;
        this.arrayList = arrayList;


    }


    @Override
    public int getCount() {
        return arrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return arrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {

            LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.item_grid,null);

        }

        TextView tituloSigno = (TextView) convertView.findViewById(R.id.titulo);
        tituloSigno.setText(arrayList.get(position));

        return convertView;
    }
}
    
asked by pauuu 20.11.2018 в 19:55
source

1 answer

-1

If the images are in the drawable folder, make an array of images in onCreate of the Activity and before configure the adapter:

 int[] imagenes = {R.drawable.tu_imagen1, ..., R.drawable.tu_imagen12};

and you add it to the adapter:

 adapter = new GridAdapter(this,arrayList,imagenes);

Quite possibly you will mark error (underlined in red) this is because the images parameter is not in the constructor of the Adapter class. Simply on the red icon to the left (like a red bulb!) click and choose the option Add int[] as 3rd parameter , to add it to the constructor, or you enter it manually.

In the GridAdapter declares the int array:

 private int []mImagen;

The constructor should look like this:

 public GridAdapter (Context context, ArrayList <String> arrayList, int[] imagenes){
    this.context = context;
    this.arrayList = arrayList;
    this.mImagen = imagenes;
  }

And in getView you assign the corresponding image:

 tuImageView.setImageResource(mImagen[position]);
    
answered by 20.11.2018 в 21:31