Work with images in Android Studio

Asked

Viewed 779 times

1

I wonder if there is a way to work with images on android. I have a List View, in this list I have several categories. When the user selects this category, he would like it to load all the images related to these categories. However I don’t know how to do this, I’m using Fragments. I have an xml file with the list view and another with Fragment where the images would appear.

This and the main class:

public class MainActivity extends FragmentActivity {
FragmentManager fm = getSupportFragmentManager();
ListView listItemView;

String[] listaCategorias = new String[]{"INÍCIO", "RESPOSTAS","PERGUNTAS","ALFABETO","ALMOÇO","BEBIDAS", "COMO ESTOU ME SENTINDO",
        "EU QUERO","FAMILIA", "FRUTAS", "HIGIENE FEMININA", "HIGIENE MASCULINA","INFORMÁTICA", "LEGUMES", "MANICURE",
        "MAQUIAGEM","MUSICAS", "NUMERAIS","ONDE ESTÁ?", "SENTIMENTOS", "TAMANHOS", "VERDURAS"};


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    final Fragment1 fragment1 = (Fragment1) fm.findFragmentById(R.id.fragment1);
    listItemView = (ListView) findViewById(R.id.listView1);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, listaCategorias);
    listItemView.setAdapter(adapter);

    listItemView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(MainActivity.this, listaCategorias[position], Toast.LENGTH_SHORT).show();
            switch (position){
                case 1:


            }
        }
    });

}

And this is the Fragment class, in which the images would appear:

public class Fragment1 extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup conteiner, Bundle saveInstanceState) {

    View view = inflater.inflate(R.layout.layout_frag_1, conteiner, false);
    /*ImageView  imageView   = null;
    imageView.findViewById( R.id.imageView ) ;

    Glide . with ( this )
            . load ( "http://goo.gl/gEgYUd" )
            . into ( imageView ) ;*/
    return (view);

}
  • You have the Urls of all the images you want to upload?

  • Yes, they’re all in the drawable folder

  • R.id.imageView is a variable of type int that points to the drawable folder. Mas.... it is much simpler to load an array already defined and pass as parameter when clicking on the list...(an array of type int filled with paths). Ex: Clicked position 5 of the list(food category) > loads an array > passes Aray by parameter > fills list. Ex: int [] images= new int[10]; images[0] = R.id.img1; // and so on

1 answer

0


I don’t know if I understand your question. But have you considered creating an object that has the image reference and category reference? With this Voce could sort or filter according to objects.

class Foo {
     private int imageRef;
     private int categoryRef;
     ...
}

Then you could pass the object array to Listview, and you would also have to create a custom Arrayadapter. I hope your doubt was that flws.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.