通过MainActivity

时间:2016-08-07 08:55:12

标签: android listview android-fragments

我试图通过使用MainActivity中的方法从片段中的ListView获取值。

这是我的片段,我将textview项目添加到我的列表视图中:

@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);
}

当添加这些内容时,我尝试使用此方法从片段中获取每个textview的值:

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

我从 MainActivity 触发方法,如下所示:

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

它全部导致空对象引用:

  
    

尝试在空对象引用上调用虚拟方法'android.widget.ListAdapter android.widget.ListView.getAdapter()'

  

1 个答案:

答案 0 :(得分:0)

对于我的情况,下面的代码工作正常。

活动类

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());
    }
}

片段类

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>