如何从一种方法获取int值到另一种方法

时间:2014-01-28 22:48:25

标签: java android listview count int

我需要将我的int值从一个方法转移到另一个方法,但我没有找到任何解决方案。一种方法计算对象的数量,并且数量需要“传递”到另一个,并检查,如果计数等于某事。

你有什么想法,怎么做?

检查下面的代码:

@Override
protected void onPostExecute(Void result) {
    ParseQuery<Animal> query = ParseQuery.getQuery(Animal.class);
    query.countInBackground(callcount = new CountCallback(){
        public void done(int count, ParseException e) {
            if (e == null) {
                Log.v("count is" + count, "count is");                           
                //I need to export int count from "done" method to another                
            } else {
            }
        }
    }); 
}

secont方法,必须收到该值并使用它:

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
    //i need to set int count from method above here
    int threshold = 1;
    int counter = mListView.getCount();

    if (scrollState == SCROLL_STATE_IDLE) {

    if (mListView.getLastVisiblePosition() >= counter - threshold) {
        updateData();                   
    }
}

整个代码:

public class Fragment1 extends Fragment {
    static ListView mListView;
    static AnimalAdapter mAdapter;
    static ProgressBar mProgressBar;
    static EditText mEditText;
    static LayoutInflater inflater;
    CountCallback callcount;
    int g_count = 0;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        final View rootView = inflater.inflate(R.layout.animalsfrag, container, false);
        mListView = (ListView)rootView.findViewById(R.id.animal_list);

        View header = getActivity().getLayoutInflater().inflate(R.layout.header, null);

        header.setPadding(2, 8, 4, 2);
        mListView.setVisibility(View.INVISIBLE);
        mListView.requestFocus();
        mListView.addHeaderView(header); 

        View footie = getActivity().getLayoutInflater().inflate(R.layout.footer, null);
        mListView.addFooterView(footie);
        footie.setVisibility(View.INVISIBLE);


        mProgressBar = (ProgressBar) rootView.findViewById (R.id.loading_animals);
        mProgressBar.setVisibility(View.VISIBLE);

        RemoteDataTask task = new RemoteDataTask();
        task.execute();




        return rootView;
    }



    public void updateData() {
         mListView = (ListView)getView().findViewById(R.id.animal_list);     
   final ParseQuery<Animal> query = ParseQuery.getQuery(Animal.class);
    query.setCachePolicy(CachePolicy.NETWORK_ONLY);
    query.orderByAscending("animal");
    query.setLimit(mListView.getCount() + 5);

    Log.v("updateData", "uploading data from Parse.com");
    query.findInBackground(new FindCallback<Animal>() {

        @Override
          public void done(List<Animal> animals, ParseException error) {

              if(animals != null){
                mAdapter.clear();
                mProgressBar = (ProgressBar) getView().findViewById (R.id.loading_animals);
               mProgressBar.setVisibility(View.INVISIBLE);
             RelativeLayout footie = (RelativeLayout) getView().findViewById(R.id.footerview);  
            footie.setVisibility(View.VISIBLE);

              mAdapter.clear();
            for (int i = 0; i < animals.size(); i++) {

                      mAdapter.add(animals.get(i));

                    }  

              }  
            }
         }); }






     private class RemoteDataTask extends AsyncTask<Void, Void, Void> {



         @Override
            protected void onPreExecute() {
                super.onPreExecute();  }

         @Override
            protected Void doInBackground(Void... params) {

                return null;



         }


         @Override
            protected void onPostExecute(Void result) {
             ParseQuery<Animal> query = ParseQuery.getQuery(Animal.class);
             query.countInBackground(callcount = new CountCallback(){
                 public void done(int count, ParseException e) {
                        if (e == null) {
                            Log.v("count is" + count, "count is");
                         if (mListView.getCount() == count) {
                             Log.v("all loaded", "executed");
                             g_count=count;
                         }
                        } else {
                          // The request failed
                        }
                      }
                    }); 

                Log.v("onpostexecute", "executing");
               mListView = (ListView) getView().findViewById(R.id.animal_list);
               if(mListView.getCount() == 0){
                    updateData();

                }


               mEditText = (EditText) getView().findViewById(R.id.search_animal);

               mAdapter = new AnimalAdapter(getActivity(), new ArrayList<Animal>());

               mListView.setVisibility(View.VISIBLE);
               mListView.setTextFilterEnabled(true);
               mListView.setAdapter(mAdapter);




               mListView.setOnScrollListener(new OnScrollListener() {

                    @Override
                    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
                            int totalItemCount) {
                    }


                    @Override
                    public void onScrollStateChanged(AbsListView view,
                            int scrollState) {

                        int threshold = 1;
                        int counter = mListView.getCount();

                        if (scrollState == SCROLL_STATE_IDLE) {

                            if (mListView.getLastVisiblePosition() >= counter
                                    - threshold){

                                updateData();

                                Log.v("count", "count is" + g_count);

                                }
                            }


                        if (SCROLL_STATE_TOUCH_SCROLL == scrollState) {
                            View currentFocus = getActivity().getCurrentFocus();
                            if(currentFocus != null) {
                                currentFocus.clearFocus();
                            }
                        }



                    }

                });


                mEditText.addTextChangedListener(new TextWatcher(){

                    @Override
                    public void afterTextChanged(Editable s) {}

                    @Override
                    public void beforeTextChanged(CharSequence s,
                            int start, int count, int after) {}

                    @Override
                    public void onTextChanged(CharSequence s, int start,
                            int before, int count) {

                        System.out.println("Text ["+s+"]");
                        mAdapter.getFilter().filter(s.toString());  
                        }
                });

               }

            }
         }

我很乐意提出任何建议。 提前谢谢。

1 个答案:

答案 0 :(得分:1)

如果你没有从另一个调用函数,那么你需要创建一个全局变量。 在您的类声明之后声明一个变量,即

int g_count = 0;

然后在

 public void done(int count, ParseException e) {
                    if (e == null) {
                        Log.v("count is" + count, "count is");
                        g_count=count;
                     }

然后在第二次成功

public void onScrollStateChanged(AbsListView view, int scrollState) {
int count=g_count; \\you dont really need to declare this, just use g_count
int threshold = 1;
int counter = mListView.getCount();

if (scrollState == SCROLL_STATE_IDLE) {

if (mListView.getLastVisiblePosition() >= counter - threshold) {
    updateData();                   
}
}