我有一个显示矩阵元素的GridView,每个元素都包含一个EditText和一个TextView对象。
如何在GridView中访问特定的EditText和TextView对象?
我想动态地将文本设置为EditText和TextView对象。我已经开发了一些代码来将文本设置为EditText row + col和TextView(row,col),但所有EditText显示2和所有TextView显示(1,1)请检查我的代码并帮助我,我将非常感谢你。
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;
}
}
答案 0 :(得分:0)
您没有更新回收的视图。请仔细阅读适当的教程like this。查看5.5节
修改后的代码:
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;
}
}