Jump to content

Par défaut GridLayout sous android


ASEST
 Share

Recommended Posts

Bonjour, 
je suis encore débutante en android et j'ai besoin d'aide. Je dois faire une partie d'une application gestion courses , l'idée est de créer un premier interface qui lister les différents catégories : texte + image comme montre l'image ci_dessous

 

donc pour mettre les images sous cette format je dois utiliser un GridLayout que j'arrive pas comment le faire avec mon code , je le mets mais il affiche que le text quelqu'un peut m'aider dans ce sens là , comment utiliser un GridLayout avec mon cas et en meme temps avec la base SQLite car j'ai créer la Table catégorie.

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
 
import com.example.a.Entity.CategorieEntity;
 
import java.util.ArrayList;
 
public class MainActivity extends AppCompatActivity {
    ArrayList<CategorieEntity> listCategories;
    LinearLayout layoutBtn;
    ArrayList<LinearLayout> listlayoutBtnText;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listCategories = new ArrayList<CategorieEntity>();
        CategorieEntity fruit = new CategorieEntity(1, "Fruit", "desc cate 1", "https://openclipart.org/image/2400px/svg_to_png/280304/set-icone-frutta.png");
        CategorieEntity legume = new CategorieEntity(2, "Legumes", "desc 2", "https://icon-icons.com/icons2/53/PNG/256/fruits_vegetables_10762.png");
        listCategories.add(fruit);
        listCategories.add(legume);
        layoutBtn = (LinearLayout) findViewById(R.id.layoutImageButton);
        listlayoutBtnText = new ArrayList<LinearLayout>();
        for (int i = 0; i < listCategories.size(); i++) {
            //imageCategories.setScaleType(ImageView.ScaleType.FIT_XY);
            ImageView imageCategories = new ImageView(getApplicationContext());
 
 
            GestionImage.loadImageFromUr(getApplicationContext(), listCategories.get(i).getImageCategorie(), imageCategories);
            TextView libelleCategories = new TextView(getApplicationContext());
            libelleCategories.setText(listCategories.get(i).getLibelleCategorie());
            LinearLayout layoutBtnText = new LinearLayout(getApplicationContext());
            layoutBtnText.addView(imageCategories);
            layoutBtnText.addView(libelleCategories);
            listlayoutBtnText.add(layoutBtnText);
            final int finalI = i;
            imageCategories.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    long idCategorie = listCategories.get(finalI).getIdCategorie();
                    Intent intent = new Intent(getApplicationContext(), ListSelonCategorie.class);
                    intent.putExtra("idCategories", idCategorie);
                    startActivity(intent);
                }
            });
        }
        for (int i = 0; i < listlayoutBtnText.size(); i++) {
            layoutBtn.addView(listlayoutBtnText.get(i));
        }
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.zied.coivoturageetudiant.MainActivity">
 
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:rowCount="2"
            android:columnCount="2">
        <!--LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"!-->
 
            <LinearLayout
                android:id="@+id/layoutImageButton"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"></LinearLayout>
 
         </GridLayout>
    </ScrollView>
 
</LinearLayout>

 

cat.png

Link to comment
Share on other sites

  • 4 years later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...