0

I have created tabbed activity with ViewPager Contain two tab,tab one Contain EditText and tab two Contain ListView ,When you write something in EditText and click Save the text is pass to ListView, I want to add an title for ListView. How do I do this?

my fragment_one.java file:

public class FragmentOne extends Fragment {

SendMessage SM;
ViewPager viewPager;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View rootView = inflater.inflate(
            R.layout.fragment_one, container, false);
    return rootView;


}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    Button btnPassData = (Button) view.findViewById(R.id.btnPassData);
    viewPager = (ViewPager) getActivity().findViewById(R.id.viewPager);
    final EditText inData = (EditText) view.findViewById(R.id.inMessage);

    btnPassData.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            SM.sendData(inData.getText().toString().trim());
            viewPager.setCurrentItem(1);
        }
    });

}

interface SendMessage {
    void sendData(String message);
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);

    try {
        SM = (SendMessage) getActivity();
    } catch (ClassCastException e) {
        throw new ClassCastException("Error in retrieving data. Please try again");
    }
}

}

my fragment_two.xml file:`

<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="store.exercise.com.store.MainActivity$PlaceholderFragment">

<ListView
    android:id="@+id/list_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" /></RelativeLayout>
KeLiuyue
  • 8,149
  • 4
  • 25
  • 42
monzer ax
  • 13
  • 6
  • Try adding `addHeaderView` to your list. You can do it by `listView.addHeaderView(textView);` – Procrastinator Sep 15 '17 at 07:15
  • You can view [this](https://stackoverflow.com/questions/7978359/using-listview-how-to-add-a-header-view) thread, it may solve your problem – AwaisMajeed Sep 15 '17 at 07:19
  • Add like this `View header = LayoutInflater.from(this).inflate(R.layout.title_layout, null); mListView.addHeaderView(header);` . – KeLiuyue Sep 15 '17 at 07:28
  • Hello friend You seem to have experience in Android programming, I have a problem and I want you to solve this problem. I have already posted my problem in stackoverflow but I can not find the right solution. Can you help me? – monzer ax Oct 28 '17 at 06:19

0 Answers0