Hide column in listview android

2019-08-14 18:49发布

问题:

I had about 20 columns input into listview. But I want that user will have an option to disable (hide) some columns in view. I found that I can make my own Array Adapter and view for row. But I don't want to define this for 2 to 20 items in row. Is there any option how to do it?

回答1:

I found one way how to do it. For example I have this layout of item in listview. I will made my own adapter and then simply by calling setVisibility(View.GONE); i will hide this item from listview. PS: Here is only example in realcode there should be some IF() to hide column.

Layout for item.

<TextView
    android:id="@+id/description"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

<TextView
    android:id="@+id/value"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/description"
    android:layout_alignBottom="@+id/description"
    android:layout_toRightOf="@+id/description"
    android:text="TextView" />

And MyAdapter.

private class ConnectedAdapter extends ArrayAdapter<ConnectionClass> {

    private ArrayList<ConnectionClass> connectionList;

    public ConnectedAdapter(Context context, int textViewResourceId,
            ArrayList<ConnectionClass> countryList) {
        super(context, textViewResourceId, countryList);
        this.connectionList = new ArrayList<ConnectionClass>();
        this.connectionList.addAll(countryList);
    }

    private class ViewHolder {
        TextView description;
        TextView value;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;
        Log.v("ConvertView", String.valueOf(position));

        if (convertView == null) {
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = vi.inflate(R.layout.activity_connected_item,
                    null);

            holder = new ViewHolder();
            holder.description = (TextView) convertView
                    .findViewById(R.id.description);
            holder.value = (TextView) convertView
                    .findViewById(R.id.value);

            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        ConnectionClass connection = connectedList.get(position);
        holder.description.setText(connection.getDescription());
        holder.description.setVisibility(View.GONE);
        holder.value.setText(connection.getValue());

        return convertView;

    }

}