0

I am trying to get value from a ListView that are in a fragment by using a method inside my MainActivity.

Here is my Fragment where I am adding textview items to my listview:

@Override
public void onStart(){
    super.onStart();
    FirebaseListAdapter<String> adapter = new FirebaseListAdapter<String>(
            getActivity(),
            String.class,
            R.layout.textview_layout,
            mRootRef) {
        @Override
        protected void populateView(View view, String s, int i) {
            EditText editText = (EditText)view.findViewById(R.id.input_edittext);
            editText.setHint(s);
        }
    };
    listView.setAdapter(adapter);
}

And when these are added I try to get the value from each textview with this method inside my fragment:

public void getListViewItems(ListView l){
    for(int i=0;i<l.getAdapter().getCount();i++)
        System.out.println(l.getItemAtPosition(i));
}

I trigger the method from MainActivity like this:

public void getListItems(){
    driverFragment = new DriverFragment();
    driverFragment.getListViewItems(driverFragment.listView);
}

And its all resulting in null object reference:

Attempt to invoke virtual method 'android.widget.ListAdapter android.widget.ListView.getAdapter()' on a null object reference

Solarflare
  • 10,721
  • 2
  • 18
  • 35
  • You are trying to get values from an object which is still not created. – Md Sufi Khan Aug 07 '16 at 08:58
  • Ok, but my list is full of items. – Beard o Mania Aug 07 '16 at 08:59
  • Creating an object of Fragment doesn't mean that it attached with activity. :) The best way to send data from fragment to activity is interface. – Md Sufi Khan Aug 07 '16 at 09:00
  • @Md Sufi Khan Ok. Is there any good tutorial how to send data using interface? – Beard o Mania Aug 07 '16 at 09:05
  • You can find the details explanation in https://developer.android.com/guide/components/fragments.html. Also you may follow http://stackoverflow.com/questions/14439941/passing-data-between-fragments-to-activity and http://stackoverflow.com/questions/9343241/passing-data-between-a-fragment-and-its-container-activity – Md Sufi Khan Aug 07 '16 at 09:12
  • Have some problem getting it to work since in this [post](http://stackoverflow.com/questions/14439941/passing-data-between-fragments-to-activity) the data is passed to **fragments** that are set in XML and my fragments are created programatically. – Beard o Mania Aug 07 '16 at 11:24
  • Setting fragment from xml or from java code will not create any problem I think. – Md Sufi Khan Aug 07 '16 at 12:26

1 Answers1

0

For my case below code working fine.

Activity class

public class MainActivity extends ActionBarActivity implements MenuListFragment.ListCallbacks {

    private static final String TAG = MainActivity.class.getSimpleName();

    private FragmentManager mFragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mFragmentManager = getSupportFragmentManager();

        mFragmentManager.beginTransaction()
                .add(R.id.frg_menu, new MenuListFragment())
                .commit();
    }

    @Override
    public void onListView(ListView listView) {
        BaseAdapter adapter = (BaseAdapter) listView.getAdapter();
        Log.d(TAG, "" + adapter.getCount());
    }
}

Fragment Class

public class MenuListFragment extends Fragment{

    private List<MenuItem> mMenuList = new ArrayList<>();

    private MenuListAdapter mAdapter;

    private ListView lvMenu;

    public interface ListCallbacks {
        void onListView(ListView listView);
    }

    private ListCallbacks mListCallbacks = null;

    public MenuListFragment() {

    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        if(!(activity instanceof ListCallbacks)){
            throw new IllegalStateException("Activity must implement MenuListFragment's callbacks.");
        }

        mListCallbacks = (ListCallbacks) activity;

    }

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

        // Initialized mMenuList

        mAdapter = new MenuListAdapter(getActivity(), R.layout.simple_menu_item, mMenuList);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_menu_list, container, false);

        lvMenu = (ListView) rootView.findViewById(R.id.lv_menu);
        lvMenu.setAdapter(mAdapter);

        return rootView;
    }

    @Override
    public void onResume() {
        super.onResume();

        if(mListCallbacks != null) {
            mListCallbacks.onListView(lvMenu);
        }
    }
}

main_activity

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/frg_menu"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="@dimen/activity_horizontal_margin"
    android:layout_marginRight="@dimen/activity_horizontal_margin"
    tools:layout="@android:layout/list_content">

</FrameLayout>
Md Sufi Khan
  • 1,751
  • 1
  • 14
  • 19
  • Dont sure how I will implement this. So I think that by showing my main and fragment class its get better to understand. Here is my [MainActivity](http://pastebin.com/qEjRX2MF) and here is one of my [Fragments](http://pastebin.com/gStuNuxJ) – Beard o Mania Aug 07 '16 at 14:29