tofu951753
tofu951753

Reputation: 1

textView not showing up in fragment

So I'm trying to create a list fragment with a textview on top of the list items, but I can't get the textview to show up.

Here is my code for the list fragment

public class VarsFragment extends ListFragment {

private ArrayList<VarData> VarList;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    VarList = AllVars.get(getActivity()).getAllVars();
    VarAdapter varAdapter = new VarAdapter(VarList);
    setListAdapter(varAdapter);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    LinearLayout rootView = (LinearLayout) inflater.inflate(R.layout.fragment_vars, container, false);
    TextView Header = (TextView) rootView.findViewById(R.id.VarHeader);
    Header.setText("Variables");
    return rootView;
}

private class VarAdapter extends ArrayAdapter<VarData> {
    public VarAdapter(ArrayList<VarData> variables) {
        super(getActivity(), R.layout.list_item, variables);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView == null) {
            convertView = getActivity().getLayoutInflater().inflate(R.layout.list_item, null);
        }
        VarData varData = getItem(position);
        TextView varNameTextView = (TextView) convertView.findViewById(R.id.varstv1);
        varNameTextView.setText(varData.getVarName());
        TextView varValueTextView = (TextView) convertView.findViewById(R.id.varstv2);
        varValueTextView.setText(varData.getVarValue());
        ImageView editIndicator = (ImageView) convertView.findViewById(R.id.editable_indicator);
        if (varData.isEditable())
        {
            editIndicator.setVisibility(View.VISIBLE);
        }
        else
        {
            editIndicator.setVisibility(View.INVISIBLE);
        }
        return convertView;
    }
}

public static VarsFragment newInstance(String text) {

    VarsFragment f = new VarsFragment();
    Bundle b = new Bundle();
    b.putString("msg", text);

    f.setArguments(b);

    return f;
}

And here is the code for the xml

<LinearLayout
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity$VarsFragment"
android:gravity="center_horizontal"
android:background="#ffff8800">

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

<ListView
    android:id="@android:id/list"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

 </LinearLayout>

When I run my app, my list shows up, but my textview remains empty instead of showing the header "Variables".

Thanks for any help

edit: Don't I just add

TextView Header;

at the top and

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    Header = (TextView) view.findViewById(R.id.VarHeader);
    Header.setText("Variables");
    Header.setVisibility(View.VISIBLE);
}

Upvotes: 0

Views: 2110

Answers (1)

Dheeraj Shetty
Dheeraj Shetty

Reputation: 11

I had a similar problem. In my case the the textview was hidden because of the action bar. I added "paddingTop" to my fragments layout to fix this.

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="@dimen/action_bar_height"   
    tools:context="my frag context">

action_bar_height = 56dp(Portrait) or 48dp(Landscape)

Upvotes: 1

Related Questions