Android GridView与不同大小的列

时间:2013-09-26 21:12:08

标签: android gridview

这是我的第一个问题。 我正在为Android开发一个应用程序,我正在使用GridView作为DataGrid。 我有一些有7或8列的网格,我需要根据其内容使gridview的列具有不同的宽度。 我在这里查看所有帖子但找不到解决方案。 我现在正在做的是设置每个视图的LayoutParams计算固定宽度,我为每个视图选择一个百分比,视图获得正确的宽度,但第二个视图的文本显示在第一个视图上,依此类推。 我的所有视图都是TextView或EditText。

我会给你提供任何帮助。 非常感谢你提前!

全型

    @Override
public View getView(int position, View convertView, ViewGroup parent) {
    final int row = GetRow(position);
    View field = null;
    String text = "";
//  convertView = grid.getChildAt(position);
    /*f (grid.getChildAt(position) != null) {
        if (null == convertView) {
            return grid.getChildAt(position);
        } else {
            convertView = null;
        }
    }*/
    int width = 0;
    Point size = new Point();
    ((Activity) context).getWindowManager().getDefaultDisplay().getSize(size);
    width = weights[GetCol(position)] * (size.x - 50) / 100; 

    if (row == 0){
        text = localfieldstitles[position];
        if (convertView != null){
            field = (TextView) convertView;
        } else {
            field = new TextView(context);
        }

        ((TextView)field).setText(text);
        field.setBackgroundResource(R.drawable.buttonblue);
        ((TextView)field).setTextAppearance(context, android.R.style.TextAppearance_DeviceDefault_Medium);
        LayoutParams params = new GridView.LayoutParams(width, LayoutParams.MATCH_PARENT);
        field.setLayoutParams(params);
    } else {
    // Get our text for position
        final int col = GetCol(position);
        try {
            gridData.absolute(row);
            if (gridData.getString(remotefields[col]) != null) {
                text = gridData.getString(remotefields[col]);
            } else {
                text = "";
            }

            if (convertView != null) {
                field = convertView;
                if (convertView instanceof EditText) {
                    ((EditText) field).setText(text);
                } else {
                    ((TextView) field).setText(text);
                }
            } else {
                if (editable[col].equalsIgnoreCase("T")) {
                    field = new EditText(context);
                    ((EditText) field).setText(text);
                    ((EditText) field).setInputType(fieldtypes[col]);
                } else {
                    field = new TextView(context);
                    ((TextView) field).setText(text);
                }
            }
            if (editable[col].equalsIgnoreCase("T")) {
                ((EditText)field).setSingleLine();
                ((EditText)field).setOnEditorActionListener(
                        new EditText.OnEditorActionListener() {
                    @Override
                    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                        if (actionId == EditorInfo.IME_ACTION_DONE || 
                                actionId == EditorInfo.IME_ACTION_NEXT) {
                                try {
                                    if (!isFindColumn(col)) { 
                                        gridData.absolute(row);
                                        switch (gridData.getMetaData().getColumnType(col)) {
                                            case Types.FLOAT: 
                                                case Types.REAL: 
                                                case Types.DECIMAL: gridData.updateFloat(localfields[col], Float.parseFloat(v.getText().toString())); break;
                                            case Types.DOUBLE: gridData.updateDouble(localfields[col], Double.parseDouble(v.getText().toString())); break;
                                            case Types.INTEGER: gridData.updateInt(localfields[col], Integer.parseInt(v.getText().toString())); break;
                                            case Types.VARCHAR: 
                                                case Types.NVARCHAR: 
                                                case Types.CHAR: gridData.updateString(localfields[col], v.getText().toString()); break;                                            
                                        }
                                        datachange.fieldChange(localfields[col], v.getText().toString());
                                    } else {
                                        findColumn.FINDTEXT = v.getText().toString();
                                        executeSearch();
                                    }
                                } catch (SQLException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                        }
                        return true; // pass on to other listeners. 
                    }


                    });
                    ((EditText)field).setOnFocusChangeListener(new EditText.OnFocusChangeListener() {          

                        public void onFocusChange(View v, boolean hasFocus) {
                            if(!hasFocus){
                                try {
                                    if (!isFindColumn(col)) { 
                                        gridData.absolute(row);
                                        switch (gridData.getMetaData().getColumnType(col)) {
                                            case Types.FLOAT: 
                                                case Types.REAL: 
                                                case Types.DECIMAL: gridData.updateFloat(localfields[col], Float.parseFloat(((TextView)v).getText().toString())); break;
                                            case Types.DOUBLE: gridData.updateDouble(localfields[col], Double.parseDouble(((TextView)v).getText().toString())); break;
                                            case Types.INTEGER: gridData.updateInt(localfields[col], Integer.parseInt(((TextView)v).getText().toString())); break;
                                            case Types.VARCHAR: 
                                                case Types.NVARCHAR: 
                                                case Types.CHAR: gridData.updateString(localfields[col], ((TextView)v).getText().toString()); break;                                            
                                        }
                                        datachange.fieldChange(localfields[col], ((TextView)v).getText().toString());
                                    } /*else {
                                        findColumn.FINDTEXT = ((TextView)v).getText().toString();
                                        executeSearch();
                                    }   */                              
                                } catch (SQLException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }   
                            }
                               //do job here owhen Edittext lose focus 
                        }
                    });
            }
            LayoutParams params;
            if (text.equalsIgnoreCase("")) {
                params = new GridView.LayoutParams(width, 40);
            } else {
                params = new GridView.LayoutParams(width, LayoutParams.WRAP_CONTENT);
            }
            field.setLayoutParams(params);

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    //Finally return the view }
    return field; 
}

1 个答案:

答案 0 :(得分:1)

首先告诉我你如何设置gridview?你是否使用setAdapter方法? 如果你告诉我,我可以帮助你,我认为你必须为此目的创建CustomAdapter。所以第一步回答我的问题。