gosulove gosulove - 1 month ago 9
Android Question

Android failed to loop through all the Numbers in Gridview

I try to use Gridview to display total number of Tables in a Restaurant. User can set the total number of Tables by integer.

Each table has an ID starting from 1. However, all my table numbers are the same. Looks like something is wrong with the loop?

class my_adapter extends BaseAdapter{

LayoutInflater inflater=null;
Context ctx;
int table_count;

my_adapter(Context ctx, int table_count){
this.ctx=ctx;
this.table_count=table_count;
}

@Override
public int getCount() {
return table_count; // total no. of tables
}

@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) {

View row=view;

if(row==null){
inflater=(LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row=inflater.inflate(R.layout.single,null);
}

TextView tv_table_no=(TextView)row.findViewById(R.id.table_no);

for(int count=1;count<=table_count;count++){
tv_table_no.setText("table number: "+count);
}

return row;
}
}


If table_count is 9, then there will be 9 tables with table no. from 1- 9.

Anyone knows what's wrong here?

enter image description here

Answer

Replace this

for(int count=1;count<=table_count;count++){
    tv_table_no.setText("table number: "+count);
}

by

    tv_table_no.setText("table number: "+i);

Hope for the best!

Comments