0

Here is My GridView implement for android application. For Program First time it Work well.But after making some Scrolling Up and Down My problem is when in gridview i scroll it some time put wrong textview in unexpected position.

My Source Code For GridView Adapter is Following.

   @Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mGridView=(GridView)findViewById(R.id.gridview_id);
    InitializeVariable();
    mGridView.setAdapter(new GridAdapter(GridViewExampleActivity.this));
    mGridAdapter=new GridAdapter(GridViewExampleActivity.this);
   // mGridView.setEnabled(false);
    mGridView.setOnLongClickListener(this);
    mGridView.setOnItemClickListener(this);

}

    public class GridAdapter extends BaseAdapter{
    public  Context mContext_GridAdapter=getApplicationContext();

    GridAdapter(Context context){
        this.mContext_GridAdapter=context;
    }

    @Override
    public int getCount() {

        return Fix_Grid_View;
    }

    @Override
    public Object getItem(int position) {

        return position;
    }

    @Override
    public long getItemId(int position) {

        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        RelativeLayout mRelative_Box_Grid;
        TextView mTextView;
        View v;
        if(convertView==null){
            mLayoutInflater=LayoutInflater.from(GridViewExampleActivity.this);
            v=mLayoutInflater.inflate(R.layout.relative_box_view, null);
            mTextView=(TextView)v.findViewById(R.id.txt_number);
            mRelative_Box_Grid=(RelativeLayout)v.findViewById(R.id.relative_view);
            mRelative_Box_Grid.setLayoutParams(new GridView.LayoutParams(GridView.AUTO_FIT,Device_Height));
            mRelative_Box_Grid.setPadding(2, 2, 2, 2);
            mTextView.setTextSize(Fix_Text_Size);
            mTextView.setText(String.valueOf(position));
            mTextView.setId(position);

        }else{
            v=convertView;
        }
        return v;
    }

}

Here is my Main xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<GridView android:numColumns="3" 
    android:stretchMode="columnWidth"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:id="@+id/gridview_id"
    android:longClickable="true"
    ></GridView>
 </LinearLayout>

i put same main.xml file in res/layout-land for Landscape mode .

and i also add this line in my AndroidManifest file for handle changes in orientation

android:configChanges="orientation"

and i also implement when we click any item at that position that TextView gone for invisible here is code for that.

    @Override
public void onItemClick(AdapterView<?> AdapterView, View view, int position, long arg3) {

    int Child_Count;
    RelativeLayout mRelativeLayout;
    TextView mTextView;

    try{
        Toast.makeText(GridViewExampleActivity.this, "Position "+ position, 600).show();
        Child_Count=AdapterView.getChildCount();
        mRelativeLayout=(RelativeLayout) AdapterView.getChildAt(position);
        mTextView=(TextView) mRelativeLayout.getChildAt(0);
        mTextView.setVisibility(View.INVISIBLE);
       // mGridAdapter.notifyDataSetChanged();
       // mGridView.setAdapter(mGridAdapter);
        Log.v(TAG, "Child Count is"+ Child_Count);
    }catch(Exception e){
        Log.v("log", " Mes"+TAG +e.getMessage());
    }


}

this function work well but fail for some time after making some orientation changes in my Emulator Using Ctrl+F11 Keys. Here is my Inflater View xml file.

 <?xml version="1.0" encoding="utf-8"?>
   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" 
android:background="@drawable/rectangle_white"
android:id="@+id/relative_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:text="@string/hello"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:id="@+id/txt_number"/>
   </RelativeLayout>

Here i attach Reference Image of my demo application for problem i Get.

enter image description here

For landscape

Look at First image when i Scroll up and down value of Textview interchange Unexpected

and when same change in landscape view of android mobile it some time start from 11 that should not happen in such manner.

Herry
  • 7,037
  • 7
  • 50
  • 80

2 Answers2

1

In your Adapter, try moving these lines:

mTextView=(TextView)v.findViewById(R.id.txt_number);
mTextView.setText(String.valueOf(position));

to be just above your return statement:

return v;

Also, I'm not sure, but i don't think you need the line mTextView.setId(position);, as the position of the view is provided in the onClick callback.

Paul Burke
  • 25,496
  • 9
  • 66
  • 62
  • @Hello iPaulPro Thanks For Response But i try you Think also but it give me 10-22 23:48:49.152: ERROR/AndroidRuntime(897): java.lang.NullPointerException and much more Error List. – Herry Oct 22 '11 at 18:22
  • @Herry - Sorry, you must also move mTextView=(TextView)v.findViewById(R.id.txt_number). I have updated the answer accordingly. You might want to look into the ViewHolder pattern (check out List14.java in APIDemos). – Paul Burke Oct 23 '11 at 02:04
  • @Hello Have you idea how to remove scrolling functionally from GridView or may be that can not possible what you think. – Herry Oct 23 '11 at 15:57
  • @Herry - Your welcome. On scrolling - see these answers - http://stackoverflow.com/questions/4852867/how-to-disable-gridview-scrolling-android – Paul Burke Oct 23 '11 at 19:20
  • i think use that setEnabled(false) will disable all functionally like onItemClickListner also so that not use full for me. – Herry Oct 24 '11 at 04:31
  • @Herry - Sorry that didn't work for you. Your only option may be to ensure that the GridView is smaller than its parent. Please remember to accept this answer if it solved your original issue. – Paul Burke Oct 24 '11 at 05:37
  • @Yes i will sure accept you answer if it solve my original problem ok. – Herry Oct 24 '11 at 05:52
0

At first glance, I notice the following:

mGridView=(GridView)findViewById(R.id.gridview_id);
InitializeVariable();
mGridView.setAdapter(new GridAdapter(GridViewExampleActivity.this));
mGridAdapter=new GridAdapter(GridViewExampleActivity.this);

Why do you create two GridAdapters? I would think you only need one like so:

mGridView=(GridView)findViewById(R.id.gridview_id);
InitializeVariable();
// Create the GridAdapter
mGridAdapter=new GridAdapter(GridViewExampleActivity.this);
// Set the Adapter tp the gridview
mGridView.setAdapter(mGirdAdapter);

Furthermore, take a look at your code for the Adapter's getView method. Android will try to re-use views as much as possible. You only instantiate your views, when convertView == null, e.g. only for the first views that are populated.

After scrolling, Android will try to re-use those views. Hence, convertView will not be null, and the code which sets the text in the views is not executed. Change to this:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    RelativeLayout mRelative_Box_Grid;
    TextView mTextView;
    View v;

    // Assign the re-usable view to v
    v = convertView;

    if(v==null){
        // Create views if v == null
        mLayoutInflater=LayoutInflater.from(GridViewExampleActivity.this);
        v=mLayoutInflater.inflate(R.layout.relative_box_view, null);

        // NOTE: This will only be executed for the Views that will be
        // visible initially (so, for 0 through 11)
    }

    // Setup your view to show the proper values
    // NOTE: This will always be executed
    mTextView=(TextView)v.findViewById(R.id.txt_number);
    mRelative_Box_Grid=(RelativeLayout)v.findViewById(R.id.relative_view);
    mRelative_Box_Grid.setLayoutParams(new GridView.LayoutParams(GridView.AUTO_FIT,Device_Height));
    mRelative_Box_Grid.setPadding(2, 2, 2, 2);
    mTextView.setTextSize(Fix_Text_Size);
    mTextView.setText(String.valueOf(position));
    mTextView.setId(position);

    return v;
}

Good luck

Entreco
  • 12,738
  • 8
  • 75
  • 95
  • "You only instantiate your views, when convertView == null" -- Isn't that exactly what he's doing (if(convertView==null))? I believe the important fix here is moving mTextView.setText(String.valueOf(position)); to be just above the return statement. – Paul Burke Oct 22 '11 at 13:38
  • Exactly! I just meant instantiating & initializing. I added code with comments, to indicate that his textview's text is not being set. And how to fix it – Entreco Oct 22 '11 at 14:24
  • @Entreco i try to move as you describe it fail to do and give run time error – Herry Oct 22 '11 at 18:37
  • Okay, than try to only move the lines: mTextView.setText(...) and mTextView.setId(...) outside the if-statement, like described in the other answer. The text being set only when the convertView==null is defenitaly the cause of your problems... – Entreco Oct 25 '11 at 12:21