0

我有一个显示矩阵元素的 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;
        }
    }
4

1 回答 1

0

您没有更新回收的视图。请通过这样的适当教程。检查第 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;
    }
}
于 2012-11-12T09:21:02.623 回答