[英]How to get and set text to an EditText in GridView in Android?
I have a GridView that displays matrix element and each of the element consists of an EditText and a TextView object. 我有一个显示矩阵元素的GridView,每个元素都包含一个EditText和一个TextView对象。
how can I get access to a specific EditText and TextView object in the GridView? 如何获得对GridView中特定EditText和TextView对象的访问?
I want to get and set text to EditText and TextView object dynamically. 我想动态获取文本并将其设置为EditText和TextView对象。 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.
我已经开发了一些代码来将文本设置为EditText row + col和TextView(行,列),但是所有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;
}
}
You are not updating the recycled views. 您没有更新回收的视图。 Please go through a proper tutorial like this .
请仔细阅读类似的教程。 Check the section 5.5
检查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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.