HOW TO IMPLEMENT ONITEMCLICK METHOD IN FRAGMENTLIST?

marcel ethan

Total Post:105

Points:735
Posted by  marcel ethan
 1418  View(s)
Ratings:
Rate this:
Hi Expert! 

I need listView in one tab fragment, so I implemented code from this example: http://geekswithblogs.net/bosuch/archive/2011/01/31/android---create-a-custom-multi-

line-listview-bound-to-an.aspx. The list shows up, when some operation in MainActivity is completed, but I don't know how to do it clickable. I tried like is shown in this example, but it's not working, I think the problem is because I have fragment, this example is in MainActivity. Please, help!

public class FragmentLogbook extends ListFragment {

        //handler for passing data through thread
        private Handler handler;
        //progress dialog
        private ProgressDialog progressDialog;

        private int progressBarStatus;

        SingletonLogbookPack mSingletonLogbookPack = SingletonLogbookPack.getInstance();
        ArrayList<SearchResults> searchResults = GetSearchResults();

        MyCustomBaseAdapter mAdapter;

        // Create an empty adapter we will use to display the loaded data.
        //CustomArrayAdapter mAdapter = new CustomArrayAdapter(getActivity());


        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState); 

            handler = new Handler(); 
            startDownloadingLogbooks();

        } 

        private void startDownloadingLogbooks() {   
            progressDialog = new ProgressDialog(getActivity());
            //progressDialog = ProgressDialog.show(getActivity(),"Loading Logbooks", "Please wait...");
            progressDialog.setCancelable(false);
            progressDialog.setMessage("Loading Logbooks ...");
            progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            progressDialog.setProgress(0);
            progressDialog.setMax(100);
            progressDialog.show();

            //reset progress bar status
            progressBarStatus = 0;

            // start the time consuming task in a new thread
            Thread thread = new Thread() {
                public void run () {
                    boolean p = ((MainActivity)getActivity()).mGpsColibri.mBinary.getLogbook();
                    while ( ((MainActivity)getActivity()).logbookFinished != true ) {
                        progressBarStatus = (int) ((MainActivity)getActivity()).GetProgressValue();
                        progressDialog.setProgress(progressBarStatus);
                        //in this time Progress dialog will be on screen                    
                    }
                    //set logbookFinished variable from MainActivity to false
                    ((MainActivity)getActivity()).logbookFinished = false;
                    // this will handle the post task.
                    // it will run when the time consuming task get finished
                    handler.post(new Runnable() {
                        @Override
                        public void run() {                     
                            // Update your UI or
                            // do any Post job after the time consuming task
                            // remember to dismiss the progress dialog here.
                            updateUI();
                            progressDialog.dismiss(); 
                        }
                    });
                }
            };

            thread.start();
        }

        private void updateUI() {
            //setListAdapter(mAdapter);

            mAdapter = new MyCustomBaseAdapter(getActivity(), searchResults);
            setListAdapter(mAdapter);
        }

        private ArrayList<SearchResults> GetSearchResults(){
            ArrayList<SearchResults> results = new ArrayList<SearchResults>();

            SearchResults sr1 = new SearchResults();
            sr1.setName("John Smith");
            sr1.setCityState("Dallas, TX");
            sr1.setPhone("214-555-1234");
            results.add(sr1);

            sr1 = new SearchResults();
            sr1.setName("Jane Doe");
            sr1.setCityState("Atlanta, GA");
            sr1.setPhone("469-555-2587");
            results.add(sr1);

            sr1 = new SearchResults();
            sr1.setName("Steve Young");
            sr1.setCityState("Miami, FL");
            sr1.setPhone("305-555-7895");
            results.add(sr1);

            sr1 = new SearchResults();
            sr1.setName("Fred Jones");
            sr1.setCityState("Las Vegas, NV");
            sr1.setPhone("612-555-1234");
            results.add(sr1);

            sr1 = new SearchResults();
            sr1.setName("Bob Marsh");
            sr1.setCityState("New York, NY");
            sr1.setPhone("612-555-5678");
            results.add(sr1);

            sr1 = new SearchResults();
            sr1.setName("Harold Funk");
            sr1.setCityState("Chicago, IL");
            sr1.setPhone("612-555-8765");
            results.add(sr1);

            sr1 = new SearchResults();
            sr1.setName("Scott Dorf");
            sr1.setCityState("Winslow, AZ");
            sr1.setPhone("612-555-5432");
            results.add(sr1);

            sr1 = new SearchResults();
            sr1.setName("Mike Hail");
            sr1.setCityState("Seattle, WA");
            sr1.setPhone("612-555-0961");
            results.add(sr1);

            sr1 = new SearchResults();
            sr1.setName("Scott Dorf");
            sr1.setCityState("Winslow, AZ");
            sr1.setPhone("612-555-5432");
            results.add(sr1);

            sr1 = new SearchResults();
            sr1.setName("Mike Hail");
            sr1.setCityState("Seattle, WA");
            sr1.setPhone("612-555-0961");
            results.add(sr1);

            return results;
        }
    }

Thanks in advance.
  1. AVADHESH PATEL

    Post:604

    Points:4228
    Re: How to implement onItemClick method in FragmentList?

    Hi Marcel,

    Try this after setting listview adapter

    getListView().setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // Here you write auto - generated method stub
        }
    });

    It might be helpful for you.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!