1

Well guys, i'm just trying to creat a listView of Objects from a Parse query. So i don't know too much about list views in android but someone can tell me how to implement that? Or do you have an tutorial that can explain what should I do?

Thank you very much!

rici
  • 234,347
  • 28
  • 237
  • 341
Mriaco
  • 91
  • 1
  • 2
  • 11

2 Answers2

1

ListView is a view group that displays a list of scrollable items. The list items are automatically inserted to the list using an Adapter that pulls content from a source such as an array or database query and converts each item result into a view that's placed into the list.

so create your listview inside xml file using ListView element for example

<ListView
  android:id="@android:id/list"
  android:layout_width="match_parent"
  android:layout_height="wrap_content" >
</ListView> 

Then in your main activity. Make the use of Adapter to fill your list

final ListView listview = (ListView) findViewById(R.id.list);
    String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
        "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
        "Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux",
        "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2",
        "Android", "iPhone", "WindowsMobile" };

    final ArrayList<String> list = new ArrayList<String>();
    for (int i = 0; i < values.length; ++i) {
      list.add(values[i]);
    }
    final StableArrayAdapter adapter = new StableArrayAdapter(this,
        android.R.layout.simple_list_item_1, list);
    listview.setAdapter(adapter);

This will populate all the items from values array into list

for more example and tutorial http://developer.android.com/guide/topics/ui/layout/listview.html

eLemEnt
  • 1,741
  • 14
  • 21
1

I'll show you how I do this. First of all I wouldn't use the query.findInBackground() y would use the query.find() that gives back a list of all the objects you want. To make use of query.find() you have to make a AsyncTask class inside the activity or fragment your going to create your listview in.

    /* the arraylist of RowItemChapter is becouse im making use of a 
    * custom adapter for my listview
    */
    ArrayList<RowItemChapter> grupos;
    ParseObject course;
    // this list is the one whe are going to use to save what the query gives back
    List<ParseObject> ob;
    ProgressDialog mProgressDialog;
    // this would be my custo adater
    ChaptersAdapter adapter;
    // this is my expandableListView
    ExpandableListView listView;

// RemoteDataTask AsyncTask
    class RemoteDataTask extends AsyncTask<Void, Void, Void> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Create a progressdialog
            mProgressDialog = new ProgressDialog(getActivity());
            // Set progressdialog title
            mProgressDialog.setTitle("Looking for Chapters");
            // Set progressdialog message
            mProgressDialog.setMessage("Loading...");
            mProgressDialog.setIndeterminate(false);
            // Show progressdialog
            mProgressDialog.show();
        }

        @Override
        protected Void doInBackground(Void... params) {
            // Create the array
            grupos = new ArrayList<RowItemChapter>();
            try {

                // Locate the class table named "Chapters"
                ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Chapters");
                // look only for objects witch column is equal to the ParseObject course.
                query.whereEqualTo("Curso", course);
                // get data in an ascending list
                query.orderByAscending("position");
                // execute the query and get back a list of the data you where looking for
                ob = query.find();
                // make a for inside the list and set the data to the adapter
                for (ParseObject chapter : ob) {
                    // Locate images in flag column
                    RowItemChapter item = new RowItemChapter((String) chapter.get("name"), new SubrowItemChapter(10 , 5, 80), 80);
                    grupos.add(item);
                }
            } catch (ParseException e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            // Locate the listview in the XML 
            listView = (ExpandableListView) getActivity().findViewById(R.id.listaChapters);
            // Pass the results into ListViewAdapter.java
            adapter = new ChaptersAdapter(getActivity(), grupos);
            // Binds the Adapter to the ListView
            listView.setAdapter(adapter);
            // Close the progressdialog
            mProgressDialog.dismiss();
        }
    }

You shouldnt forget to execute the AsyncTask class inside the onCreate

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new RemoteDataTask().execute();

    }

You might not need must of what I did it was an extract from a proyect I did, if your not making use of any custom adapter its even easier. Hope it helps.