I have a GridView that displays matrix element and each of the element consists of an EditText and a TextView object.
how can I get access to a specific EditText and TextView object in the GridView?
I want to get and set text to EditText and TextView object dynamically. I have developed some code to set text to EditText row+col and to TextView (row, col) but all the EditText show 2 and all TextView show (1,1) Please check my code and help me, I will very thankful to you.
public class GridAdapter extends BaseAdapter
{
Context mContext;
TextView elementSubscript;
EditText elementValue ;
int matrixIndex =0;
public GridAdapter(Context c)
{
mContext = c;
}
public View getView(int position, View convertView, ViewGroup parent)
{
// TODO Auto-generated method stub
View elementLayout;
if(convertView==null)
{
LayoutInflater layoutInflater = getLayoutInflater();
elementLayout = layoutInflater.inflate(R.layout.matrix_element, null);
elementSubscript = (TextView)elementLayout.findViewById(R.id.elementSubscript);
elementValue = (EditText)elementLayout.findViewById(R.id.elementValue);
for(int row=1; row<=5; row++)
{
for(int col=1; col<=4; col++)
{
if(position == matrixIndex)
{
int mIndex =row+col;
elementSubscript.setText("( "+row+","+col+" )");
elementValue.setText(mIndex+"");
matrixIndex++;
}
}
}
}
else
{
elementLayout = convertView;
}
return elementLayout;
}
public int getCount()
{
// TODO Auto-generated method stub
return 20;
}
public Object getItem(int arg0)
{
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position)
{
// TODO Auto-generated method stub
return 0;
}
}
You are not updating the recycled views. Please go through a proper tutorial like this . Check the section 5.5
Modified code:
public class GridAdapter extends BaseAdapter
{
Context mContext;
TextView elementSubscript;
EditText elementValue ;
int matrixIndex =0;
public GridAdapter(Context c)
{
mContext = c;
}
public View getView(int position, View convertView, ViewGroup parent)
{
View elementLayout = convertView;
if(convertView==null)
{
LayoutInflater layoutInflater = getLayoutInflater();//If view is null inflate a view
elementLayout = layoutInflater.inflate(R.layout.matrix_element, null);
}
elementSubscript = (TextView)elementLayout.findViewById(R.id.elementSubscript);
elementValue = (EditText)elementLayout.findViewById(R.id.elementValue);
for(int row=1; row<=5; row++)
{
for(int col=1; col<=4; col++)
{
if(position == matrixIndex)
{
int mIndex =row+col;
elementSubscript.setText("( "+row+","+col+" )");
elementValue.setText(mIndex+"");
matrixIndex++;
}
}
}
//updating the view in case of view reuse and new view
return elementLayout;
}
public int getCount()
{
// TODO Auto-generated method stub
return 20;
}
public Object getItem(int arg0)
{
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position)
{
// TODO Auto-generated method stub
return 0;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.