Android-尝试从片段调用父方法
从列表适配器调用父函数时遇到问题。 本质上,这是一个按钮,它将在执行一些DB操作后再次重新加载列表 我的列表适配器如下所示: 列出适配器Android-尝试从片段调用父方法,android,listadapter,Android,Listadapter,从列表适配器调用父函数时遇到问题。 本质上,这是一个按钮,它将在执行一些DB操作后再次重新加载列表 我的列表适配器如下所示: 列出适配器 public class LazyAdapterFragmentCheckInList extends BaseAdapter { private FragmentActivity activity; private ArrayList<HashMap<String, String>> data; private static La
public class LazyAdapterFragmentCheckInList extends BaseAdapter {
private FragmentActivity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;
public LazyAdapterFragmentCheckInList(FragmentActivity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity);
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.list_row_checkin, null);
Button checkin=(Button)vi.findViewById(R.id.checkin);
checkin.setText("reload");
checkin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
此列表适配器是从列表片段调用的,该片段具有公共方法“GetBlogPostsTask”
列表片段
public class CheckInListViewFragment extends ListFragment {
Context _context;
ListView list;
LazyAdapterFragmentCheckInList adapter;
Context appContext;
ArrayList<HashMap<String, String>> songsList;
View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
appContext = inflater.getContext().getApplicationContext();
View rootView = inflater.inflate(R.layout.fragment_custom_listview_fragment, container, false);
return rootView;
}
@Override
public void onCreate(Bundle savedInstanceState) {
Log.v("a=", "b");
super.onCreate(savedInstanceState);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
GetBlogPostsTask getBlogPostsTask = new GetBlogPostsTask();
getBlogPostsTask.execute();
list = (ListView) getActivity().findViewById(android.R.id.list);
mProgressBar = (ProgressBar) getActivity().findViewById(R.id.progressBar1);
mProgressBar.setVisibility(View.VISIBLE);
Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "trade-gothic-next-condensed-latin-1.ttf");
}
public class GetBlogPostsTask extends AsyncTask<Object, Void, JSONObject> {
ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
@Override
public void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
@Override
public JSONObject doInBackground(Object... arg0){
Log.v("NEWURL=", NEWURL);
JSONObject jsonResponse = null;
jsonResponse= JSONFunctions.getJSONfromURL(NEWURL);
return jsonResponse;
}
public void logException(Exception e) {
Log.e(TAG, "Exception Caught", e);
}
public void handleBlogRequest(){
mProgressBar.setVisibility(View.INVISIBLE);
if(mBlogData==null){
ArrayList<HashMap<String, String>> blogPosts = new ArrayList<HashMap<String, String>>();
HashMap<String, String> blogPost = new HashMap<String, String>();
blogPost.put("position", "1");
blogPosts.add(blogPost);
adapter=new LazyAdapterFragmentCheckInList(getActivity(),blogPosts);
list.setAdapter(adapter);
}
else{
try{
JSONArray jsonPosts = mBlogData.getJSONArray("tickets");
int jsonLength = jsonPosts.length();
if(jsonLength>20){
jsonLength = 20;
}
ArrayList<HashMap<String, String>> blogPosts = new ArrayList<HashMap<String, String>>();
for(int i = 0; i <jsonLength; i++){
JSONObject post = jsonPosts.getJSONObject(i);
HashMap<String, String> blogPost = new HashMap<String, String>();
}
adapter=new LazyAdapterFragmentCheckInList(getActivity(),blogPosts);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> Parent, View view, int position, long id) {
try{
}
catch(JSONException e){
logException(e);
}
}
});
}
catch (JSONException e){
logException(e);
}
}
}
@Override
public void onPostExecute(JSONObject result) {
mBlogData = result;
handleBlogRequest();
}
}
}
公共类CheckInListViewFragment扩展ListFragment{
语境(Context)语境;;
列表视图列表;
LazyAdapterFragmentCheckInList适配器;
语境;
ArrayList songsList;
视图;
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
appContext=inflater.getContext().getApplicationContext();
视图根视图=充气器。充气(R.layout.fragment\u custom\u listview\u fragment,container,false);
返回rootView;
}
@凌驾
创建时的公共void(Bundle savedInstanceState){
Log.v(“a=”,“b”);
super.onCreate(savedInstanceState);
}
@凌驾
已创建ActivityState上的公共无效(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
GetBlogPostsTask GetBlogPostsTask=新建GetBlogPostsTask();
getBlogPostsTask.execute();
list=(ListView)getActivity().findviewbyd(android.R.id.list);
mProgressBar=(ProgressBar)getActivity().findViewById(R.id.progressBar1);
mProgressBar.setVisibility(View.VISIBLE);
Typeface font=Typeface.createFromAsset(getActivity().getAssets(),“trade-gothic-next-condensed-latin-1.ttf”);
}
公共类GetBlogPostsTask扩展异步任务{
ArrayList songsList=新的ArrayList();
@凌驾
公共无效onPreExecute(){
//TODO自动生成的方法存根
super.onPreExecute();
}
@凌驾
公共JSONObject doInBackground(对象…arg0){
Log.v(“NEWURL=”,NEWURL);
JSONObject jsonResponse=null;
jsonResponse=JSONFunctions.getJSONfromURL(NEWURL);
返回jsonResponse;
}
公共无效日志例外(例外e){
Log.e(标记“捕获异常”,e);
}
公共无效handleBlogRequest(){
mProgressBar.setVisibility(视图.不可见);
if(mBlogData==null){
ArrayList blogPosts=新的ArrayList();
HashMap blogPost=新的HashMap();
blogPost.put(“位置”,“1”);
添加(blogPost);
adapter=newlazyadapterFragmentCheckInList(getActivity(),blogPosts);
list.setAdapter(适配器);
}
否则{
试一试{
JSONArray jsonPosts=mBlogData.getJSONArray(“票证”);
int jsonLength=jsonPosts.length();
如果(jsonLength>20){
jsonLength=20;
}
ArrayList blogPosts=新的ArrayList();
对于(int i=0;i,在您的情况下,所需的方法如下
CheckInListViewFragment.GetBlogPostsTask getBlogPostsTask = new CheckInListViewFragment.new GetBlogPostsTask();
但这不是一个好的解决方案。分离异步任务更好
因为,GetBlogPostsTask只能通过CheckInListViewFragment实例访问
用法1
public class SampleClass {
public class NestedClass {
}
}
上述使用的无障碍方式
new SampleClass().new NestedClass();
new SampleClass.NestedClass();
用法2
public class SampleClass {
public static class NestedClass {
}
}
上述使用的无障碍方式
new SampleClass().new NestedClass();
new SampleClass.NestedClass();
我建议您不要在另一个类中嵌套AsyncTask类。嗨,Blackkara,谢谢您的解决方案。当我尝试此操作时,我会遇到类似的错误“不在封闭类中”.我不知道为什么,因为一切都是公开的。谢谢Blackkara,这让我访问了很棒的。我现在收到了其他错误,因为我认为变量没有被正确访问,比如JSON URL。-非常感谢