Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android GridView with Columns of Different Size

This is my first question here. I am developing an app for Android and i'm using GridView as a DataGrid. I have some grids that has 7 or 8 columns and i need to make the columns of the gridview to have different width according to its contents. I check all the Posts here but can't find a solution. What i'm doing now is setting the LayoutParams of each view calculating a fixed width with a percentage i choose for each one, the views gets the correct width but the text of second view is shown over the first one and so on. All my view are TextView or EditText.

I will preciate any help you can give me. Thank you very much in advance!

Maxi

    @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; 
}
like image 903
Maxi Avatar asked Dec 05 '25 10:12

Maxi


1 Answers

Firstly tell me how you set your gridview? did you use setAdapter method or not? I can help you if you tell me that, and I think you must create CustomAdapter for this purpose. So for first step answer my question.

like image 107
Majid Daeinejad Avatar answered Dec 07 '25 00:12

Majid Daeinejad