I am working on an Android Application , here I need to display data in the GridView , The problem I am facing now is that value of position variable is repeating inside the getView(int position, View convertView, ViewGroup parent) method of the subclass of BaseAdapter . I tried to find out the issue after removing additional code from the subclass , still result is the same . I visited similar posts on stackoverflow but no luck .
Here is the code of LazyAdapter (subclass of BaseAdapter)
public class LazyAdapter extends BaseAdapter {
private Context ctx;
private List<ItemDetails> items;
BooksViewHolder booksViewHolder = null ;
public LazyAdapter(Context context, List<ItemDetails> arrayList) {
super();
ctx = context;
this.items = arrayList;
}
public int getCount() {
return items.size();
}
public Object getItem(int position) {
return items.get(position);
}
public long getItemId(int position) {
return items.indexOf(getItem(position));
}
class BooksViewHolder{
RelativeLayout containerLayout , playout , prelayout , offlinetext , thumblayout ;
TextView text , priceText;
ImageView image ;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(convertView==null) {
booksViewHolder = new BooksViewHolder();
booksViewHolder.prelayout = (RelativeLayout) convertView.findViewById(R.id.prelayout);
convertView = inflater.inflate(R.layout.bookcontent, null);
booksViewHolder.text = (TextView) convertView.findViewById(R.id.bookname);
booksViewHolder.priceText = (TextView) convertView.findViewById(R.id.pricetext);
convertView.setTag(booksViewHolder);
} else {
booksViewHolder = (BooksViewHolder) convertView.getTag();
}
prelayout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
bookClickListener.onBookClick(position);
}});
return convertView;
}
interface OnBookClickListener{
void onBookClick(int bookPosition);
}
public void setOnBookClickListenre(OnBookClickListener clickListener){
bookClickListener = clickListener ;
}
}
MyActivity.java :
public class MyActivity extends Activity implements LazyAdapter.OnBookListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.info);
//Code to get the reference of Gridview .
// set the listener on Gridview
}
@Override
public void onBookClick(int position){
BookModel book= lazyadapter.getItems().get(position);
//Here due to incorrect position I am not able to get particular book .
}
}
Help me please , I am not able to figure out the issue .