简体   繁体   English

如何在gridview中显示图像

[英]How to display Images in gridview

I want to display images from drawble, but my app have trouble when i run it, gtidview is not display images, so i need everyone help.我想显示来自 drawble 的图像,但是我的应用程序在运行时遇到问题,gtidview 不显示图像,所以我需要大家的帮助。

here My class is MinhAcivity.java这里我的课是MinhAcivity.java

  public class MinhActivity extends Activity {
   GridView gridView;
   int categoryImages[] ={R.drawable.android1,R.drawable.android2,R.drawable.android3,R.drawable.android4, R.drawable.android5, R.drawable.android6};

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

        gridView = findViewById(R.id.grView_categories);
        Adapter_Category adapter_category = new Adapter_Category(MinhActivity.this,categoryImages);
        adapter_category.notifyDataSetChanged();
        gridView.setAdapter(adapter_category);
    }
}

and My adapter class is Adapter_Category.java我的适配器类是Adapter_Category.java

public class Adapter_Category extends BaseAdapter {
     LayoutInflater inflater;
 Context context;
    int  arr[];
    public Adapter_Category( Context txt,int[] arrlist){
        this.context = txt;
        this.arr = arrlist;
    }

    public Adapter_Category(MinhActivity txt, ArrayList arrayList) {
    }

    @Override
    public    int getCount() {
        return arr.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

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

Maybe error can display here;也许这里会显示错误;

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (inflater==null) {
            inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        }
        if (convertView == null)
        {
            convertView = inflater.inflate(R.layout.single_categories,null);

        }
        ImageView imageView = convertView.findViewById(R.id.imageView);
         imageView.setImageResource(arr[position]);
        return convertView;
    }
}

You can refer to this link https://www.homeandlearn.co.uk/android/grid_view_set_adapter.html你可以参考这个链接https://www.homeandlearn.co.uk/android/grid_view_set_adapter.html

But this is not the best way to do it.Use Recylerview with GridLayoutManager for better performance ref https://abhiandroid.com/materialdesign/recyclerview-gridview.html但这不是最好的方法。使用 Recylerview 和 GridLayoutManager 以获得更好的性能参考https://abhiandroid.com/materialdesign/recyclerview-gridview.html

Your code is running and a GridView is shown on my device:您的代码正在运行并且GridView显示在我的设备上:

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

GridView gridView;
int categoryImages[] ={R.mipmap.ic_launcher_round, R.drawable.ic_launcher_foreground, R.mipmap.ic_launcher_round,
R.drawable.ic_launcher_foreground, R.mipmap.ic_launcher, R.drawable.ic_launcher_foreground};

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


    gridView = findViewById(R.id.grView_categories);
    Adapter_Category adapter_category = new Adapter_Category(MainActivity.this, categoryImages);
    adapter_category.notifyDataSetChanged();
    gridView.setAdapter(adapter_category);
}

The Adapter : Adapter

public class Adapter_Category extends BaseAdapter {

    LayoutInflater inflater;
    Context context;
    int arr[];

    public Adapter_Category(Context txt, int[] arrlist) {
        this.context = txt;
        this.arr = arrlist;
    }

    public Adapter_Category(MainActivity txt, ArrayList arrayList) {
    }

    @Override
    public int getCount() {
        return arr.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (inflater == null) {
            inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        }
        if (convertView == null)
        {
            convertView = inflater.inflate(R.layout.single_categories, null);

        }
        ImageView imageView = convertView.findViewById(R.id.imageView);
        imageView.setImageResource(arr[position]);
        return convertView;
    }
}

The .xml looks like: .xml 看起来像:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<GridView
    android:id="@+id/grView_categories"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:numColumns="3"
    android:gravity="center" />

</androidx.constraintlayout.widget.ConstraintLayout>

If this code is not helping, please add some error logs.如果此代码没有帮助,请添加一些错误日志。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM