0

I am trying to display the image by clicking on the "next" button but this code doesn't work.

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;

public class MyGridView extends Activity {
    GridView mGridView;
    int counter,i;


public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mygridacti);


    mGridView = (GridView)findViewById(R.id.grdvw);
    //mGridView.setAdapter(new MyImageAdapter(this));

    ImageView imageview = (ImageView)findViewById(R.id.GalleryView);
    Button btnnext = (Button)findViewById(R.id.btn_nxt);        

    Log.i("counter", ""+counter);   
    btnnext.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            //for(i=0;i<=5;i++){
                mGridView.setAdapter(new MyImageAdapter(getApplicationContext()));
                Log.i("counter", ""+counter);
            //}
            // TODO Auto-generated method stub

            //mGridView.setAdapter(new MyImageAdapter(getApplicationContext()));
            //Toast.makeText(MyGridView.this, "No Images", Toast.LENGTH_SHORT).show();
            }
        }
    );
}
 }

class MyImageAdapter extends BaseAdapter{
  private Context mContext;
  ImageView imageview;
  int counter;

public MyImageAdapter(Context c){
    mContext = c;
}
public MyImageAdapter(OnClickListener onClickListener) {
    // TODO Auto-generated constructor stub

}
@Override
public int getCount() {
    // TODO Auto-for(i = 6;i<=counter;i++) {generated method stub
    //counter = mThumbsIds.length;
    //System.out.println("Counter = "+counter);

    return 5;

}
@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    if(convertView == null){
        imageview = new ImageView(mContext);
        imageview.setLayoutParams(new GridView.LayoutParams(85,85));
        imageview.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageview.setPadding(8, 8, 8, 8);
    }
    else{
        imageview = (ImageView) convertView;
    }
    //int[] mThumbIds = null;
    imageview.setImageResource(mThumbsIds[position]);
    return imageview;
    //return null;
}
private Integer[] mThumbsIds = {
        R.drawable.image1,R.drawable.image2,
        R.drawable.image3,R.drawable.image4,
        R.drawable.image5,R.drawable.icon,
        R.drawable.icon,R.drawable.icon,
        R.drawable.icon};
 }

Please help me out to correct this.

jzd
  • 23,473
  • 9
  • 54
  • 76
Saurabh
  • 37
  • 12
  • What does , "does not work" mean ? Also , why are you returning null from getItem() in the adapter class ? @Override public Object getItem(int position) { // TODO Auto-generated method stub return null; } – Salil Apr 22 '11 at 10:40
  • then what should i have to return instead of null over there? – Saurabh Apr 22 '11 at 10:45
  • @Saurabh - Depends on what you want to retrieve from the listView , but I guess in your case it would be mThumbsIds[position] – Salil Apr 22 '11 at 10:47
  • yes its mThumbsIds[position].But by giving null i m getting the images as output. but my question is that "i want to display "5 images" on starting n then by clicking "next" button the rest 4 images should be displayed as here here total 9 images 1st time i want 5 image n then i want 4 images" very confused for this. – Saurabh Apr 22 '11 at 10:51
  • Not sure but I guess you need to refresh the data in the adapter, have a look at the BaseAdapter help - http://developer.android.com/reference/android/widget/BaseAdapter.html#notifyDataSetChanged() – Salil Apr 22 '11 at 10:57
  • means? how could the adapter be refreshed? i didn't get u.. – Saurabh Apr 22 '11 at 10:58
  • refreshed means pass new data to the adapter ( in your case the remaining 4 images ) and call notifyDataSetChanged(). See this - http://stackoverflow.com/questions/2250770/how-to-refresh-android-listview – Salil Apr 22 '11 at 11:10
  • is there not any other way to solve this? – Saurabh Apr 22 '11 at 11:19
  • Consider doing a search on StackOverflow,they do provide with a search option , its on the top right hand corner. See if you can find any other solution - http://stackoverflow.com/search?q=%5Bandroid%5D+refresh+ListView – Salil Apr 22 '11 at 11:23

1 Answers1

0

To refresh grid view see this search result

Community
  • 1
  • 1
Salil
  • 496
  • 1
  • 5
  • 15
  • @Saurabh - sorry for the list view answer, updated it with search results for GridView refresh. – Salil Apr 22 '11 at 11:44
  • ohk..thanks a lot.if u got any related solution then please post that here. – Saurabh Apr 22 '11 at 11:46
  • I understand that you need pagination in a grid , http://stackoverflow.com/questions/4449416/android-pagination-in-gridview and http://stackoverflow.com/questions/2764019/android-gridview-and-paging might help. – Salil Apr 22 '11 at 12:05