如何从FragmentTwo类中调用listview方法?我正在使用基于片段的NavDrawer,所以我必须在FragmentTwo类中启动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方法。
答案 0 :(得分:1)
这不是在Fragment
的父Activity
中调用方法的正确方法。
您可以使用以下内容获取父Activity
MainActivity activity = (MainActivity) getActivity();
然后您可以使用此样式访问其方法:
activity.callSomeMethodInYourActivity();
<强>更新强>
同样,您的onCreateView()
会抛出“不可替换的语句”错误,因为您在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
中。恕我直言,这会更有意义......