Rashid
Rashid

Reputation: 31

Disappearing Android gridview while scrolling

The code is working perfect until I scroll the gridview up and down twice. When I start scrolling up and down, the contents of gridview starts to disappear from the top and gradually the screen becomes blank. I know it's a recycle issue but with next to zero programming knowledge in my head I am unable to sort out this problem. I searched here for the answers, but I failed to find out the problem of my code. That's why I decided to ask myself.

I have given codes below, I hope someone come across to sort out my isssue.

Main class

public class NEWS extends AppCompatActivity {
    GridView gridView;
    static final String[] MOBILE_OS = new String[] { "Mathrubhumi", "Malayala Manorama","Madhyamam","Deshabhimani", "One India","Marunadan Malayali","Janayugom",
"Janmabhumi","Kvartha","Bignews Live"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news);
       
        gridView = (GridView) findViewById(R.id.gridView);
        gridView.setAdapter(new adapter_news(this, MOBILE_OS));
    }

adapter class

public class adapter_news extends BaseAdapter {
    private ImageView flag;
    private Context context;
    private final String[] countries;
    public adapter_news(Context context, String[] countries) {
        this.context = context;
        this.countries = countries;
    }
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View gridView;
        if (convertView == null) {
            gridView = new View(context);
            gridView = inflater.inflate(R.layout.adapter_news, null);
            TextView textView = (TextView) gridView.findViewById(R.id.label);
            textView.setText(countries[position]);
            flag = (ImageView) gridView.findViewById(R.id.flag);
            String mobile = countries[position];
            if (mobile.equals("Mathrubhumi")) {
                flag.setImageResource(R.drawable.mathrubhumi);
                flag.setOnClickListener(
                        new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent=new Intent("");
                                v.getContext().startActivity(intent);
                            }
                        }
                );
            } else if (mobile.equals("Malayala Manorama")) {
                flag.setImageResource(R.drawable.malayala_manorama);
                flag.setOnClickListener(
                        new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent = new Intent("");
                                v.getContext().startActivity(intent);
                            }
                        }
                );
            } else if (mobile.equals("Madhyamam")) {
                flag.setImageResource(R.drawable.madhyamam);
                flag.setOnClickListener(
                        new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent = new Intent("");
                                v.getContext().startActivity(intent);
                            }
                        }
                );
            } else if (mobile.equals("Deshabhimani")){
                flag.setImageResource(R.drawable.deshabhimani);
                flag.setOnClickListener(
                        new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent = new Intent("");
                                v.getContext().startActivity(intent);
                            }
                        }
                );
            } else if (mobile.equals("One India")){
                flag.setImageResource(R.drawable.oneindia);
                flag.setOnClickListener(
                        new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent = new Intent("");
                                v.getContext().startActivity(intent);
                            }
                        }
                );
            } else if (mobile.equals("Marunadan Malayali")){
                flag.setImageResource(R.drawable.marunadan);
                flag.setOnClickListener(
                        new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent = new Intent("");
                                v.getContext().startActivity(intent);
                            }
                        }
                );
            } else if (mobile.equals("Janayugom")){
                flag.setImageResource(R.drawable.janayugom);
                flag.setOnClickListener(
                        new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent = new Intent("");
                                v.getContext().startActivity(intent);
                            }
                        }
                );
            } else if (mobile.equals("Janmabhumi")){
                flag.setImageResource(R.drawable.janmabhumi);
                flag.setOnClickListener(
                        new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent = new Intent("");
                                v.getContext().startActivity(intent);
                            }
                        }
                );
            } else if (mobile.equals("Kvartha")){
                flag.setImageResource(R.drawable.kvartha);
                flag.setOnClickListener(
                        new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent = new Intent("");
                                v.getContext().startActivity(intent);
                            }
                        }
                );
            } else {
                flag.setImageResource(R.drawable.bignews);
                flag.setOnClickListener(
                        new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent = new Intent("");
                                v.getContext().startActivity(intent);
                            }
                        }
                );
            }
        } else {
            gridView = convertView;
        }
        return gridView;
    }
    @Override
    public int getCount() {
        return countries.length;
    }
    @Override
    public Object getItem(int position) {
        return null;
    }
    @Override
    public long getItemId(int position) {
        return 0;
    }
}

Main xml

<GridView
    android:id="@+id/gridView"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:gravity="center"
    android:numColumns="auto_fit"
    android:columnWidth="140dp"
    android:stretchMode="columnWidth"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent">
</GridView>

Adapter xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingRight="20dp"
android:paddingLeft="20dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:gravity="center">
<ImageView
    android:id="@+id/flag"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dp">
</ImageView>
<TextView
    android:id="@+id/label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAlignment="center"
    android:text="@+id/label"
    android:textSize="15sp" >
</TextView>

Upvotes: 3

Views: 1642

Answers (1)

Tin Tran
Tin Tran

Reputation: 2285

This is the description of the method:

/**
     * Get the row id associated with the specified position in the list.
     * 
     * @param position The position of the item within the adapter's data set whose row id we want.
     * @return The id of the item at the specified position.
     */
@Override
    public long getItemId(int position) {
        return position; // return 0 here means All items are the same;
    }

Base on that, return 0 from here could cause confusion to the listView. You would probably never do this. the return 0 statement is generated by the IDE and have nothing to do with the logic of the method.

Upvotes: 1

Related Questions