class 中的访问方法

Access method in class

如何从 FragmentTwo class 中调用 listview 方法?我正在使用基于片段的 NavDrawer,因此我必须在 FragmentTwo class 中启动 ListView。 这是我的代码:

    package xy;
    import android.content.Context;
    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    import android.widget.TextView;
    import android.widget.Toast;


    public class FragmentTwo extends Fragment {
View rootview;
private Context context;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    rootview = inflater.inflate(R.layout.fragment_two, container, false);
    return rootview;
    list.this.listview();
}
class list extends MainActivity{
        public void listview(){
            String[] listcontent = {"Test1","text2","text4"};
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, getView().R.layout.listviewcontent, listcontent);
            ListView listView = (ListView) getView().findViewById(R.id.list);
            listView.setAdapter(adapter);
        }
        public void registerClickCallback() {
            ListView listView = (ListView) findViewById(R.id.list);
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    TextView textView = (TextView) view;
                    String message = "You clicked # " + position + " which is:" + textView.getText().toString();
                    Toast.makeText(list.this, message, Toast.LENGTH_LONG).show();

                }
            });
        }


    }

}

我的主要问题是错误信息

    .....is not an enclosing class

当调用 list.this.listview() 时;来自 onCreate 方法。

这不是在您的 Fragment 的父级 Activity 中调用方法的正确方法。

您可以通过以下方式获取父项 Activity

MainActivity activity = (MainActivity) getActivity();

然后您可以以这种方式访问​​它的方法:

activity.callSomeMethodInYourActivity();

更新:

您的 onCreateView() 也会抛出 "unrechable statement" 错误,因为您在 return 语句之后有代码。无法执行此代码,因为该方法在到达 return 语句时退出。

完整解决方案:

将其放入您的 MainActivity.java 文件中:

public void listview(){
    String[] listcontent = {"Test1","text2","text4"};
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, getView().R.layout.listviewcontent, listcontent);
    ListView listView = (ListView) getView().findViewById(R.id.list);
    listView.setAdapter(adapter);
}

public void registerClickCallback() {
    ListView listView = (ListView) findViewById(R.id.list);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            TextView textView = (TextView) view;
            String message = "You clicked # " + position + " which is:" + textview.getText().toString();
            Toast.makeText(list.this, message, Toast.LENGTH_LONG).show();
        }
    });
}

你的片段应该是这样的:

public class FragmentTwo extends Fragment {
    View rootview;
    private Context context;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootview = inflater.inflate(R.layout.fragment_two, container, false);

        MainActivity activity = (MainActivity) getActivity();
        activity.listview()

        return rootview;
    }
}

毕竟我真的不明白你为什么不把 listview()registerClickCallback() 方法直接放在你的 Fragment 中。恕我直言,这会更有意义...