简体   繁体   中英

Access method in class

How can i call the listview method out of the FragmentTwo class? I'm using a NavDrawer which is fragment-based, so i have to initiate the ListView in the FragmentTwo class. This is my code:

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

                }
            });
        }


    }

}

My main problem is the error message

    .....is not an enclosing class

when calling list.this.listview(); from the onCreate method.

This isn't the right way to call methods in your Fragment 's parent Activity .

You can get the parent Activity with:

MainActivity activity = (MainActivity) getActivity();

And you can then access its methods in this style:

activity.callSomeMethodInYourActivity();

Update:

Also your onCreateView() throws an "unrechable statement" error because you have code after the return statement. This code can't be executed because the method is quit when the return statement is reached.

Complete solution:

Place this in your MainActivity.java file:

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

And your fragment should look like this:

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

After all I don't really understand why you are not placing the listview() and registerClickCallback() methods directly in your Fragment . IMHO this would make more sense...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM