Yu Seong Yang Yu Seong Yang - 12 days ago 6
Android Question

GridView only load one item when i start GridView Activity with Intent

When I opened my Activity(named MovieViewActivity.java or .class) with Intent and startActivity(), the activity load a gridView successfully, but the gridView only loads just first and unseen item which is vanished in bottom-out of screen.

At first, i thought it was a problem that related by memory leak, but when i scroll it up and down fastly, it loads their remain items as time goes.

I think a item is loaded when they vanished from screen, and i have know idea how to fix it.
Please help me so i can see all of items at first time that i load a view...

These are pictures about the problem.
(I erased picture and text for my privacy, blue square is picture(ImageView) and black one is text(TextView).)

When I load activity, only first item only shown

After I scroll up and down fastly, every items are appear.

And these are my codes.

Part of MainActivity.java(load MovieViewActivity that has GridView)

@Override
public void onClick (View view){
switch (view.getId()) {
case R.id.movieGallery:
showMovieGallery();
break;
}
}

public void showMovieGallery(){
Intent videoIntent = new Intent(this, MovieViewActivity.class);
startActivity(videoIntent);
}


Whole code of MovieViewActivity.java

package SubViewClass;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.l.workspace.R;

import org.w3c.dom.Text;


public class MovieViewActivity extends AppCompatActivity {
private int[] imgIDs = new int[]{
R.drawable.moviethumb0,
R.drawable.moviethumb1,
R.drawable.moviethumb2,
R.drawable.moviethumb3,
R.drawable.moviethumb4,
R.drawable.moviethumb5,
R.drawable.moviethumb6
};
private String[] imgTitles = new String[]{
"소개 영상", "교육 후기", "다양한 놀이 활동",
"영어로 놀아요", "영어 뮤지컬", "영어 요가", "영어 요리"
};

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

GridView movieGridView = (GridView)findViewById(R.id.movieGrid);

movieAdapter thumbnailGridAdapter = new movieAdapter(
this,
R.layout.griditem,
imgIDs, imgTitles);
movieGridView.setAdapter(thumbnailGridAdapter);

movieGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
showVideoWithPosition(position);
}
});
}
public void showVideoWithPosition(int position){
String movieName = "movie"+position;
Intent videoIntent = new Intent(this, VideoViewActivity.class);
videoIntent.putExtra("pos", movieName);
startActivity(videoIntent);
}

/* 그리드뷰 아이템을 위한 Adapter클래스 */
public class movieAdapter extends BaseAdapter{
private Context context;
private int[] img = null;
private String[] titles = null;
private LayoutInflater inf;
private int layout;

public movieAdapter(Context _context,int _layout, int[] _imgIDs, String[] _titles){
this.context = _context;
this.layout = _layout;
this.img = _imgIDs;
this.titles = _titles;

inf = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public int getCount(){
return (img != null) ? img.length : 0;
}
public Object getItem(int position){
return (img != null) ? img[position] : 0;
}
public long getItemId(int position){
return position;
}

public View getView(int position, View convertView, ViewGroup parent){
ImageView imgView;
TextView txtView;

if(convertView == null){
convertView = inf.inflate(layout, parent, false);
}else{

/* 값을 지정할 이미지뷰와 텍스트뷰를 각각 griditem에서 가져옴. */
imgView = (ImageView)convertView.findViewById(R.id.thumbnailImg);
txtView = (TextView)convertView.findViewById(R.id.thumbnailTxt);

/* GridView의 아이템을 구성할 ImageView와 TextView를 실제로 구성 */
imgView.setImageResource(img[position]);
txtView.setText(titles[position]);
}
return convertView;
}
}
}


Layout XML file of MovieViewActivity

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<GridView
android:id="@+id/movieGrid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="2"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"></GridView>

</LinearLayout>

Answer

Make following changes in MovieAdapter class.

public class MovieAdapter extends BaseAdapter {
    private Context context;
    private int[] img = null;
    private String[] titles = null;
    private LayoutInflater inf;
    private int layout;

    public MovieAdapter (Context context, int[] img, String[] titles, int layout) {
        this.context = context;
        this.img = img;
        this.titles = titles;
        this.layout = layout;
        inf = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
       return (img != null) ? img.length : 0;
    }

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

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

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;

        if (view == null) {
            view = inf.inflate(R.layout.vault_item, null, false);
            holder = new ViewHolder();
            holder.imgView = (ImageView)convertView.findViewById(R.id.thumbnailImg);
            holder.txtView = (TextView)convertView.findViewById(R.id.thumbnailTxt);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        holder.imgView.setImageResource(img[i]);
        holder.txtView.setText(titles[i]);
        return view;
    }

    public static class ViewHolder {
        private TextView txtView;
        private ImageView imgView;
    }
}
Comments